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`
|
||||
- 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
|
||||
|
||||
|
@ -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,13 +51,12 @@ 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)
|
||||
@ -69,10 +78,6 @@ class Config:
|
||||
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()
|
||||
|
||||
|
||||
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()
|
||||
|
Loading…
Reference in New Issue
Block a user