diff options
Diffstat (limited to 'manager.py')
| -rw-r--r-- | manager.py | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/manager.py b/manager.py new file mode 100644 index 0000000..a7d8ebd --- /dev/null +++ b/manager.py @@ -0,0 +1,97 @@ +import os +import sys +import signal +from threading import Thread +from OpenGL.GLUT import glutLeaveMainLoop + + +class PixMan: + _instance = None + _initialized = False + + @staticmethod + def handle_sigint(sig, frame): + try: + # Threads are started as daemons so will automatically shut down + glutLeaveMainLoop() + sys.exit(0) + except: + pass + finally: + print("Exiting...") + + def __new__(cls, *args, **kwargs): + if cls._instance is None: + cls._instance = super().__new__(cls) + else: + assert not args and not kwargs, f"Singleton {cls.__name__} cannot be called with arguments more than once" + return cls._instance + + def __init__(self): + pass + + @classmethod + def initialize(cls, configfile, app, socketio, host, port): + assert not cls._initialized, "Already initialized" + if cls._initialized: + return + cls._initialized = True + self = cls() + + self.app = app + self.socketio = socketio + self.immich_connector = None + self.texture_list = None + self.display = None + self.t_flask = None + self.t_idle_download = None + + signal.signal(signal.SIGINT, PixMan.handle_sigint) + + self.configfile = configfile + config = Config.load(self.configfile) if os.path.exists(self.configfile) else Config(file=self.configfile) + + self.init_web(host, port) + self.update_config(config) + + def init_web(self, host, port): + self.t_flask = Thread(target=self.app.run, kwargs={ "host": host, "port": port }) + self.t_flask.start() + + def init_window(self): + # Initialize immich connector + self.immich_connector = ImmichConnector() + if not self.immich_connector.validate_connection(): + self.immich_connector = None + return + + # Initialize texture list + change_callback = lambda d: self.socketio.emit("seek", d) + album_keys = [ "38617851-6b57-44f1-b5f7-82577606afc4" ] + self.texture_list = LazyCachingTextureList(album_keys, max_cache_items=self.config.max_cache_assets, change_callback=change_callback) + + # Begin downloading images + self.t_idle_download = Thread(target=self.immich_connector.idle, daemon=True) + self.t_idle_download.start() + + # Create display + self.display = PixDisplay(self.texture_list) + self.display.main({}) # TODO glut args + + def update_config(self, config): + #old_config = self.config + self.config = config + + # Initialize window if immich parameters are valid + if config.immich_url and config.immich_api_key and not self.display: + self.init_window() + + # If all goes well + config.save(self.configfile) + return True + + +from lazycachelist import LazyCachingTextureList +from window import PixDisplay +from immich import ImmichConnector +from settings import Config |
