Skip to content

Commit f7235f5

Browse files
committed
Improvements of initial backend handling
This improved how the installed backnends are checked and makes sure that only installed backends are reconded into config. This addresses the issue #23 and generally improved the code.
1 parent 8518e1e commit f7235f5

File tree

7 files changed

+54
-59
lines changed

7 files changed

+54
-59
lines changed

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
]
1919
},
2020
install_requires=["PyGObject", "importlib_metadata", "platformdirs", "Pillow"],
21-
version='2.0.3',
21+
version='2.0.4',
2222
python_requires='>3.9',
2323
classifiers=[
2424
"Development Status :: 4 - Beta",

waypaper/__main__.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from waypaper.config import Config
88
from waypaper.app import App
99
from waypaper.changer import change_wallpaper
10-
from waypaper.common import get_random_file, check_missing_backends
10+
from waypaper.common import get_random_file
1111
from waypaper.aboutdata import AboutData
1212
from waypaper.options import FILL_OPTIONS, BACKEND_OPTIONS
1313
from waypaper.translations import English, German, French, Russian, Polish, Chinese
@@ -39,12 +39,10 @@
3939
args = parser.parse_args()
4040

4141

42-
4342
def run():
4443
"""Read user arguments and either run GUI app or just reset the wallpaper"""
4544

4645
cf.read_parameters_from_user_arguments(args)
47-
missing_backends = check_missing_backends()
4846

4947
# Set the wallpaper and quit:
5048
if args.restore:
@@ -55,7 +53,7 @@ def run():
5553

5654
if wallpaper is None:
5755
continue
58-
change_wallpaper(wallpaper, cf, monitor, txt, missing_backends)
56+
change_wallpaper(wallpaper, cf, monitor, txt)
5957
time.sleep(0.1)
6058
exit(0)
6159

waypaper/app.py

+22-30
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
import threading
55
import os
66
import gi
7+
import shutil
78
from pathlib import Path
8-
from platformdirs import user_cache_path
99
from PIL import Image
1010

1111
from waypaper.aboutdata import AboutData
1212
from waypaper.changer import change_wallpaper
1313
from waypaper.config import Config
14-
from waypaper.common import get_image_paths, get_random_file, check_missing_backends
14+
from waypaper.common import get_image_paths, get_random_file
1515
from waypaper.options import FILL_OPTIONS, BACKEND_OPTIONS, SORT_OPTIONS, SORT_DISPLAYS
1616

1717
gi.require_version("Gtk", "3.0")
@@ -27,7 +27,7 @@ def read_webp_image(image_path):
2727
return pixbuf
2828

2929

30-
def cache_image(image_path, cachedir):
30+
def cache_image(image_path, cache_dir):
3131
"""Resize and cache images using gtk library"""
3232
ext = os.path.splitext(image_path)[1].lower()
3333
if ext == ".webp":
@@ -38,7 +38,7 @@ def cache_image(image_path, cachedir):
3838
scaled_width = 240
3939
scaled_height = int(scaled_width / aspect_ratio)
4040
scaled_pixbuf = pixbuf.scale_simple(scaled_width, scaled_height, GdkPixbuf.InterpType.BILINEAR)
41-
output_file = cachedir / Path(os.path.basename(image_path))
41+
output_file = cache_dir / Path(os.path.basename(image_path))
4242
scaled_pixbuf.savev(str(output_file), "jpeg", [], [])
4343

4444

@@ -47,15 +47,14 @@ class App(Gtk.Window):
4747

4848
def __init__(self, txt):
4949
super().__init__(title="Waypaper")
50+
self.cf = Config()
51+
self.about = AboutData()
5052
self.txt = txt
5153
self.check_backends()
5254
self.set_default_size(780, 600)
5355
self.connect("delete-event", Gtk.main_quit)
5456
self.selected_index = 0
5557
self.highlighted_image_row = 0
56-
self.aboutData = AboutData()
57-
self.cachePath = user_cache_path(self.aboutData.applicationName())
58-
self.cf = Config()
5958
self.init_ui()
6059

6160
# Start the image processing in a separate thread:
@@ -96,16 +95,13 @@ def init_ui(self):
9695

9796
# Create a backend dropdown menu:
9897
self.backend_option_combo = Gtk.ComboBoxText()
99-
for backend, is_missing in zip(BACKEND_OPTIONS, self.missing_backends):
100-
if not is_missing:
101-
self.backend_option_combo.append_text(backend)
98+
for backend in self.cf.installed_backends:
99+
self.backend_option_combo.append_text(backend)
102100

103101
# Set as active line the backend from config, if it is installed:
104-
try:
105-
installed_backends = [value for value, miss in zip(BACKEND_OPTIONS, self.missing_backends) if not miss]
106-
active_num = installed_backends.index(self.cf.backend)
107-
except:
108-
active_num = 0
102+
active_num = 0
103+
if self.cf.backend in self.cf.installed_backends:
104+
active_num = self.cf.installed_backends.index(self.cf.backend)
109105
self.backend_option_combo.set_active(active_num)
110106
self.backend_option_combo.connect("changed", self.on_backend_option_changed)
111107
self.backend_option_combo.set_tooltip_text(self.txt.tip_backend)
@@ -192,7 +188,6 @@ def init_ui(self):
192188
def monitor_option_display(self):
193189
"""Display monitor option if backend is swww"""
194190
self.options_box.remove(self.monitor_option_combo)
195-
# if "swww" not in self.missing_backends and self.cf.backend not in ["wallutils", "feh"]:
196191
if self.cf.backend == "swww":
197192

198193
# Check available monitors:
@@ -219,12 +214,9 @@ def monitor_option_display(self):
219214

220215

221216
def check_backends(self):
222-
"""Before running the app, check which backends are installed"""
223-
self.missing_backends = check_missing_backends()
224-
225-
# Show error message if no backends are installed:
226-
if all(self.missing_backends):
227-
self.show_message(sefl.txt.err_backend)
217+
"""Before running the app, check which backends are installed or show the error"""
218+
if not self.cf.installed_backends:
219+
self.show_message(self.txt.err_backend)
228220
exit()
229221

230222

@@ -275,9 +267,9 @@ def process_images(self):
275267
self.image_paths.remove(image_path)
276268
continue
277269
# If this image is not cached yet, resize and cache it:
278-
cached_image_path = self.cachePath/os.path.basename(image_path)
270+
cached_image_path = self.cf.cache_dir / os.path.basename(image_path)
279271
if not cached_image_path.exists():
280-
cache_image(image_path, self.cachePath)
272+
cache_image(image_path, self.cf.cache_dir)
281273

282274
# Load cached thumbnail:
283275
thumbnail = GdkPixbuf.Pixbuf.new_from_file(str(cached_image_path))
@@ -406,7 +398,7 @@ def on_image_clicked(self, widget, path):
406398
self.load_image_grid()
407399
print(self.txt.msg_path, self.cf.selected_wallpaper)
408400
self.cf.fill_option = self.fill_option_combo.get_active_text() or self.cf.fill_option
409-
change_wallpaper(self.cf.selected_wallpaper, self.cf, self.cf.selected_monitor, self.txt, self.missing_backends)
401+
change_wallpaper(self.cf.selected_wallpaper, self.cf, self.cf.selected_monitor, self.txt)
410402
self.cf.save()
411403

412404

@@ -433,17 +425,17 @@ def set_random_wallpaper(self):
433425
return
434426
print(self.txt.msg_path, self.cf.selected_wallpaper)
435427
self.cf.fill_option = self.fill_option_combo.get_active_text() or self.cf.fill_option
436-
change_wallpaper(self.cf.selected_wallpaper, self.cf, self.cf.selected_monitor, self.txt, self.missing_backends)
428+
change_wallpaper(self.cf.selected_wallpaper, self.cf, self.cf.selected_monitor, self.txt)
437429
self.cf.save()
438430

439431

440432
def clear_cache(self):
441433
"""Delete cache folder and reprocess the images"""
442434
try:
443-
shutil.rmtree(self.cachePath)
444-
os.makedirs(self.cachePath)
435+
shutil.rmtree(self.cf.cache_dir)
436+
os.makedirs(self.cf.cache_dir)
445437
except OSError as e:
446-
print(f"{self.txt.err_cache} '{self.cachePath}': {e}")
438+
print(f"{self.txt.err_cache} '{self.cf.cache_dir}': {e}")
447439
threading.Thread(target=self.process_images).start()
448440

449441

@@ -501,7 +493,7 @@ def on_key_pressed(self, widget, event):
501493
print(self.txt.msg_path, self.cf.selected_wallpaper)
502494
self.cf.backend = self.backend_option_combo.get_active_text()
503495
self.cf.fill_option = self.fill_option_combo.get_active_text() or self.cf.fill_option
504-
change_wallpaper(self.cf.selected_wallpaper, self.cf, self.cf.selected_monitor, self.txt, self.missing_backends)
496+
change_wallpaper(self.cf.selected_wallpaper, self.cf, self.cf.selected_monitor, self.txt)
505497
self.cf.save()
506498

507499
# Prevent other default key handling:

waypaper/changer.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
from waypaper.options import BACKEND_OPTIONS
77

88

9-
def change_wallpaper(image_path, cf, monitor, txt, missing_backends):
10-
"""Run a system command to change the wallpaper depending on the backend"""
9+
def change_wallpaper(image_path, cf, monitor, txt):
10+
"""Run system commands to change the wallpaper depending on the backend"""
1111

1212
fill_option = cf.fill_option
1313
color = cf.color
1414
backend = cf.backend
1515
swww_transition = cf.swww_transition
16+
installed_backends = cf.installed_backends
1617

1718
try:
1819
# swaybg backend:
@@ -41,14 +42,12 @@ def change_wallpaper(image_path, cf, monitor, txt, missing_backends):
4142
"tile": "no",
4243
}
4344
fill = fill_types[fill_option.lower()]
44-
is_swaybg_installed = not missing_backends[BACKEND_OPTIONS.index("swaybg")]
45-
if is_swaybg_installed:
45+
if "swaybg" in installed_backends:
4646
try:
4747
subprocess.Popen(["killall", "swaybg"])
4848
time.sleep(0.005)
4949
except Exception as e:
5050
print(f"{ERR_KILL} {e}")
51-
print(missing_backends)
5251
subprocess.Popen(["swww", "init"])
5352
command = ["swww", "img", image_path]
5453
command.extend(["--resize", fill])

