fix: Allowed use of --random without --restore

This commit is contained in:
Aaron Dunlap 2024-02-15 09:17:26 -06:00
parent f27c268a4f
commit 8786471754
4 changed files with 92 additions and 42 deletions

View File

@ -1,16 +1,16 @@
"""Main module that runs the program and either runs GUI or just changer wallpaper""" """Main module that runs the program and either runs GUI or just changer wallpaper"""
import time
import argparse import argparse
import sys
import time
from waypaper.aboutdata import AboutData
from waypaper.config import Config
from waypaper.app import App from waypaper.app import App
from waypaper.changer import change_wallpaper from waypaper.changer import change_wallpaper
from waypaper.common import get_random_file from waypaper.common import get_random_file
from waypaper.aboutdata import AboutData from waypaper.config import Config
from waypaper.options import FILL_OPTIONS, BACKEND_OPTIONS from waypaper.options import BACKEND_OPTIONS
from waypaper.translations import English, German, French, Russian, Polish, Chinese from waypaper.translations import Chinese, English, French, German, Polish, Russian
about = AboutData() about = AboutData()
cf = Config() cf = Config()
@ -29,8 +29,9 @@ else:
txt = English() txt = English()
parser = argparse.ArgumentParser(prog = about.applicationName(), description = txt.msg_desc, parser = argparse.ArgumentParser(
epilog = txt.msg_info) prog=about.applicationName(), description=txt.msg_desc, epilog=txt.msg_info
)
parser.add_argument("-v", "--version", help=txt.msg_arg_help, action="store_true") parser.add_argument("-v", "--version", help=txt.msg_arg_help, action="store_true")
parser.add_argument("--restore", help=txt.msg_arg_rest, action="store_true") parser.add_argument("--restore", help=txt.msg_arg_rest, action="store_true")
parser.add_argument("--random", help=txt.msg_arg_rand, action="store_true") parser.add_argument("--random", help=txt.msg_arg_rand, action="store_true")
@ -45,22 +46,27 @@ def run():
cf.read_parameters_from_user_arguments(args) cf.read_parameters_from_user_arguments(args)
# Set the wallpaper and quit: # Set the wallpaper and quit:
if args.restore: if args.restore or args.random:
for wallpaper, monitor in zip(cf.wallpaper, cf.monitors): for i, (wallpaper, monitor) in enumerate(zip(cf.wallpaper, cf.monitors)):
w = wallpaper
if args.random: if args.random:
wallpaper = get_random_file(cf.backend, cf.image_folder, cf.include_subfolders) w = get_random_file(cf.backend, cf.image_folder, cf.include_subfolders)
if wallpaper is None: if w is None:
continue continue
change_wallpaper(wallpaper, cf, monitor, txt)
cf.selected_wallpaper = w
cf.save()
change_wallpaper(w, cf, monitor, txt)
time.sleep(0.1) time.sleep(0.1)
exit(0) sys.exit(0)
# Print the version and quit: # Print the version and quit:
if args.version: if args.version:
print(f"{about.applicationName()} v.{about.applicationVersion()}") print(f"{about.applicationName()} v.{about.applicationVersion()}")
exit(0) sys.exit(0)
# Start GUI: # Start GUI:
app = App(txt) app = App(txt)

View File

@ -1,19 +1,28 @@
"""Module to store acces application metadata""" """Module to store acces application metadata"""
from importlib_metadata import metadata from importlib_metadata import metadata
class AboutData():
class AboutData:
"""This class stores application about data in a central place""" """This class stores application about data in a central place"""
def __init__(self): def __init__(self):
self.__app_metadata = metadata(__package__) self.__app_metadata = metadata(__package__)
def applicationName(self): def applicationName(self):
return self.__app_metadata['Name'] return self.__app_metadata["Name"]
def applicationSummary(self): def applicationSummary(self):
return self.__app_metadata['Summary'] return self.__app_metadata["Summary"]
def applicationLogo(self): def applicationLogo(self):
return str(self.__app_metadata['Name'])+".svg" return str(self.__app_metadata["Name"]) + ".svg"
def applicationVersion(self): def applicationVersion(self):
return self.__app_metadata['Version'] # pylint: disable=no-member return self.__app_metadata["Version"] # pylint: disable=no-member
def homePage(self): def homePage(self):
return self.__app_metadata['Home-page'] return self.__app_metadata["Home-page"]
def Author(self): def Author(self):
return self.__app_metadata['Author'] return self.__app_metadata["Author"]

