From 002a9c287fe8c1496bef4769c743fa78e67d5f2b Mon Sep 17 00:00:00 2001 From: 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> Date: Sat, 13 Jun 2026 02:25:09 +1000 Subject: [PATCH] refactor: add get_config func --- src/caelestia/subcommands/record.py | 7 ++--- src/caelestia/subcommands/resizer.py | 6 ++--- src/caelestia/subcommands/toggle.py | 6 ++--- src/caelestia/utils/paths.py | 12 +++++++++ src/caelestia/utils/theme.py | 7 ++--- src/caelestia/utils/wallpaper.py | 39 +++++++++++++--------------- 6 files changed, 40 insertions(+), 37 deletions(-) diff --git a/src/caelestia/subcommands/record.py b/src/caelestia/subcommands/record.py index 7a24ba74..372aee41 100644 --- a/src/caelestia/subcommands/record.py +++ b/src/caelestia/subcommands/record.py @@ -1,4 +1,3 @@ -import json import re import shutil import subprocess @@ -9,7 +8,7 @@ from pathlib import Path from caelestia.utils import hypr from caelestia.utils.notify import close_notification, notify -from caelestia.utils.paths import recording_notif_path, recording_path, recordings_dir, user_config_path +from caelestia.utils.paths import get_config, recording_notif_path, recording_path, recordings_dir RECORDER = "gpu-screen-recorder" @@ -65,12 +64,10 @@ class Command: if self.args.sound: args += ["-a", "default_output"] + config = get_config() try: - config = json.loads(user_config_path.read_text()) if "record" in config and "extraArgs" in config["record"]: args += config["record"]["extraArgs"] - except (json.JSONDecodeError, FileNotFoundError): - pass except TypeError as e: raise ValueError(f"Config option 'record.extraArgs' should be an array: {e}") diff --git a/src/caelestia/subcommands/resizer.py b/src/caelestia/subcommands/resizer.py index 4b6eb790..01c25988 100644 --- a/src/caelestia/subcommands/resizer.py +++ b/src/caelestia/subcommands/resizer.py @@ -8,7 +8,7 @@ from typing import Any, Dict, Optional from caelestia.utils import hypr from caelestia.utils.io import log_message -from caelestia.utils.paths import user_config_path +from caelestia.utils.paths import get_config class WindowRule: @@ -52,8 +52,8 @@ class Command: WindowRule("^[Pp]icture(-| )in(-| )[Pp]icture$", "titleRegex", "", "", ["pip"]), ] + config = get_config() try: - config = json.loads(user_config_path.read_text()) if "resizer" in config and "rules" in config["resizer"]: rules = [] for rule_config in config["resizer"]["rules"]: @@ -67,7 +67,7 @@ class Command: ) ) return rules - except (json.JSONDecodeError, KeyError): + except KeyError: log_message("ERROR: invalid config") except FileNotFoundError: pass diff --git a/src/caelestia/subcommands/toggle.py b/src/caelestia/subcommands/toggle.py index 56565f37..f114bbfa 100644 --- a/src/caelestia/subcommands/toggle.py +++ b/src/caelestia/subcommands/toggle.py @@ -6,7 +6,7 @@ from collections import ChainMap from typing import Any, Callable, cast from caelestia.utils import hypr -from caelestia.utils.paths import user_config_path +from caelestia.utils.paths import get_config def is_subset(superset, subset): @@ -103,8 +103,8 @@ class Command: }, } try: - self.cfg = DeepChainMap(json.loads(user_config_path.read_text())["toggles"], self.cfg) - except (FileNotFoundError, json.JSONDecodeError, KeyError): + self.cfg = DeepChainMap(get_config()["toggles"], self.cfg) + except KeyError: pass def run(self) -> None: diff --git a/src/caelestia/utils/paths.py b/src/caelestia/utils/paths.py index db8d0a42..62348d9d 100644 --- a/src/caelestia/utils/paths.py +++ b/src/caelestia/utils/paths.py @@ -5,6 +5,8 @@ import tempfile from pathlib import Path from typing import Any +from caelestia.utils.io import warn + config_dir: Path = Path(os.getenv("XDG_CONFIG_HOME", Path.home() / ".config")) data_dir: Path = Path(os.getenv("XDG_DATA_HOME", Path.home() / ".local/share")) state_dir: Path = Path(os.getenv("XDG_STATE_HOME", Path.home() / ".local/state")) @@ -67,3 +69,13 @@ def atomic_write(path: Path, content: str) -> None: def atomic_dump(path: Path, content: dict[str, Any]) -> None: atomic_write(path, json.dumps(content)) + + +def get_config() -> dict[str, Any]: + try: + return json.loads(user_config_path.read_text()) + except json.JSONDecodeError: + warn("failed to parse config, invalid JSON") + except FileNotFoundError: + pass + return {} diff --git a/src/caelestia/utils/theme.py b/src/caelestia/utils/theme.py index 1cd33d94..472cfb8e 100644 --- a/src/caelestia/utils/theme.py +++ b/src/caelestia/utils/theme.py @@ -15,9 +15,9 @@ from caelestia.utils.paths import ( c_state_dir, config_dir, data_dir, + get_config, templates_dir, theme_dir, - user_config_path, user_templates_dir, ) from caelestia.utils.scheme import get_scheme @@ -417,10 +417,7 @@ def apply_colours(colours: dict[str, str], mode: str) -> None: except BlockingIOError: return - try: - cfg = json.loads(user_config_path.read_text())["theme"] - except (FileNotFoundError, json.JSONDecodeError, KeyError): - cfg = {} + cfg = get_config().get("theme", {}) def check(key: str) -> bool: return cfg[key] if key in cfg else True diff --git a/src/caelestia/utils/wallpaper.py b/src/caelestia/utils/wallpaper.py index 227a8fd5..2cf08698 100644 --- a/src/caelestia/utils/wallpaper.py +++ b/src/caelestia/utils/wallpaper.py @@ -16,7 +16,7 @@ from caelestia.utils.material import get_colours_for_image from caelestia.utils.colourfulness import get_variant from caelestia.utils.paths import ( compute_hash, - user_config_path, + get_config, wallpaper_link_path, wallpaper_path_path, wallpaper_thumbnail_path, @@ -186,26 +186,23 @@ def set_wallpaper(wall: Path, no_smart: bool) -> None: apply_colours(scheme.colours, scheme.mode) # Run custom post-hook if configured - try: - cfg = json.loads(user_config_path.read_text()).get("wallpaper", {}) - if post_hook := cfg.get("postHook"): - subprocess.run( - post_hook, - shell=True, - env={ - **os.environ, - "WALLPAPER_PATH": str(wall), - "SCHEME_NAME": scheme.name, - "SCHEME_FLAVOUR": scheme.flavour, - "SCHEME_MODE": scheme.mode, - "SCHEME_VARIANT": scheme.variant, - "SCHEME_COLOURS": json.dumps(scheme.colours), - "THUMBNAIL_PATH": str(thumb), - }, - stderr=subprocess.DEVNULL, - ) - except (FileNotFoundError, json.JSONDecodeError): - pass + cfg = get_config().get("wallpaper", {}) + if post_hook := cfg.get("postHook"): + subprocess.run( + post_hook, + shell=True, + env={ + **os.environ, + "WALLPAPER_PATH": str(wall), + "SCHEME_NAME": scheme.name, + "SCHEME_FLAVOUR": scheme.flavour, + "SCHEME_MODE": scheme.mode, + "SCHEME_VARIANT": scheme.variant, + "SCHEME_COLOURS": json.dumps(scheme.colours), + "THUMBNAIL_PATH": str(thumb), + }, + stderr=subprocess.DEVNULL, + ) def set_random(args: Namespace) -> None: