Implement various cleanup
- Don't import wallman_systray to see if it exists, use importlib.util.find_spec() to look for deps instead - Improve handling if no config file exists.
This commit is contained in:
		
							parent
							
								
									6c72bdc31b
								
							
						
					
					
						commit
						105c59d122
					
				
					 1 changed files with 19 additions and 13 deletions
				
			
		| 
						 | 
					@ -26,6 +26,8 @@ class _ConfigLib:
 | 
				
			||||||
        self.config_total_changing_times: int = len(self.config_changing_times)
 | 
					        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_log_level: str = self.config_general.get("loglevel", "INFO").upper()
 | 
				
			||||||
        self.config_behavior: str = self._set_behavior()
 | 
					        self.config_behavior: str = self._set_behavior()
 | 
				
			||||||
 | 
					        # Setup logging
 | 
				
			||||||
 | 
					        self._set_log_level()
 | 
				
			||||||
        # HACK: Add a function to handle these try/except blocks cleanlier.
 | 
					        # HACK: Add a function to handle these try/except blocks cleanlier.
 | 
				
			||||||
        try:
 | 
					        try:
 | 
				
			||||||
            self.config_notify: bool = self.config_general["notify"]
 | 
					            self.config_notify: bool = self.config_general["notify"]
 | 
				
			||||||
| 
						 | 
					@ -38,24 +40,29 @@ class _ConfigLib:
 | 
				
			||||||
            self.config_systray: bool = True
 | 
					            self.config_systray: bool = True
 | 
				
			||||||
            logger.warning("'systray' is not set in the dictionary general in the config file, defaulting to '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.
 | 
					        # Setup systray.
 | 
				
			||||||
        if self.config_systray:
 | 
					        if self.config_systray:
 | 
				
			||||||
            self._initialize_systray()
 | 
					            self._verify_systray_deps()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # Read config. TODO: Add error handling for the config not found case.
 | 
					    # Read config. TODO: Add error handling for the config not found case.
 | 
				
			||||||
    def _initialize_config(self) -> ConfigFile:
 | 
					    def _initialize_config(self) -> ConfigFile:
 | 
				
			||||||
        chdir(str(getenv("HOME")) + "/.config/wallman/")
 | 
					        chdir(str(getenv("HOME")) + "/.config/wallman/")
 | 
				
			||||||
 | 
					        try:
 | 
				
			||||||
            with open("wallman.toml", "rb") as config_file:
 | 
					            with open("wallman.toml", "rb") as config_file:
 | 
				
			||||||
                data: ConfigFile = tomllib.load(config_file) #pyright:ignore
 | 
					                data: ConfigFile = tomllib.load(config_file) #pyright:ignore
 | 
				
			||||||
                return data
 | 
					                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
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # HACK on this to avoid double importing of wallman_systray due to variable scope. Idea: Global variable or Variable that is inherited?
 | 
					    def _verify_systray_deps(self):
 | 
				
			||||||
    def _initialize_systray(self):
 | 
					        from importlib import util
 | 
				
			||||||
        try:
 | 
					        if util.find_spec("pystray") is None or util.find_spec("pillow") is None:
 | 
				
			||||||
            import wallman_systray as _
 | 
					            logger.error("systray is enabled, but dependencies for the systray couldn't be found. Are pystray and pillow installed?")
 | 
				
			||||||
        except (ImportError, FileNotFoundError):
 | 
					            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
 | 
					            self.config_systray = False
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def _set_log_level(self):
 | 
					    def _set_log_level(self):
 | 
				
			||||||
| 
						 | 
					@ -313,9 +320,9 @@ class WallpaperLogic(_ConfigLib):
 | 
				
			||||||
            scheduler.start()
 | 
					            scheduler.start()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if self.config_systray:
 | 
					        if self.config_systray:
 | 
				
			||||||
            # NOTE: The wallman_systray impomrt should be handled differently. See the note in Config_Validity.
 | 
					 | 
				
			||||||
            import wallman_systray as systray
 | 
					            import wallman_systray as systray
 | 
				
			||||||
            from functools import partial
 | 
					            from functools import partial
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            scheduler: BackgroundScheduler = _schedule_background_wallpapers()
 | 
					            scheduler: BackgroundScheduler = _schedule_background_wallpapers()
 | 
				
			||||||
            menu: systray.Menu = systray.Menu (
 | 
					            menu: systray.Menu = systray.Menu (
 | 
				
			||||||
                systray.item("Re-Set Wallpaper", partial(systray.set_wallpaper_again, wallpaper_setter=self.set_wallpaper_by_time)),
 | 
					                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 = systray.Icon("wallman_icon", systray.icon_image, "My Tray Icon", menu)
 | 
				
			||||||
            icon.run()
 | 
					            icon.run()
 | 
				
			||||||
        else:
 | 
					        else:
 | 
				
			||||||
 | 
					 | 
				
			||||||
            _schedule_blocking_wallpapers()
 | 
					            _schedule_blocking_wallpapers()
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue