Skip to content

Commit

Permalink
Fixing subfolder levels
Browse files Browse the repository at this point in the history
This reimplements subfolder filtering, so that now there is no "depth"
just either show all subfolders or only first level. The logic of UI
ticks are also changed. This addresses the issue #134
  • Loading branch information
anufrievroman committed Jan 25, 2025
1 parent 856254a commit aa04e6f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 46 deletions.
6 changes: 4 additions & 2 deletions waypaper/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ def run():

# Otherwise set a random wallpaper:
else:
wallpaper_str = get_random_file(cf.backend, str(cf.image_folder), cf.include_subfolders, cf.cache_dir, cf.show_hidden)
wallpaper_str = get_random_file(cf.backend, str(cf.image_folder), cf.include_subfolders,
cf.include_all_subfolders, cf.cache_dir, cf.show_hidden)
if wallpaper_str:
wallpaper = pathlib.Path(wallpaper_str)
else:
Expand All @@ -88,7 +89,8 @@ def run():
if args.restore or args.random:
for index, (wallpaper, monitor) in enumerate(zip(cf.wallpapers, cf.monitors)):
if args.random:
wallpaper_str = get_random_file(cf.backend, str(cf.image_folder), cf.include_subfolders, cf.cache_dir, cf.show_hidden)
wallpaper_str = get_random_file(cf.backend, str(cf.image_folder), cf.include_subfolders,
cf.include_all_subfolders, cf.cache_dir, cf.show_hidden)
if wallpaper_str:
wallpaper = pathlib.Path(wallpaper_str)
cf.wallpapers[index] = wallpaper
Expand Down
26 changes: 19 additions & 7 deletions waypaper/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,6 @@ def __init__(self, txt: Chinese|English|French|German|Polish|Russian|Belarusian|
# Start the image processing in a separate thread:
threading.Thread(target=self.process_images).start()

@property
def depth(self) -> int:
"""Calculate the depth of the subfolders depending on active parameters"""
return int(self.cf.subfolder_depth) if self.cf.include_subfolders else 0


def init_ui(self) -> None:
"""Initialize the UI elements of the application"""
Expand Down Expand Up @@ -321,6 +316,12 @@ def create_options_menu(self) -> None:
self.include_subfolders_checkbox.connect("toggled", self.on_include_subfolders_toggled)
self.menu.append(self.include_subfolders_checkbox)

# Create all subfolder toggle:
self.include_all_subfolders_checkbox = Gtk.CheckMenuItem(label=self.txt.msg_all_subfolders)
self.include_all_subfolders_checkbox.set_active(self.cf.include_all_subfolders)
self.include_all_subfolders_checkbox.connect("toggled", self.on_include_all_subfolders_toggled)
self.menu.append(self.include_all_subfolders_checkbox)

# Create hidden toggle:
self.include_hidden_checkbox = Gtk.CheckMenuItem(label=self.txt.msg_hidden)
self.include_hidden_checkbox.set_active(self.cf.show_hidden)
Expand Down Expand Up @@ -463,7 +464,7 @@ def sort_images(self) -> None:
def process_images(self) -> None:
"""Load images from the selected folder, resize them, and arrange into a grid"""
self.image_paths = get_image_paths(self.cf.backend, str(self.cf.image_folder), self.cf.include_subfolders,
self.cf.show_hidden, self.cf.show_gifs_only, self.depth)
self.cf.include_all_subfolders, self.cf.show_hidden, self.cf.show_gifs_only)
self.sort_images()

# Show caching label:
Expand Down Expand Up @@ -609,6 +610,16 @@ def on_mpv_sound_toggled(self, toggle) -> None:
def on_include_subfolders_toggled(self, toggle) -> None:
"""Toggle subfolders visibility via menu"""
self.cf.include_subfolders = toggle.get_active()
if not self.cf.include_subfolders:
self.cf.include_all_subfolders = False
threading.Thread(target=self.process_images).start()


def on_include_all_subfolders_toggled(self, toggle) -> None:
"""Toggle subfolders visibility via menu"""
self.cf.include_all_subfolders = toggle.get_active()
if self.cf.include_all_subfolders:
self.cf.include_subfolders = True
threading.Thread(target=self.process_images).start()


Expand Down Expand Up @@ -722,7 +733,8 @@ def on_exit_clicked(self, widget) -> None:

def set_random_wallpaper(self) -> None:
"""Choose a random image and set it as the wallpaper"""
new_wallpaper = get_random_file(self.cf.backend, str(self.cf.image_folder), self.cf.include_subfolders, self.cf.cache_dir)
new_wallpaper = get_random_file(self.cf.backend, str(self.cf.image_folder), self.cf.include_subfolders,
self.cf.include_all_subfolders, self.cf.cache_dir)
if new_wallpaper:
self.cf.select_wallpaper(new_wallpaper)
else:
Expand Down
41 changes: 9 additions & 32 deletions waypaper/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
import os
import random
import shutil
from pathlib import Path

from pathlib import Path
from typing import List
from glob import glob