waypaper/common.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,15 @@ def get_random_file(folder, include_subfolders):
3838
return None
3939

4040

41-
def check_missing_backends():
41+
def check_installed_backends():
4242
"""Check which backends are installed in the system"""
43-
missing_backends = []
43+
installed_backends = []
4444
for backend in BACKEND_OPTIONS:
4545
if backend == "wallutils":
46-
backend = "setwallpaper"
47-
is_backend_missing = not bool(shutil.which(backend))
48-
missing_backends.append(is_backend_missing)
49-
return missing_backends
46+
binary_name = "setwallpaper"
47+
else:
48+
binary_name = backend
49+
is_installed = bool(shutil.which(binary_name))
50+
if is_installed:
51+
installed_backends.append(backend)
52+
return installed_backends

waypaper/config.py

+14-11
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,34 @@
77
from platformdirs import user_config_path, user_pictures_path, user_cache_path
88

99
from waypaper.aboutdata import AboutData
10-
from waypaper.options import FILL_OPTIONS, SORT_OPTIONS, SWWW_TRANSITIONS
10+
from waypaper.options import FILL_OPTIONS, SORT_OPTIONS, SWWW_TRANSITIONS, BACKEND_OPTIONS
11+
from waypaper.common import check_installed_backends
12+
1113

