config.py (1278B)
1 """Configuration loaded from data/config.json with defaults.""" 2 3 import json 4 from dataclasses import dataclass, field 5 from pathlib import Path 6 7 8 @dataclass 9 class DashboardConfig: 10 """Application configuration with sensible defaults.""" 11 12 hermes_base_url: str = "http://localhost:8642" 13 data_dir: str = "data/" 14 hermes_api_key: str = "" 15 16 @classmethod 17 def from_file(cls, path: str) -> "DashboardConfig": # pragma: no mutate: block 18 """Load config from a JSON file, falling back to defaults.""" 19 defaults = cls() 20 try: 21 raw = Path(path).read_text() 22 data = json.loads(raw) 23 except FileNotFoundError: 24 return defaults 25 return cls( 26 hermes_base_url=data.get("hermes_base_url", defaults.hermes_base_url), 27 data_dir=data.get("data_dir", defaults.data_dir), 28 hermes_api_key=data.get("hermes_api_key", defaults.hermes_api_key), 29 ) 30 31 32 _config: DashboardConfig | None = None 33 34 35 def get_config() -> DashboardConfig: # pragma: no mutate: block 36 """Return the cached global config, loading from file on first call.""" 37 global _config # noqa: PLW0603 38 if _config is None: 39 _config = DashboardConfig.from_file("data/config.json") 40 return _config