diff --git a/src/wallman_lib.py b/src/wallman_lib.py index 9b4e30b..66101a1 100644 --- a/src/wallman_lib.py +++ b/src/wallman_lib.py @@ -26,6 +26,8 @@ class _ConfigLib: self.config_total_changing_times: int = len(self.config_changing_times) self.config_log_level: str = self.config_general.get("loglevel", "INFO").upper() self.config_behavior: str = self._set_behavior() + # Setup logging + self._set_log_level() # HACK: Add a function to handle these try/except blocks cleanlier. try: self.config_notify: bool = self.config_general["notify"] @@ -38,24 +40,29 @@ class _ConfigLib: self.config_systray: bool = True logger.warning("'systray' is not set in the dictionary general in the config file, defaulting to 'true'.") - # Setup logging - self._set_log_level() # Setup systray. if self.config_systray: - self._initialize_systray() + self._verify_systray_deps() # Read config. TODO: Add error handling for the config not found case. def _initialize_config(self) -> ConfigFile: chdir(str(getenv("HOME")) + "/.config/wallman/") - with open("wallman.toml", "rb") as config_file: - data: ConfigFile = tomllib.load(config_file) #pyright:ignore - return data - - # HACK on this to avoid double importing of wallman_systray due to variable scope. Idea: Global variable or Variable that is inherited? - def _initialize_systray(self): try: - import wallman_systray as _ - except (ImportError, FileNotFoundError): + with open("wallman.toml", "rb") as config_file: + data: ConfigFile = tomllib.load(config_file) #pyright:ignore + return data + except FileNotFoundError: + raise FileNotFoundError("No config file could be found in ~/.config/wallman/wallman.toml") + except tomllib.TOMLDecodeError as e: + print("ERROR: Config could not be parsed: Invalid TOML Syntax") + raise e + + def _verify_systray_deps(self): + from importlib import util + if util.find_spec("pystray") is None or util.find_spec("pillow") is None: + logger.error("systray is enabled, but dependencies for the systray couldn't be found. Are pystray and pillow installed?") + logger.info("Setting self.config_systray to false.") + print("ERROR: systray is enabled, but dependencies for the systray couldn't be found. Are pystray and pillow installed?") self.config_systray = False def _set_log_level(self): @@ -313,9 +320,9 @@ class WallpaperLogic(_ConfigLib): scheduler.start() if self.config_systray: - # NOTE: The wallman_systray impomrt should be handled differently. See the note in Config_Validity. import wallman_systray as systray from functools import partial + scheduler: BackgroundScheduler = _schedule_background_wallpapers() menu: systray.Menu = systray.Menu ( systray.item("Re-Set Wallpaper", partial(systray.set_wallpaper_again, wallpaper_setter=self.set_wallpaper_by_time)), @@ -325,5 +332,4 @@ class WallpaperLogic(_ConfigLib): icon = systray.Icon("wallman_icon", systray.icon_image, "My Tray Icon", menu) icon.run() else: - _schedule_blocking_wallpapers()