def run(self): sys.stdin = self sys.stdout = self sys.stderr = self try: ExceptionHooks().add_hook(lambda *args: os._exit(1)) self.interpreter.interact(banner="") except ExitException as e: MessageHandler().emit(f"exiting interpreter: {repr(e)}", MessageLevel.INFO) except Exception as e: MessageHandler().emit(f"exception in interpreter: {repr(e)}") MessageHandler().emit("interpreter stopped", MessageLevel.INFO) sys.stdin = sys.__stdin__ sys.stdout = sys.__stdout__ sys.stderr = sys.__stderr__
def parse(self): name = self.name[:-len(ModuleTree.file_extension)] try: functions = Function.match(self, name) self.nodes.extend(functions) classes = Class.match(self, name) self.nodes.extend(classes) except ImportException as e: MessageHandler().emit( f"unable to import python module: {self.path} {e}", MessageLevel.WARNING) self.has_error = True
def hook(exec_type, exec_value, exec_trackback): string = "".join( traceback.format_exception(exec_type, exec_value, exec_trackback)) sys.stdin = sys.__stdin__ sys.stdout = sys.__stderr__ sys.stderr = sys.__stderr__ print(string) MessageHandler().emit( f"unhandlable exception occurred, please check the traceback", MessageLevel.ERROR) for h in self.hooks: h(exec_type, exec_value, exec_trackback)
def pre_app(): # fix windows icon not displaying if sys.platform == "win32": import ctypes appid = "com.troppydash.werry_math" ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(appid) # set current directory as path currentDir = os.path.dirname(os.path.realpath(__file__)) # sys.path.insert(0, os.path.realpath(currentDir)) # add python if exists pythonlib_paths = [] try: if sys.platform == "win32": python_path = subprocess.check_output( "python -c \"import sys;print(sys.path)\"", shell=True).strip() python_path = python_path.decode('utf-8') if r'Microsoft\WindowsApps' not in python_path: paths = eval(python_path) paths = filter(lambda x: len(x) > 0, paths) pythonlib_paths.extend(paths) MessageHandler().emit(f"found python with: {python_path}", MessageLevel.DEBUG) else: MessageHandler().emit( f"found msstore python, please use another version of python", MessageLevel.DEBUG) else: python_path = subprocess.check_output( 'python3 -c \"import sys;print(sys.path)\"', shell=True).strip() python_path = python_path.decode('utf-8') paths = eval(python_path) paths = filter(lambda x: len(x) > 0, paths) pythonlib_paths.extend(paths) MessageHandler().emit(f"found python with: {paths}", MessageLevel.DEBUG) except Exception as e: MessageHandler().emit(str(e)) if len(pythonlib_paths) == 0: MessageHandler().emit( f"unable to import python on current platform {sys.platform}") # include base zip if os.path.exists('pythonlib.zip'): MessageHandler().emit(f"found pythonlib.zip", MessageLevel.DEBUG) PathHandler().insert_to_path(os.path.join(currentDir, 'pythonlib.zip')) # add to path for path in pythonlib_paths: PathHandler().add_to_path(path) # register exception hook ExceptionHooks().add_hook(lambda *a, **k: QApplication.quit())
def __init__(self, newLine, *args, **kwargs): super(TerminalWorker, self).__init__(*args, **kwargs) self.event = threading.Event() self.signals = TerminalWorkerSignals() try: exec('from utilities.markers import Proxy') exec('Proxy.proxy_fn = self.proxy') except Exception as e: MessageHandler().emit(f"unable to import module 'Proxy': {repr(e)}", MessageLevel.WARNING) self.interpreter = CustomConsole(self.signals.started) self.line = "" self.newLine = newLine
def update_syspath(self): MessageHandler().emit(f"updated path: {sys.path}", MessageLevel.DEBUG) MessageHandler().emit(f"updated external: {self.external}", MessageLevel.DEBUG)
def throw_exception(self): MessageHandler().emit( f"unhandlable exception occurred, please check the traceback", MessageLevel.ERROR) for h in self.hooks: h(None, None, None)
dark_palette.setColor(QPalette.HighlightedText, Qt.white) app.setPalette(dark_palette) if __name__ == '__main__': # parser config and help messages p = CLIParser(sys.argv[1:], DisplayHelper()) # create display config config = DisplayConfig(p) # set logging level value = config.value('lvl') if value: level = MessageLevel.from_str(value) MessageHandler(level).emit( f"message handler started with level: {value}", MessageLevel.INFO) # add imports and sys.path pre_app() app = QApplication(sys.argv) # create splash screen splash = SplashScreen() splash.show() SplashScreen().displayMessage("loading styles") # set styling post_app(app) # start display display = Display(config)
def proxy(self, fn, args, kwargs): try: exec('from utilities.markers import ProxyPackage') self.signals.proxy.emit(eval('ProxyPackage(fn, args, kwargs)')) except Exception as e: MessageHandler().emit(f"unable to import module 'ProxyPackage': {repr(e)}", MessageLevel.WARNING)