"""Global config (~/.brainconfig) management.""" import json from pathlib import Path from brain.paths import get_config_path def load_config() -> dict: """Read ~/.brainconfig. Returns empty dict if missing or malformed.""" path = get_config_path() if not path.exists(): return {} try: return json.loads(path.read_text(encoding="utf-8")) except (json.JSONDecodeError, OSError): return {} def save_config(config: dict) -> None: """Write config to ~/.brainconfig.""" path = get_config_path() path.write_text(json.dumps(config, indent=2), encoding="utf-8") def get_active_brain() -> Path | None: """Return the active Brain Instance path, or None.""" cfg = load_config() raw = cfg.get("active_brain") if raw: return Path(raw) return None def set_active_brain(path: Path) -> None: """Set the active Brain Instance path.""" cfg = load_config() cfg["active_brain"] = str(path.resolve()) save_config(cfg) def get_storage_mode() -> str: """Return the current project storage mode ('local' or 'global').""" cfg = load_config() return cfg.get("storage_mode", "local") def set_storage_mode(mode: str) -> None: """Set the project storage mode ('local' or 'global').""" cfg = load_config() cfg["storage_mode"] = mode save_config(cfg)