67 lines
1.9 KiB
Python
67 lines
1.9 KiB
Python
"""Shared configuration constants for the application."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import contextlib
|
|
from pathlib import Path
|
|
from typing import Any, Callable
|
|
|
|
try: # Python 3.11 and above
|
|
import tomllib # type: ignore[attr-defined]
|
|
except ModuleNotFoundError: # pragma: no cover - fallback for <3.11
|
|
with contextlib.suppress(ModuleNotFoundError):
|
|
import tomli as tomllib # type: ignore[assignment]
|
|
if "tomllib" not in globals():
|
|
tomllib = None # type: ignore[assignment]
|
|
|
|
PREVIEW_MAX_SIZE = (1200, 800)
|
|
|
|
BASE_DIR = Path(__file__).resolve().parents[2]
|
|
IMAGES_DIR = BASE_DIR / "images"
|
|
CONFIG_FILE = BASE_DIR / "config.toml"
|
|
|
|
_DEFAULTS_BASE = {
|
|
"hue_min": 250.0,
|
|
"hue_max": 310.0,
|
|
"sat_min": 15.0,
|
|
"val_min": 15.0,
|
|
"val_max": 100.0,
|
|
"alpha": 120,
|
|
}
|
|
|
|
_DEFAULT_TYPES: dict[str, Callable[[Any], Any]] = {
|
|
"hue_min": float,
|
|
"hue_max": float,
|
|
"sat_min": float,
|
|
"val_min": float,
|
|
"val_max": float,
|
|
"alpha": int,
|
|
}
|
|
|
|
|
|
def _load_default_overrides() -> dict[str, Any]:
|
|
"""Load default slider overrides from config.toml if available."""
|
|
if tomllib is None or not CONFIG_FILE.exists():
|
|
return {}
|
|
decode_error = getattr(tomllib, "TOMLDecodeError", ValueError) # type: ignore[attr-defined]
|
|
try:
|
|
with CONFIG_FILE.open("rb") as handle:
|
|
data = tomllib.load(handle)
|
|
except (OSError, AttributeError, decode_error, TypeError): # type: ignore[arg-type]
|
|
return {}
|
|
settings = data.get("defaults")
|
|
if not isinstance(settings, dict):
|
|
return {}
|
|
overrides: dict[str, Any] = {}
|
|
for key, caster in _DEFAULT_TYPES.items():
|
|
if key not in settings:
|
|
continue
|
|
try:
|
|
overrides[key] = caster(settings[key])
|
|
except (TypeError, ValueError):
|
|
continue
|
|
return overrides
|
|
|
|
|
|
DEFAULTS = {**_DEFAULTS_BASE, **_load_default_overrides()}
|