mirror of
https://github.com/anufrievroman/waypaper.git
synced 2024-09-11 16:26:14 +03:00
Rewrite Config class to use AboutData and platformdirs
Instead of hardcoding paths use platformdirs for configuration-, pictures- and cache-directory. Also instead of creating a global instance, initialize the class in the constructor, including the creation of all directories we use. Also it's not required to create the config file if it doesn't exist, configparser will simply not an emtpy configuration object. Change default value of wallpaper to be a string, the config parser later uses a string, if the value is a list as it was said it doesn't work a list doesn't have a .split() method. Signed-off-by: Björn Bidar <bjorn.bidar@thaodan.de>
This commit is contained in:
parent
b539759298
commit
8c960e0093
@ -47,6 +47,7 @@ The `waypaper` package is available thanks to Basil Keeler.
|
|||||||
- `swww` or `swaybg` or `feh` or `wallutils`
|
- `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.)
|
- gobject python library (it might be called `python-gobject` or `python3-gi` or `python3-gobject` in your package manager.)
|
||||||
- `python-importlib_metadata`
|
- `python-importlib_metadata`
|
||||||
|
- `python-platformdirs`
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
|
@ -3,16 +3,16 @@
|
|||||||
import configparser
|
import configparser
|
||||||
import pathlib
|
import pathlib
|
||||||
import os
|
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
|
from waypaper.options import FILL_OPTIONS, SORT_OPTIONS
|
||||||
|
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
"""User configuration loaded from the config.ini file"""
|
"""User configuration loaded from the config.ini file"""
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.image_folder = str(pathlib.Path.home())
|
self.image_folder = user_pictures_path()
|
||||||
if os.path.exists(str(pathlib.Path.home()) + "/Pictures"):
|
|
||||||
self.image_folder = str(pathlib.Path.home()) + "/Pictures"
|
|
||||||
self.selected_wallpaper = None
|
self.selected_wallpaper = None
|
||||||
self.selected_monitor = "All"
|
self.selected_monitor = "All"
|
||||||
self.fill_option = "fill"
|
self.fill_option = "fill"
|
||||||
@ -21,11 +21,21 @@ class Config:
|
|||||||
self.color = "#ffffff"
|
self.color = "#ffffff"
|
||||||
self.lang = "en"
|
self.lang = "en"
|
||||||
self.monitors = [self.selected_monitor]
|
self.monitors = [self.selected_monitor]
|
||||||
self.wallpaper = []
|
self.wallpaper = str()
|
||||||
self.include_subfolders = False
|
self.include_subfolders = False
|
||||||
self.config_folder = str(pathlib.Path.home()) + "/.config/waypaper"
|
self.aboutData = AboutData()
|
||||||
self.config_file = self.config_folder + "/config.ini"
|
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):
|
def create(self):
|
||||||
"""Create a default config.ini file if it does not exist"""
|
"""Create a default config.ini file if it does not exist"""
|
||||||
@ -41,38 +51,33 @@ class Config:
|
|||||||
"wallpaper": str(self.selected_wallpaper),
|
"wallpaper": str(self.selected_wallpaper),
|
||||||
"monitors": str(self.selected_monitor),
|
"monitors": str(self.selected_monitor),
|
||||||
}
|
}
|
||||||
with open(cf.config_file, "w") as configfile:
|
with open(self.config_file, "w") as configfile:
|
||||||
config.write(configfile)
|
config.write(configfile)
|
||||||
|
|
||||||
|
|
||||||
def read(self):
|
def read(self):
|
||||||
"""Load data from the config.ini or use default if it does not exists"""
|
"""Load data from the config.ini or use default if it does not exists"""
|
||||||
try:
|
config = configparser.ConfigParser()
|
||||||
config = configparser.ConfigParser()
|
config.read(self.config_file, 'utf-8')
|
||||||
config.read(self.config_file, 'utf-8')
|
self.image_folder = config.get("Settings", "folder", fallback=self.image_folder)
|
||||||
self.image_folder = config.get("Settings", "folder", fallback=self.image_folder)
|
self.fill_option = config.get("Settings", "fill", fallback=self.fill_option)
|
||||||
self.fill_option = config.get("Settings", "fill", fallback=self.fill_option)
|
if self.fill_option not in FILL_OPTIONS:
|
||||||
if self.fill_option not in FILL_OPTIONS:
|
self.sort_option = FILL_OPTIONS[0]
|
||||||
self.sort_option = FILL_OPTIONS[0]
|
self.sort_option = config.get("Settings", "sort", fallback=self.sort_option)
|
||||||
self.sort_option = config.get("Settings", "sort", fallback=self.sort_option)
|
if self.sort_option not in SORT_OPTIONS:
|
||||||
if self.sort_option not in SORT_OPTIONS:
|
self.sort_option = SORT_OPTIONS[0]
|
||||||
self.sort_option = SORT_OPTIONS[0]
|
self.backend = config.get("Settings", "backend", fallback=self.backend)
|
||||||
self.backend = config.get("Settings", "backend", fallback=self.backend)
|
self.color = config.get("Settings", "color", fallback=self.color)
|
||||||
self.color = config.get("Settings", "color", fallback=self.color)
|
self.lang = config.get("Settings", "language", fallback=self.lang)
|
||||||
self.lang = config.get("Settings", "language", fallback=self.lang)
|
self.include_subfolders = config.getboolean("Settings", "subfolders", fallback=self.include_subfolders)
|
||||||
self.include_subfolders = config.getboolean("Settings", "subfolders", fallback=self.include_subfolders)
|
|
||||||
|
|
||||||
self.monitors_str = config.get("Settings", "monitors", fallback=self.selected_monitor, raw=True)
|
self.monitors_str = config.get("Settings", "monitors", fallback=self.selected_monitor, raw=True)
|
||||||
if self.monitors_str is not None:
|
if self.monitors_str is not None:
|
||||||
self.monitors = [str(monitor) for monitor in self.monitors_str.split(",")]
|
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.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):
|
def save(self):
|
||||||
"""Update the parameters and save them to the configuration file"""
|
"""Update the parameters and save them to the configuration file"""
|
||||||
@ -91,16 +96,18 @@ class Config:
|
|||||||
# Write configuration to the file:
|
# Write configuration to the file:
|
||||||
config = configparser.ConfigParser()
|
config = configparser.ConfigParser()
|
||||||
config.read(self.config_file)
|
config.read(self.config_file)
|
||||||
config.set("Settings", "folder", cf.image_folder)
|
if not config.has_section("Settings"):
|
||||||
config.set("Settings", "fill", cf.fill_option)
|
config.add_section("Settings")
|
||||||
config.set("Settings", "sort", cf.sort_option)
|
config.set("Settings", "folder", self.image_folder)
|
||||||
config.set("Settings", "backend", cf.backend)
|
config.set("Settings", "fill", self.fill_option)
|
||||||
config.set("Settings", "color", cf.color)
|
config.set("Settings", "sort", self.sort_option)
|
||||||
config.set("Settings", "language", cf.lang)
|
config.set("Settings", "backend", self.backend)
|
||||||
config.set("Settings", "subfolders", str(cf.include_subfolders))
|
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", "wallpaper", ",".join(self.wallpaper))
|
||||||
config.set("Settings", "monitors", ",".join(self.monitors))
|
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)
|
config.write(configfile)
|
||||||
|
|
||||||
|
|
||||||
@ -111,20 +118,3 @@ class Config:
|
|||||||
if args.fill:
|
if args.fill:
|
||||||
self.fill_option = 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()
|
|
||||||
|
Loading…
Reference in New Issue
Block a user