#!/usr/bin/env python3 """ Main entry point for the application. """ import sys import threading import traceback import logging import urllib.request from PyQt5.QtWidgets import QApplication from PyQt5.QtCore import Qt from vasl_templates.main_window import MainWindow from vasl_templates.webapp import app as webapp # --------------------------------------------------------------------- class LoggerProxy: """Redirect messages to Python logging.""" def __init__( self, logger, level ): self.logger = logger self.level = level self.closed = False def write( self, msg ): """Output a message.""" if isinstance(msg, bytes): msg = msg.decode( "utf-8" ) msg = msg.rstrip() if msg: self.logger.log( self.level, msg ) def flush( self ): """Flush the output stream.""" pass # --------------------------------------------------------------------- def main(): """Main entry point for the application.""" # connect stdout/stderr to Python logging # NOTE: Logging to the console causes crashes on Windows if we are frozen, so don't do that! sys.stdout = LoggerProxy( logging, logging.INFO ) sys.stderr = LoggerProxy( logging, logging.WARNING ) # start the webapp server port = webapp.config["FLASK_PORT_NO"] def webapp_thread(): """Run the webapp server.""" try: webapp.run( host="localhost", port=port, use_reloader=False ) except Exception as ex: logging.critical( "WEBAPP SERVER EXCEPTION: %s", ex ) logging.critical( traceback.format_exc() ) raise thread = threading.Thread( target=webapp_thread ) thread.start() # check if we should disable OpenGL # Using the QWebEngineView crashes on Windows 7 in a VM. It uses OpenGL, which is # apparently not well supported on Windows, and is dependent on the graphics card driver: # https://stackoverflow.com/a/50393872 # https://stackoverflow.com/questions/33090346/is-there-any-way-to-use-qtwebengine-without-opengl # Switching to software rendering (AA_UseSoftwareOpenGL) got things going :shrug: # Also see: https://doc.qt.io/qt-5/windows-requirements.html opengl_type = webapp.config.get( "OPENGL_TYPE" ) if opengl_type: logging.info( "Setting OpenGL: %s", opengl_type ) opengl_type = getattr( Qt, opengl_type ) QApplication.setAttribute( opengl_type ) # run the application app = QApplication( sys.argv ) url = "http://localhost:{}/main".format( port ) main_window = MainWindow( url ) main_window.show() ret_code = app.exec_() # shutdown the webapp server url = "http://localhost:{}/shutdown".format( port ) urllib.request.urlopen( url ).read() thread.join() return ret_code # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - if __name__ == "__main__": sys.exit( main() )