"""Configuration manager using Python's configparser (INI format).""" import configparser from pathlib import Path from fungus.path_manager import PathManager __all__ = ["ConfigManager", "fungus_launcher_dir"] _CONFIG_SECTION = "settings" fungus_launcher_dir = PathManager.user_config('fungus-launcher') _config_path = Path(fungus_launcher_dir) / "config.ini" _DEFAULTS = { "default-runner": "GE-Proton", "discrete-gpu": "False", "show-hidden": "False", "game-directories": "", } class ConfigManager: """Thin wrapper around configparser for the fungus-launcher settings.""" def __init__(self): self._config = configparser.ConfigParser() self._config[_CONFIG_SECTION] = dict(_DEFAULTS) self._load() # ── public helpers ────────────────────────────────────────────────── def get(self, key: str, fallback: str = "") -> str: """Return the value for *key* as a string.""" return self._config.get(_CONFIG_SECTION, key, fallback=fallback) def get_bool(self, key: str, fallback: bool = False) -> bool: """Return the value for *key* interpreted as a boolean.""" raw = self._config.get(_CONFIG_SECTION, key, fallback=str(fallback)) return raw.strip().lower() in ("true", "1", "yes", "on") def set(self, key: str, value) -> None: """Store *value* for *key* (converted to string).""" self._config.set(_CONFIG_SECTION, key, str(value)) # ── Global environment variables ────────────────────────────────── def get_envars(self) -> list[str]: """Return global environment variables as a list of KEY=VALUE strings.""" raw = self._config.get(_CONFIG_SECTION, "environment", fallback="").strip() if not raw: return [] return [line for line in raw.splitlines() if line.strip()] def set_envars(self, envvars: list[str]) -> None: """Store global environment variables as a multiline string.""" if envvars: self._config.set(_CONFIG_SECTION, "environment", "\n".join(envvars)) else: # Remove the key if empty to keep INI clean try: self._config.remove_option(_CONFIG_SECTION, "environment") except KeyError: pass def save(self) -> None: """Persist the current state to disk.""" _config_path.parent.mkdir(parents=True, exist_ok=True) with open(_config_path, "w", encoding="utf-8") as fh: self._config.write(fh) # ── internal ──────────────────────────────────────────────────────── def _load(self) -> None: if _config_path.is_file(): self._read_file(_config_path) def _read_file(self, path: Path) -> None: """Read an INI file, handling legacy key=value format gracefully.""" try: self._config.read(path, encoding="utf-8") except configparser.Error: # File exists but isn't valid INI — fall back to legacy parser self._read_legacy(path) def _read_legacy(self, path: Path) -> None: """Parse old-style key=value files (no sections, optional quoting). Only keys present in ``_DEFAULTS`` are imported; stray or deprecated entries are silently ignored. """ try: with open(path, encoding="utf-8") as fh: for raw_line in fh: if (line := raw_line.strip()) and "=" in line \ and not line.startswith(";") and not line.startswith("#"): key, value = line.split("=", 1) key = key.strip() if key in _DEFAULTS: value = value.strip().strip('"').strip("'") self._config.set(_CONFIG_SECTION, key, value) except OSError: pass