import json import os from dataclasses import asdict, dataclass from pathlib import Path def _tafa_dir() -> Path: p = Path.home() / ".tafa" p.mkdir(parents=True, exist_ok=True) return p def config_path() -> Path: return _tafa_dir() / "config.json" @dataclass class AppConfig: sources: list[str] date_start: str date_end: str destination: str def load_config() -> AppConfig | None: path = config_path() if not path.exists(): return None try: data = json.loads(path.read_text()) return AppConfig( sources=data["sources"], date_start=data["date_start"], date_end=data["date_end"], destination=data["destination"], ) except (KeyError, json.JSONDecodeError, TypeError): return None def save_config(cfg: AppConfig) -> None: path = config_path() tmp = path.with_suffix(".tmp") tmp.write_text(json.dumps(asdict(cfg), indent=2)) os.replace(tmp, path)