from waypaper.options import IMAGE_EXTENSIONS, BACKEND_OPTIONS

Expand All @@ -22,14 +19,13 @@ def has_image_extension(file_path: str, backend: str) -> bool:
def get_image_paths(backend: str,
root_folder: str,
include_subfolders: bool = False,
include_all_subfolders: bool = False,
include_hidden: bool = False,
only_gifs: bool = False,
depth: int = 1):
only_gifs: bool = False
):
"""Get a list of file paths depending on the filters that were requested"""
if depth < 0:
return get_image_paths_infinite_recursion(backend, root_folder, include_hidden, only_gifs)
image_paths = []
for root, directories, files in os.walk(root_folder):
for root, directories, files in os.walk(root_folder, followlinks=True):
# Remove hidden files from consideration:
for directory in directories:
if directory.startswith('.') and not include_hidden:
Expand All @@ -40,10 +36,9 @@ def get_image_paths(backend: str,
continue

# Remove deep folders from consideration:
if depth is not None and root != root_folder:
current_depth = root.count(os.path.sep) - str(root_folder).count(
os.path.sep)
if current_depth > depth:
if not include_all_subfolders and root != root_folder:
current_depth = root.count(os.path.sep) - str(root_folder).count(os.path.sep)
if current_depth > 1:
continue

# Remove files that are not images from consideration:
Expand All @@ -55,37 +50,19 @@ def get_image_paths(backend: str,
if not filename.endswith('.gif') and only_gifs:
continue
image_paths.append(os.path.join(root, filename))
# print(root, directories, files)
return image_paths


def get_image_paths_infinite_recursion(backend: str,
root_folder: str,
include_hidden: bool = False,
only_gifs: bool = False):
"""Get a list of file paths depending on the filters that were requested"""
paths = os.walk(root_folder, followlinks=True)
paths = map(lambda x: map(lambda y: os.path.join(x[0], y), x[2]), paths)
paths = [ x for xs in paths for x in xs ]
print(paths)
if only_gifs:
paths = list(filter(lambda f: f.lower().endswith(".gif"),paths))
else:
paths = list(filter(lambda x: has_image_extension(x, backend), paths))
if not include_hidden:
paths = list(filter(lambda x: not x.startswith(".") , paths))
return paths


def get_random_file(backend: str,
folder: str,
include_subfolders: bool,
include_all_subfolders: bool,
cache_dir: Path,
include_hidden: bool = False) -> str | None:
"""Pick a random file from the folder and update cache"""
try:
# Get all image paths from the folder:
image_paths = get_image_paths(backend, folder, include_subfolders,
image_paths = get_image_paths(backend, folder, include_subfolders, include_all_subfolders,
include_hidden, only_gifs=False)

# Read cache file with already used images:
Expand Down
8 changes: 3 additions & 5 deletions waypaper/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def __init__(self):
self.wallpapers = []
self.post_command = ""
self.include_subfolders = False
self.subfolder_depth = 1
self.include_all_subfolders = False
self.show_hidden = False
self.show_gifs_only = False
self.show_transition_options = True
Expand Down Expand Up @@ -92,7 +92,7 @@ def read(self) -> None:
self.mpvpaper_timer = config.get("Settings", "mpvpaper_timer", fallback=self.mpvpaper_timer)
self.lang = config.get("Settings", "language", fallback=self.lang)
self.include_subfolders = config.getboolean("Settings", "subfolders", fallback=self.include_subfolders)
self.subfolder_depth = config.get("Settings", "subfolder_depth", fallback=self.subfolder_depth)
self.include_all_subfolders = config.getboolean("Settings", "all_subfolders", fallback=self.include_all_subfolders)
self.show_transition_options = config.getboolean("Settings", "show_transition_options", fallback=self.show_transition_options)
self.show_hidden = config.getboolean("Settings", "show_hidden", fallback=self.show_hidden)
self.show_gifs_only = config.getboolean("Settings", "show_gifs_only", fallback=self.show_gifs_only)
Expand Down Expand Up @@ -167,8 +167,6 @@ def check_validity(self) -> None:
self.swww_transition_fps = 60
if 0 > int(self.mpvpaper_timer):
self.mpvpaper_timer = 0
if 0 > int(self.subfolder_depth):
self.subfolder_depth = 1

def attribute_selected_wallpaper(self) -> None:
"""
Expand Down Expand Up @@ -226,7 +224,7 @@ def save(self, only_state=False) -> None:
config.set("Settings", "sort", self.sort_option)
config.set("Settings", "color", self.color)
config.set("Settings", "subfolders", str(self.include_subfolders))
config.set("Settings", "subfolder_depth", str(self.subfolder_depth))
config.set("Settings", "all_subfolders", str(self.include_all_subfolders))
config.set("Settings", "show_hidden", str(self.show_hidden))
config.set("Settings", "show_gifs_only", str(self.show_gifs_only))
config.set("Settings", "show_transition_options", str(self.show_transition_options))
Expand Down

0 comments on commit aa04e6f

Please sign in to comment.