Added new Feature: Wallpaper setting behavior can now be read from the config file rather than being hardcoded

This commit is contained in:
Emma Nora Theuer 2024-12-28 00:16:44 +01:00
parent 2751417ccf
commit bd151f23be

View file

@ -1,5 +1,6 @@
from sys import exit from sys import exit
from os import chdir, getenv, system from os import chdir, getenv, system
from typing import List
import logging import logging
import tomllib import tomllib
from datetime import datetime, time from datetime import datetime, time
@ -24,6 +25,7 @@ class _ConfigLib:
self.config_wallpapers_per_set: int = self.config_general["wallpapers_per_set"] self.config_wallpapers_per_set: int = self.config_general["wallpapers_per_set"]
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()
# 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"]
@ -64,9 +66,44 @@ class _ConfigLib:
logger.setLevel(numeric_level) logger.setLevel(numeric_level)
logging.basicConfig(filename="wallman.log", encoding="utf-8", level=numeric_level) logging.basicConfig(filename="wallman.log", encoding="utf-8", level=numeric_level)
# TODO: Make this all just work inside the try/except block, there is no need for get()
# TODO: Adjust these variable names
def _set_behavior(self) -> str:
try:
self.config_general.get("behavior")
except KeyError:
logger.info("There is no wallpaper behavior specified in general, defaulting to fill...")
print("There is no wallpaper behavior specified in general, defaulting to fill...")
human_behaviors: List[str] = ["pure", "tile", "center", "fill", "max", "scale"]
machine_behaviors: List[str] = ["--bg", "--bg-tile", "--bg-center", "--bg-fill", "--bg-max", "--bg-scale"]
behavior: str = self.config_general.get("behavior", "--bg-fill").lower()
if behavior not in human_behaviors or behavior not in machine_behaviors:
logging.error(f"The value provided for behaviors, {behavior}, is not valid. Defaulting to fill...")
print(f"ERROR: The value provided for behaviors, {behavior}, is not valid. Defaulting to --bg-fill...")
if behavior not in machine_behaviors:
match behavior:
case "pure":
behavior = "--bg"
case "tile":
behavior = "--bg-tile"
case "center":
behavior = "--bg-center"
case "max":
behavior = "--bg-max"
case "scale":
behavior = "--bg-scale"
case _:
behavior = "--bg-fill"
logger.info(f"The wallpaper behavior '{behavior}' has been set.")
return behavior
def _set_fallback_wallpaper(self): def _set_fallback_wallpaper(self):
if self.config_general["fallback_wallpaper"]: if self.config_general["fallback_wallpaper"]:
system(f"feh --bg-fill --no-fehbg {self.config_general['fallback_wallpaper']}") system(f"feh {self.config_behavior} --no-fehbg {self.config_general['fallback_wallpaper']}")
logger.info("The fallback Wallpaper has been set.") logger.info("The fallback Wallpaper has been set.")
else: else:
logger.critical("An Error occured and no fallback wallpaper was provided, exiting...") logger.critical("An Error occured and no fallback wallpaper was provided, exiting...")
@ -232,7 +269,7 @@ class WallpaperLogic(_ConfigLib):
# HACK on this to see if this logic can be simplified. It's very ugly to check it that way. # HACK on this to see if this logic can be simplified. It's very ugly to check it that way.
# Check if the current time is between a given and the following changing time and if so, set that wallpaper. If not, keep trying. # Check if the current time is between a given and the following changing time and if so, set that wallpaper. If not, keep trying.
if self._time_in_range(time(int(clean_time[0]), int(clean_time[1]), int(clean_time[2])), time(int(clean_time_two[0]), int(clean_time_two[1]), int(clean_time_two[2])), datetime.now().time()): if self._time_in_range(time(int(clean_time[0]), int(clean_time[1]), int(clean_time[2])), time(int(clean_time_two[0]), int(clean_time_two[1]), int(clean_time_two[2])), datetime.now().time()):
exitcode = system(f"feh --bg-tile --no-fehbg --quiet {self.wallpaper_list[time_range]}") exitcode = system(f"feh {self.config_behavior} --no-fehbg --quiet {self.wallpaper_list[time_range]}")
has_wallpaper_been_set = self._check_system_exitcode(exitcode) has_wallpaper_been_set = self._check_system_exitcode(exitcode)
# TODO: Add this check to _notify_user. # TODO: Add this check to _notify_user.
if self.config_notify: if self.config_notify:
@ -241,7 +278,7 @@ class WallpaperLogic(_ConfigLib):
else: else:
continue continue
exitcode = system(f"feh --bg-tile --no-fehbg {self.wallpaper_list[-1]}") exitcode = system(f"feh {self.config_behavior} --no-fehbg {self.wallpaper_list[-1]}")
has_wallpaper_been_set = self._check_system_exitcode(exitcode) has_wallpaper_been_set = self._check_system_exitcode(exitcode)
if self.config_notify: if self.config_notify:
self._notify_user() self._notify_user()