diff --git a/README.md b/README.md index 3ee4c3b..31527e1 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,7 @@ The `waypaper` package is available thanks to Basil Keeler. - `swww` or `swaybg` or `feh` or `wallutils` - gobject python library (it might be called `python-gobject` or `python3-gi` or `python3-gobject` in your package manager.) - `python-importlib_metadata` +- `python-platformdirs` ## Usage diff --git a/waypaper/config.py b/waypaper/config.py index 69cef89..fcb8902 100644 --- a/waypaper/config.py +++ b/waypaper/config.py @@ -3,16 +3,16 @@ import configparser import pathlib import os +from sys import exit +from platformdirs import user_config_path, user_pictures_path, user_cache_path +from waypaper.aboutdata import AboutData from waypaper.options import FILL_OPTIONS, SORT_OPTIONS - class Config: """User configuration loaded from the config.ini file""" def __init__(self): - self.image_folder = str(pathlib.Path.home()) - if os.path.exists(str(pathlib.Path.home()) + "/Pictures"): - self.image_folder = str(pathlib.Path.home()) + "/Pictures" + self.image_folder = user_pictures_path() self.selected_wallpaper = None self.selected_monitor = "All" self.fill_option = "fill" @@ -21,11 +21,21 @@ class Config: self.color = "#ffffff" self.lang = "en" self.monitors = [self.selected_monitor] - self.wallpaper = [] + self.wallpaper = str() self.include_subfolders = False - self.config_folder = str(pathlib.Path.home()) + "/.config/waypaper" - self.config_file = self.config_folder + "/config.ini" + self.aboutData = AboutData() + self.cachePath = user_cache_path(self.aboutData.applicationName()) + self.configPath = user_config_path(self.aboutData.applicationName()) + self.config_file = self.configPath / "config.ini" + # Create all directories we use here + # Create config folder: + self.configPath.mkdir(parents=True, exist_ok=True) + + # Create cache folder: + self.cachePath.mkdir(parents=True,exist_ok=True) + + self.read() def create(self): """Create a default config.ini file if it does not exist""" @@ -41,38 +51,33 @@ class Config: "wallpaper": str(self.selected_wallpaper), "monitors": str(self.selected_monitor), } - with open(cf.config_file, "w") as configfile: + with open(self.config_file, "w") as configfile: config.write(configfile) def read(self): """Load data from the config.ini or use default if it does not exists""" - try: - config = configparser.ConfigParser() - config.read(self.config_file, 'utf-8') - self.image_folder = config.get("Settings", "folder", fallback=self.image_folder) - self.fill_option = config.get("Settings", "fill", fallback=self.fill_option) - if self.fill_option not in FILL_OPTIONS: - self.sort_option = FILL_OPTIONS[0] - self.sort_option = config.get("Settings", "sort", fallback=self.sort_option) - if self.sort_option not in SORT_OPTIONS: - self.sort_option = SORT_OPTIONS[0] - self.backend = config.get("Settings", "backend", fallback=self.backend) - self.color = config.get("Settings", "color", fallback=self.color) - self.lang = config.get("Settings", "language", fallback=self.lang) - self.include_subfolders = config.getboolean("Settings", "subfolders", fallback=self.include_subfolders) + config = configparser.ConfigParser() + config.read(self.config_file, 'utf-8') + self.image_folder = config.get("Settings", "folder", fallback=self.image_folder) + self.fill_option = config.get("Settings", "fill", fallback=self.fill_option) + if self.fill_option not in FILL_OPTIONS: + self.sort_option = FILL_OPTIONS[0] + self.sort_option = config.get("Settings", "sort", fallback=self.sort_option) + if self.sort_option not in SORT_OPTIONS: + self.sort_option = SORT_OPTIONS[0] + self.backend = config.get("Settings", "backend", fallback=self.backend) + self.color = config.get("Settings", "color", fallback=self.color) + self.lang = config.get("Settings", "language", fallback=self.lang) + self.include_subfolders = config.getboolean("Settings", "subfolders", fallback=self.include_subfolders) - self.monitors_str = config.get("Settings", "monitors", fallback=self.selected_monitor, raw=True) - if self.monitors_str is not None: - self.monitors = [str(monitor) for monitor in self.monitors_str.split(",")] - - self.wallpaper_str = config.get("Settings", "wallpaper", fallback=self.wallpaper, raw=True) - if self.wallpaper_str is not None: - self.wallpaper = [str(paper) for paper in self.wallpaper_str.split(",")] - except Exception as e: - print(e) - exit() + self.monitors_str = config.get("Settings", "monitors", fallback=self.selected_monitor, raw=True) + if self.monitors_str is not None: + self.monitors = [str(monitor) for monitor in self.monitors_str.split(",")] + self.wallpaper_str = config.get("Settings", "wallpaper", fallback=self.wallpaper, raw=True) + if self.wallpaper_str is not None: + self.wallpaper = [str(paper) for paper in self.wallpaper_str.split(",")] def save(self): """Update the parameters and save them to the configuration file""" @@ -91,16 +96,18 @@ class Config: # Write configuration to the file: config = configparser.ConfigParser() config.read(self.config_file) - config.set("Settings", "folder", cf.image_folder) - config.set("Settings", "fill", cf.fill_option) - config.set("Settings", "sort", cf.sort_option) - config.set("Settings", "backend", cf.backend) - config.set("Settings", "color", cf.color) - config.set("Settings", "language", cf.lang) - config.set("Settings", "subfolders", str(cf.include_subfolders)) + if not config.has_section("Settings"): + config.add_section("Settings") + config.set("Settings", "folder", self.image_folder) + config.set("Settings", "fill", self.fill_option) + config.set("Settings", "sort", self.sort_option) + config.set("Settings", "backend", self.backend) + config.set("Settings", "color", self.color) + config.set("Settings", "language", self.lang) + config.set("Settings", "subfolders", str(self.include_subfolders)) config.set("Settings", "wallpaper", ",".join(self.wallpaper)) config.set("Settings", "monitors", ",".join(self.monitors)) - with open(cf.config_file, "w") as configfile: + with open(self.config_file, "w") as configfile: config.write(configfile) @@ -111,20 +118,3 @@ class Config: if args.fill: self.fill_option = args.fill - -cf = Config() - -# Create config folder: -if not os.path.exists(cf.config_folder): - os.makedirs(cf.config_folder) - -# Create cache folder: -if not os.path.exists(f"{cf.config_folder}/.cache"): - os.makedirs(f"{cf.config_folder}/.cache") - -# Create config file: -if not os.path.exists(cf.config_file): - cf.create() - -# Read config file: -cf.read()