mirror of
https://github.com/blw1138/Zordon.git
synced 2025-12-17 08:48:13 +00:00
* Initial commit of py2app code * Use environment variable RESOURCE_PATH when running as a bundle * Move config files to system config location
70 lines
2.0 KiB
Python
70 lines
2.0 KiB
Python
''' app/init.py '''
|
|
import logging
|
|
import os
|
|
import sys
|
|
import threading
|
|
from collections import deque
|
|
|
|
from PyQt6.QtCore import QObject, pyqtSignal
|
|
from PyQt6.QtWidgets import QApplication
|
|
|
|
from .render_queue import RenderQueue
|
|
from .ui.main_window import MainWindow
|
|
|
|
from src.api.api_server import start_server
|
|
from src.utilities.config import Config
|
|
from src.utilities.misc_helper import system_safe_path
|
|
|
|
|
|
def run() -> int:
|
|
"""
|
|
Initializes the application and runs it.
|
|
|
|
Returns:
|
|
int: The exit status code.
|
|
"""
|
|
|
|
# Load Config YAML
|
|
Config.setup_config_dir()
|
|
Config.load_config(system_safe_path(os.path.join(Config.config_dir(), 'config.yaml')))
|
|
logging.basicConfig(format='%(asctime)s: %(levelname)s: %(module)s: %(message)s', datefmt='%d-%b-%y %H:%M:%S',
|
|
level=Config.server_log_level.upper())
|
|
|
|
app: QApplication = QApplication(sys.argv)
|
|
|
|
# Start server in background
|
|
background_server = threading.Thread(target=start_server)
|
|
background_server.daemon = True
|
|
background_server.start()
|
|
|
|
# Setup logging for console ui
|
|
buffer_handler = BufferingHandler()
|
|
buffer_handler.setFormatter(logging.getLogger().handlers[0].formatter)
|
|
logger = logging.getLogger()
|
|
logger.addHandler(buffer_handler)
|
|
|
|
window: MainWindow = MainWindow()
|
|
window.buffer_handler = buffer_handler
|
|
window.show()
|
|
|
|
return_code = app.exec()
|
|
RenderQueue.prepare_for_shutdown()
|
|
return sys.exit(return_code)
|
|
|
|
|
|
class BufferingHandler(logging.Handler, QObject):
|
|
new_record = pyqtSignal(str)
|
|
|
|
def __init__(self, capacity=100):
|
|
logging.Handler.__init__(self)
|
|
QObject.__init__(self)
|
|
self.buffer = deque(maxlen=capacity) # Define a buffer with a fixed capacity
|
|
|
|
def emit(self, record):
|
|
msg = self.format(record)
|
|
self.buffer.append(msg) # Add message to the buffer
|
|
self.new_record.emit(msg) # Emit signal
|
|
|
|
def get_buffer(self):
|
|
return list(self.buffer) # Return a copy of the buffer
|