refactor: add get_config func

This commit is contained in:
2 * r + 2 * t
2026-06-13 02:25:09 +10:00
parent d7b65b5946
commit 002a9c287f
6 changed files with 40 additions and 37 deletions
+12
View File
@@ -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 {}
+2 -5
View File
@@ -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
+18 -21
View File
@@ -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: