"""Profiles and app settings, stored as JSON in ~/.config/k65studio/config.json.""" from __future__ import annotations import json import os from dataclasses import asdict, dataclass, field from pathlib import Path CONFIG_DIR = Path(os.environ.get("XDG_CONFIG_HOME", Path.home() / ".config")) / "k65studio" CONFIG_FILE = CONFIG_DIR / "config.json" @dataclass class Profile: effect: str = "static" colors: list[str] = field(default_factory=lambda: ["#00e5ff", "#ff2d95"]) random_colors: bool = False speed: str = "medium" brightness: int = 100 # percent sleep_minutes: int = 15 game_mode: bool = False lock_win: bool = True lock_alt_tab: bool = False lock_alt_f4: bool = False lock_shift_tab: bool = False @classmethod def from_dict(cls, data: dict) -> "Profile": known = {k: v for k, v in data.items() if k in cls.__dataclass_fields__} return cls(**known) @dataclass class Config: active: str = "Standard" profiles: dict[str, Profile] = field(default_factory=lambda: {"Standard": Profile()}) start_minimized: bool = False restore_hardware_on_exit: bool = True @property def profile(self) -> Profile: if self.active not in self.profiles: self.active = next(iter(self.profiles)) return self.profiles[self.active] @classmethod def load(cls) -> "Config": try: data = json.loads(CONFIG_FILE.read_text()) except (OSError, ValueError): return cls() profiles = {name: Profile.from_dict(p) for name, p in data.get("profiles", {}).items()} return cls( active=data.get("active", "Standard"), profiles=profiles or {"Standard": Profile()}, start_minimized=data.get("start_minimized", False), restore_hardware_on_exit=data.get("restore_hardware_on_exit", True), ) def save(self) -> None: CONFIG_DIR.mkdir(parents=True, exist_ok=True) tmp = CONFIG_FILE.with_suffix(".tmp") tmp.write_text(json.dumps(asdict(self), indent=2, ensure_ascii=False)) tmp.replace(CONFIG_FILE)