From 04f7d81a56c8c04290e2d31a8e577beaa425ff2a Mon Sep 17 00:00:00 2001 From: l0drex Date: Thu, 18 Apr 2024 10:44:36 +0200 Subject: [PATCH] Refactor gtk available_themes property --- yin_yang/plugins/_plugin.py | 19 ++++++++++++++++++- yin_yang/plugins/gtk.py | 15 ++------------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/yin_yang/plugins/_plugin.py b/yin_yang/plugins/_plugin.py index d7469d2..de7abd7 100644 --- a/yin_yang/plugins/_plugin.py +++ b/yin_yang/plugins/_plugin.py @@ -4,7 +4,7 @@ from abc import ABC, abstractmethod from configparser import ConfigParser from pathlib import Path -from typing import Optional +from typing import Optional, List from PySide6.QtDBus import QDBusConnection, QDBusMessage from PySide6.QtGui import QColor, QRgba64 @@ -365,3 +365,20 @@ def flatpak_user(app_id: str) -> Path: def snap_path(app: str) -> Path: return Path(f'/var/lib/snapd/snap/{app}/current') + +def themes_from_theme_directories(type: str) -> List[Path]: + theme_directories = [ + Path('/usr/share/themes'), + Path('/usr/local/share/themes'), + Path.home() / '.themes', + Path.home() / '.local/share/themes', + ] + + themes = [] + for directory in theme_directories: + if not directory.is_dir(): + continue + + themes.extend(d.name for d in directory.iterdir() if d.is_dir() and (d / type).is_dir()) + + return themes diff --git a/yin_yang/plugins/gtk.py b/yin_yang/plugins/gtk.py index 1a5eb20..89ba084 100755 --- a/yin_yang/plugins/gtk.py +++ b/yin_yang/plugins/gtk.py @@ -5,16 +5,13 @@ from PySide6.QtDBus import QDBusMessage -from ._plugin import PluginDesktopDependent, PluginCommandline, DBusPlugin +from ._plugin import PluginDesktopDependent, PluginCommandline, DBusPlugin, themes_from_theme_directories from .system import test_gnome_availability from ..meta import Desktop logger = logging.getLogger(__name__) -theme_directories = ['/usr/share/themes', f'{Path.home()}/.themes'] - - class Gtk(PluginDesktopDependent): name = 'GTK' @@ -40,15 +37,7 @@ def __init__(self, desktop: Desktop): @property def available_themes(self) -> dict: - themes = [] - - for directory in theme_directories: - if not path.isdir(directory): - continue - - with scandir(directory) as entries: - themes.extend(d.name for d in entries if d.is_dir() and path.isdir(d.path + '/gtk-3.0')) - + themes = themes_from_theme_directories('gtk-3.0') return {t: t for t in themes}