mirror of
https://github.com/anufrievroman/waypaper.git
synced 2024-08-16 11:40:44 +03:00
fix: Allowed use of --random without --restore
This commit is contained in:
parent
f27c268a4f
commit
8786471754
@ -1,16 +1,16 @@
|
||||
"""Main module that runs the program and either runs GUI or just changer wallpaper"""
|
||||
|
||||
import time
|
||||
import argparse
|
||||
import sys
|
||||
import time
|
||||
|
||||
|
||||
from waypaper.config import Config
|
||||
from waypaper.aboutdata import AboutData
|
||||
from waypaper.app import App
|
||||
from waypaper.changer import change_wallpaper
|
||||
from waypaper.common import get_random_file
|
||||
from waypaper.aboutdata import AboutData
|
||||
from waypaper.options import FILL_OPTIONS, BACKEND_OPTIONS
|
||||
from waypaper.translations import English, German, French, Russian, Polish, Chinese
|
||||
from waypaper.config import Config
|
||||
from waypaper.options import BACKEND_OPTIONS
|
||||
from waypaper.translations import Chinese, English, French, German, Polish, Russian
|
||||
|
||||
about = AboutData()
|
||||
cf = Config()
|
||||
@ -29,8 +29,9 @@ else:
|
||||
txt = English()
|
||||
|
||||
|
||||
parser = argparse.ArgumentParser(prog = about.applicationName(), description = txt.msg_desc,
|
||||
epilog = txt.msg_info)
|
||||
parser = argparse.ArgumentParser(
|
||||
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("--restore", help=txt.msg_arg_rest, 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)
|
||||
|
||||
# Set the wallpaper and quit:
|
||||
if args.restore:
|
||||
for wallpaper, monitor in zip(cf.wallpaper, cf.monitors):
|
||||
if args.restore or args.random:
|
||||
for i, (wallpaper, monitor) in enumerate(zip(cf.wallpaper, cf.monitors)):
|
||||
w = wallpaper
|
||||
|
||||
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
|
||||
change_wallpaper(wallpaper, cf, monitor, txt)
|
||||
|
||||
cf.selected_wallpaper = w
|
||||
cf.save()
|
||||
|
||||
change_wallpaper(w, cf, monitor, txt)
|
||||
time.sleep(0.1)
|
||||
exit(0)
|
||||
sys.exit(0)
|
||||
|
||||
# Print the version and quit:
|
||||
if args.version:
|
||||
print(f"{about.applicationName()} v.{about.applicationVersion()}")
|
||||
exit(0)
|
||||
sys.exit(0)
|
||||
|
||||
# Start GUI:
|
||||
app = App(txt)
|
||||
|
@ -1,19 +1,28 @@
|
||||
"""Module to store acces application metadata"""
|
||||
|
||||
from importlib_metadata import metadata
|
||||
|
||||
class AboutData():
|
||||
|
||||
class AboutData:
|
||||
"""This class stores application about data in a central place"""
|
||||
|
||||
def __init__(self):
|
||||
self.__app_metadata = metadata(__package__)
|
||||
|
||||
def applicationName(self):
|
||||
return self.__app_metadata['Name']
|
||||
return self.__app_metadata["Name"]
|
||||
|
||||
def applicationSummary(self):
|
||||
return self.__app_metadata['Summary']
|
||||
return self.__app_metadata["Summary"]
|
||||
|
||||
def applicationLogo(self):
|
||||
return str(self.__app_metadata['Name'])+".svg"
|
||||
return str(self.__app_metadata["Name"]) + ".svg"
|
||||
|
||||
def applicationVersion(self):
|
||||
return self.__app_metadata['Version'] # pylint: disable=no-member
|
||||
return self.__app_metadata["Version"] # pylint: disable=no-member
|
||||
|
||||
def homePage(self):
|
||||
return self.__app_metadata['Home-page']
|
||||
return self.__app_metadata["Home-page"]
|
||||
|
||||
def Author(self):
|
||||
return self.__app_metadata['Author']
|
||||
return self.__app_metadata["Author"]
|
||||
|
@ -4,7 +4,8 @@ import os
|
||||
import random
|
||||
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):
|
||||
"""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:
|
||||
continue
|
||||
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:
|
||||
continue
|
||||
for filename in files:
|
||||
|
@ -1,18 +1,25 @@
|
||||
"""Module responsible for reading and saving the configuration file"""
|
||||
|
||||
import configparser
|
||||
import pathlib
|
||||
import os
|
||||
import pathlib
|
||||
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.options import FILL_OPTIONS, SORT_OPTIONS, SWWW_TRANSITION_TYPES, BACKEND_OPTIONS
|
||||
from waypaper.common import check_installed_backends
|
||||
from waypaper.options import (
|
||||
BACKEND_OPTIONS,
|
||||
FILL_OPTIONS,
|
||||
SORT_OPTIONS,
|
||||
SWWW_TRANSITION_TYPES,
|
||||
)
|
||||
|
||||
|
||||
class Config:
|
||||
"""User configuration loaded from the config.ini file"""
|
||||
|
||||
def __init__(self):
|
||||
self.image_folder = user_pictures_path()
|
||||
self.installed_backends = check_installed_backends()
|
||||
@ -20,7 +27,11 @@ class Config:
|
||||
self.selected_monitor = "All"
|
||||
self.fill_option = FILL_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.swww_transition_type = SWWW_TRANSITION_TYPES[0]
|
||||
self.swww_transition_step = 90
|
||||
@ -38,16 +49,15 @@ class Config:
|
||||
|
||||
# Create config and cache folders:
|
||||
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.check_validity()
|
||||
|
||||
|
||||
def read(self):
|
||||
"""Load data from the config.ini or use default if it does not exists"""
|
||||
config = configparser.ConfigParser()
|
||||
config.read(self.config_file, 'utf-8')
|
||||
config.read(self.config_file, "utf-8")
|
||||
|
||||
# Read parameters:
|
||||
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.backend = config.get("Settings", "backend", fallback=self.backend)
|
||||
self.color = config.get("Settings", "color", fallback=self.color)
|
||||
self.post_command = config.get("Settings", "post_command", fallback=self.post_command)
|
||||
self.swww_transition_type = config.get("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.post_command = config.get(
|
||||
"Settings", "post_command", fallback=self.post_command
|
||||
)
|
||||
self.swww_transition_type = config.get(
|
||||
"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.include_subfolders = config.getboolean("Settings", "subfolders", fallback=self.include_subfolders)
|
||||
self.monitors_str = config.get("Settings", "monitors", fallback=self.selected_monitor, raw=True)
|
||||
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.wallpaper_str = config.get("Settings", "wallpaper", fallback="", raw=True)
|
||||
|
||||
# Convert strings to lists:
|
||||
@ -74,7 +100,11 @@ class Config:
|
||||
def check_validity(self):
|
||||
"""Check if the config parameters are valid and correct them if needed"""
|
||||
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:
|
||||
self.sort_option = SORT_OPTIONS[0]
|
||||
if self.fill_option not in FILL_OPTIONS:
|
||||
@ -92,6 +122,7 @@ class Config:
|
||||
def save(self):
|
||||
"""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 self.selected_monitor == "All":
|
||||
self.monitors = [self.selected_monitor]
|
||||
@ -110,6 +141,7 @@ class Config:
|
||||
config.add_section("Settings")
|
||||
config.set("Settings", "language", self.lang)
|
||||
config.set("Settings", "folder", self.image_folder)
|
||||
print(f"HELLO: {self.wallpaper}")
|
||||
config.set("Settings", "wallpaper", ",".join(self.wallpaper))
|
||||
config.set("Settings", "backend", self.backend)
|
||||
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_step", str(self.swww_transition_step))
|
||||
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:
|
||||
config.write(configfile)
|
||||
|
||||
|
||||
def read_parameters_from_user_arguments(self, args):
|
||||
"""Read user arguments provided at the run. These values take priority over config.ini"""
|
||||
if args.backend:
|
||||
self.backend = args.backend
|
||||
if args.fill:
|
||||
self.fill_option = args.fill
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user