• sulcud

    @flipflap, I know this is not WebView based but I hope it also helps you to execute the JavaScript you need

    import objc_util
    
    
    class JavaScriptVM(object):
        def __init__(self):
            self._framework = objc_util.load_framework("JavaScriptCore")
            self._JSContext = objc_util.ObjCClass("JSContext")
            self._JSVirtualMachine = objc_util.ObjCClass("JSVirtualMachine")
            self.context = None
            self.javascript_vm = None
            
            self._prepare_vm()
    
        def _prepare_vm(self):
            context = self._JSContext.new()
            self.javascript_vm = self._JSVirtualMachine.new()
            self.context = context.initWithVirtualMachine_(self.javascript_vm)
            
        def evaluate_script(self, script_code: str):
            if self.context is not None:
                if self.javascript_vm is not None:
                    return self.context.evaluateScript(script_code)
    
    
    code = """function test(a, b){
        return a + b;
    }
    test(1+2, 3)+3"""
    vm = JavaScriptVM()
    r = vm.evaluate_script(code)
    print(r) 
    

    posted in Pythonista read more
  • sulcud

    @ccc said:

    You would need compile it into the app. Which is not be possible without the source code to Pythonista. You would be able to do it with Pyto which is open source but it would be complicated.

    You are right. With the AppStore version of both apps is not possible, I need to compile my own, at least can test it with Pyto.

    In my last approach I ended by compiling a test C library for arm64 and then I signed it with codesign, Pythonista apparently accept the dynamic library but stops importing it as the identity used to signed it is not the same of the identity the owner of the app used (or at least that is what I think, really don’t know which other thing is preventing to load it)

    If you are interested in reading the traceback of Pythonista:

    Traceback (most recent call last):
      File "/private/var/mobile/Containers/Shared/AppGroup/HIDDEN/Pythonista3/Documents/hw.py", line 4, in <module>
        test_lib = ctypes.cdll.LoadLibrary("./test-lib_1.so")
      File "/var/containers/Bundle/Application/HIDDEN/Pythonista3.app/Frameworks/Py3Kit.framework/pylib/ctypes/__init__.py", line 427, in LoadLibrary
        return self._dlltype(name)
      File "/var/containers/Bundle/Application/HIDDEN/Pythonista3.app/Frameworks/Py3Kit.framework/pylib/ctypes/__init__.py", line 349, in __init__
        self._handle = _dlopen(self._name, mode)
    OSError: dlopen(/private/var/mobile/Containers/Shared/AppGroup/HIDDEN/Pythonista3/Documents/test-lib_1.so, 0x0006): code signature invalid (errno=1) sliceOffset=0x00000000, codeBlobOffset=0x00008050, codeBlobSize=0x00004900 for '/private/var/mobile/Containers/Shared/AppGroup/HIDDEN/Pythonista3/Documents/test-lib_1.so'
    

    How apple encapsulates the apps is very interesting and I still believe that someone super skilled can archive this, anyway, thanks again @ccc for your quick answer

    posted in Pythonista read more
  • sulcud

    Hello everyone, I have this stupid question that has a high chance of receive a NO as answer.

    First the background.
    Recently I have testing the python feature of importing code written in C and compiled into dynamic libraries .so files on my PC, In my tests I could notice that I can import files with only the read and write permissions, so base on that I conclude that I don't need the execution permission in that kind of file to import them to python.

    All of those tests where made approaching

    import ctypes
    
    
    my_c_library = ctypes.cdll.LoadLibrary("/path/to/library")
    my_c_library.my_c_function(arg_1, arg_2)
    

    Now that I have access to a Mac I was trying to approach the Xcode tools to compile a dynamic library and import it to Pythonista, the problem is that it is extremely hard to setup the environment for the compilation to iOS, I mean, I could compile to arm64 but looks like iOS has other libraries that I have no access to (or at least I don't know where to find them)

    If anyone have a solution or have an alternative way to approach something similar I really will appreciate it

    posted in Pythonista read more
  • sulcud

    PyCharm could be a good alternative

    posted in Pythonista read more
  • sulcud

    Up to date pre-installed modules

    posted in Pythonista read more
  • sulcud

    I think that erasing a Pythonista build-in module is not possible, remember that python has a weird importing system so if you have a script/folder called speech in the same folder of your main script maybe you are importing it instead of the build in one. (python import order is: current dir, ... , site-packages, build-in) but If that is not the case, I think a fresh install of Pythonista could work

    posted in Pythonista read more
  • sulcud

    @cvp, you are right, using 0 fixes it, did you find anyway to fix the false positives that is_paused and is_speaking functions return?

    posted in Pythonista read more
  • sulcud

    @lyubomyr83, I made this class to make it easier to work with the reference @ccc is pointing to, all could be done thanks to the work of @JonB

    
    import objc_util
    import re
    
    
    class SpeechModule(object):
        def __init__(self):
            self.__AVSpeechUtterance = objc_util.ObjCClass('AVSpeechUtterance')
            self.__AVSpeechSynthesizer = objc_util.ObjCClass('AVSpeechSynthesizer')
            self.__AVSpeechSynthesisVoice = objc_util.ObjCClass('AVSpeechSynthesisVoice')
            self.__synthesizer = self.__AVSpeechSynthesizer.new()
    
        def get_synthesis_languages(self) -> list:
            return self.__AVSpeechSynthesisVoice.speechVoices()
    
        def say(self, msg: str, language: objc_util.ObjCInstance, rate=0.5):
    
            utterance = self.__AVSpeechUtterance.speechUtteranceWithString_(msg)
            utterance.rate = rate
            utterance.voice = voice
            utterance.useCompactVoice = False
            self.__synthesizer.speakUtterance_(utterance)
    
        def is_speaking(self) -> bool:
            return self.__synthesizer.isSpeaking()
    
        def is_paused(self) -> bool:
            return self.__synthesizer.isPaused()
    
        def pause(self) -> bool:
            return self.__synthesizer.pauseSpeakingAtBoundary_(True)
    
        def stop(self) -> bool:
            return self.__synthesizer.stopSpeakingAtBoundary_(True)
    
        def continue_speaking(self):
            self.__synthesizer.continueSpeaking()
    
    

    Here is an example how to use it

    
    import time
    
    speech_ = SpeechModule()
    
    available_voices = speech_.get_synthesis_languages()
    
    us_english_voices = tuple(filter(lambda voice: "en-US" in str(voice), available_voices))
    
    voice = us_english_voices[0]
    
    speech_.say('Hello friend! how are you?', voice, 0.3)
    
    time.sleep(1)
    speech_.stop()
    print(speech_.is_speaking())
    speech_.say("nevermind", voice)
    

    For some reason I can’t make that pause function to work correctly, anyway, it is an idea for your project and maybe my class could be buggy, so objc gurus invocation (@cvp @mikael @JonB @ccc )

    posted in Pythonista read more

Internal error.

Oops! Looks like something went wrong!