1214
class Config:
1315
"""User configuration loaded from the config.ini file"""
1416
def __init__(self):
1517
self.image_folder = user_pictures_path()
18+
self.installed_backends = check_installed_backends()
1619
self.selected_wallpaper = ""
1720
self.selected_monitor = "All"
18-
self.fill_option = "fill"
19-
self.sort_option = "name"
20-
self.backend = "swaybg"
21+
self.fill_option = FILL_OPTIONS[0]
22+
self.sort_option = SORT_OPTIONS[0]
23+
self.backend = self.installed_backends[0] if self.installed_backends else BACKEND_OPTIONS[0]
2124
self.color = "#ffffff"
22-
self.swww_transition = "any"
25+
self.swww_transition = SWWW_TRANSITIONS[0]
2326
self.lang = "en"
2427
self.monitors = [self.selected_monitor]
2528
self.wallpaper = []
2629
self.include_subfolders = False
27-
self.aboutData = AboutData()
28-
self.cachePath = user_cache_path(self.aboutData.applicationName())
29-
self.configPath = user_config_path(self.aboutData.applicationName())
30-
self.config_file = self.configPath / "config.ini"
30+
self.about = AboutData()
31+
self.cache_dir = user_cache_path(self.about.applicationName())
32+
self.config_dir = user_config_path(self.about.applicationName())
33+
self.config_file = self.config_dir / "config.ini"
3134

3235
# Create config and cache folders:
33-
self.configPath.mkdir(parents=True, exist_ok=True)
34-
self.cachePath.mkdir(parents=True,exist_ok=True)
36+
self.config_dir.mkdir(parents=True, exist_ok=True)
37+
self.cache_dir.mkdir(parents=True,exist_ok=True)
3538

3639
self.read()
3740

waypaper/options.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@
1414
BACKEND_OPTIONS[3]: ['.gif', '.jpg', '.jpeg', '.png'],
1515
}
1616

17-
SWWW_TRANSITIONS = ["none", "simple", "fade", "wipe", "left", "right", "top", "bottom",
18-
"wave", "grow", "center", "any", "outer", "random"]
17+
SWWW_TRANSITIONS = ["any", "none", "simple", "fade", "wipe", "left", "right", "top",
18+
"bottom", "wave", "grow", "center", "outer", "random"]

0 commit comments

Comments
 (0)