View File

@ -4,7 +4,8 @@ import os
import random import random
import shutil import shutil
from waypaper.options import IMAGE_EXTENSIONS, BACKEND_OPTIONS from waypaper.options import BACKEND_OPTIONS, IMAGE_EXTENSIONS
def has_image_extension(file_path, backend): def has_image_extension(file_path, backend):
"""Check if the file has image extension""" """Check if the file has image extension"""
@ -20,7 +21,9 @@ def get_image_paths(backend, root_folder, include_subfolders=False, depth=None):
if not include_subfolders and root != root_folder: if not include_subfolders and root != root_folder:
continue continue
if depth is not None and root != root_folder: if depth is not None and root != root_folder:
current_depth = root.count(os.path.sep) - str(root_folder).count(os.path.sep) current_depth = root.count(os.path.sep) - str(root_folder).count(
os.path.sep
)
if current_depth > depth: if current_depth > depth:
continue continue
for filename in files: for filename in files:

View File

@ -1,18 +1,25 @@
"""Module responsible for reading and saving the configuration file""" """Module responsible for reading and saving the configuration file"""
import configparser import configparser
import pathlib
import os import os
import pathlib
from sys import exit from sys import exit
from platformdirs import user_config_path, user_pictures_path, user_cache_path
from platformdirs import user_cache_path, user_config_path, user_pictures_path
from waypaper.aboutdata import AboutData from waypaper.aboutdata import AboutData
from waypaper.options import FILL_OPTIONS, SORT_OPTIONS, SWWW_TRANSITION_TYPES, BACKEND_OPTIONS
from waypaper.common import check_installed_backends from waypaper.common import check_installed_backends
from waypaper.options import (
BACKEND_OPTIONS,
FILL_OPTIONS,
SORT_OPTIONS,
SWWW_TRANSITION_TYPES,
)
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 = user_pictures_path() self.image_folder = user_pictures_path()
self.installed_backends = check_installed_backends() self.installed_backends = check_installed_backends()
@ -20,7 +27,11 @@ class Config:
self.selected_monitor = "All" self.selected_monitor = "All"
self.fill_option = FILL_OPTIONS[0] self.fill_option = FILL_OPTIONS[0]
self.sort_option = SORT_OPTIONS[0] self.sort_option = SORT_OPTIONS[0]
self.backend = self.installed_backends[0] if self.installed_backends else BACKEND_OPTIONS[0] self.backend = (
self.installed_backends[0]
if self.installed_backends
else BACKEND_OPTIONS[0]
)
self.color = "#ffffff" self.color = "#ffffff"
self.swww_transition_type = SWWW_TRANSITION_TYPES[0] self.swww_transition_type = SWWW_TRANSITION_TYPES[0]
self.swww_transition_step = 90 self.swww_transition_step = 90
@ -38,16 +49,15 @@ class Config:
# Create config and cache folders: # Create config and cache folders:
self.config_dir.mkdir(parents=True, exist_ok=True) self.config_dir.mkdir(parents=True, exist_ok=True)
self.cache_dir.mkdir(parents=True,exist_ok=True) self.cache_dir.mkdir(parents=True, exist_ok=True)
self.read() self.read()
self.check_validity() self.check_validity()
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"""
config = configparser.ConfigParser() config = configparser.ConfigParser()
config.read(self.config_file, 'utf-8') config.read(self.config_file, "utf-8")
# Read parameters: # Read parameters:
self.image_folder = config.get("Settings", "folder", fallback=self.image_folder) self.image_folder = config.get("Settings", "folder", fallback=self.image_folder)
@ -55,14 +65,30 @@ class Config:
self.sort_option = config.get("Settings", "sort", fallback=self.sort_option) self.sort_option = config.get("Settings", "sort", fallback=self.sort_option)
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.post_command = config.get("Settings", "post_command", fallback=self.post_command) self.post_command = config.get(
self.swww_transition_type = config.get("Settings", "swww_transition_type", fallback=self.swww_transition_type) "Settings", "post_command", fallback=self.post_command
self.swww_transition_step = config.get("Settings", "swww_transition_step", fallback=self.swww_transition_step) )
self.swww_transition_angle = config.get("Settings", "swww_transition_angle", fallback=self.swww_transition_angle) self.swww_transition_type = config.get(
self.swww_transition_duration = config.get("Settings", "swww_transition_duration", fallback=self.swww_transition_duration) "Settings", "swww_transition_type", fallback=self.swww_transition_type
)
self.swww_transition_step = config.get(
"Settings", "swww_transition_step", fallback=self.swww_transition_step
)
self.swww_transition_angle = config.get(
"Settings", "swww_transition_angle", fallback=self.swww_transition_angle
)
self.swww_transition_duration = config.get(
"Settings",
"swww_transition_duration",
fallback=self.swww_transition_duration,
)
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(
self.monitors_str = config.get("Settings", "monitors", fallback=self.selected_monitor, raw=True) "Settings", "subfolders", fallback=self.include_subfolders
)
self.monitors_str = config.get(
"Settings", "monitors", fallback=self.selected_monitor, raw=True
)
self.wallpaper_str = config.get("Settings", "wallpaper", fallback="", raw=True) self.wallpaper_str = config.get("Settings", "wallpaper", fallback="", raw=True)
# Convert strings to lists: # Convert strings to lists:
@ -74,7 +100,11 @@ class Config:
def check_validity(self): def check_validity(self):
"""Check if the config parameters are valid and correct them if needed""" """Check if the config parameters are valid and correct them if needed"""
if self.backend not in BACKEND_OPTIONS: if self.backend not in BACKEND_OPTIONS:
self.backend = self.installed_backends[0] if self.installed_backends else BACKEND_OPTIONS[0] self.backend = (
self.installed_backends[0]
if self.installed_backends
else BACKEND_OPTIONS[0]
)
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]
if self.fill_option not in FILL_OPTIONS: if self.fill_option not in FILL_OPTIONS:
@ -92,6 +122,7 @@ class Config:
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"""
print(f"ANOTHER ONE: {self.wallpaper}")
# If only certain monitor was affected, change only its wallpaper: # If only certain monitor was affected, change only its wallpaper:
if self.selected_monitor == "All": if self.selected_monitor == "All":
self.monitors = [self.selected_monitor] self.monitors = [self.selected_monitor]
@ -110,6 +141,7 @@ class Config:
config.add_section("Settings") config.add_section("Settings")
config.set("Settings", "language", self.lang) config.set("Settings", "language", self.lang)
config.set("Settings", "folder", self.image_folder) config.set("Settings", "folder", self.image_folder)
print(f"HELLO: {self.wallpaper}")
config.set("Settings", "wallpaper", ",".join(self.wallpaper)) config.set("Settings", "wallpaper", ",".join(self.wallpaper))
config.set("Settings", "backend", self.backend) config.set("Settings", "backend", self.backend)
config.set("Settings", "monitors", ",".join(self.monitors)) config.set("Settings", "monitors", ",".join(self.monitors))
@ -121,15 +153,15 @@ class Config:
config.set("Settings", "swww_transition_type", str(self.swww_transition_type)) config.set("Settings", "swww_transition_type", str(self.swww_transition_type))
config.set("Settings", "swww_transition_step", str(self.swww_transition_step)) config.set("Settings", "swww_transition_step", str(self.swww_transition_step))
config.set("Settings", "swww_transition_angle", str(self.swww_transition_angle)) config.set("Settings", "swww_transition_angle", str(self.swww_transition_angle))
config.set("Settings", "swww_transition_duration", str(self.swww_transition_duration)) config.set(
"Settings", "swww_transition_duration", str(self.swww_transition_duration)
)
with open(self.config_file, "w") as configfile: with open(self.config_file, "w") as configfile:
config.write(configfile) config.write(configfile)
def read_parameters_from_user_arguments(self, args): def read_parameters_from_user_arguments(self, args):
"""Read user arguments provided at the run. These values take priority over config.ini""" """Read user arguments provided at the run. These values take priority over config.ini"""
if args.backend: if args.backend:
self.backend = args.backend self.backend = args.backend
if args.fill: if args.fill:
self.fill_option = args.fill self.fill_option = args.fill