| """Configuration loading.""" |
|
|
| from __future__ import annotations |
|
|
| import os |
| from pathlib import Path |
| from typing import Any |
|
|
| import yaml |
|
|
|
|
| def _read_yaml(path: Path) -> dict[str, Any]: |
| if not path.exists(): |
| return {} |
| with path.open("r", encoding="utf-8") as handle: |
| return yaml.safe_load(handle) or {} |
|
|
|
|
| def load_config(config_name: str = "base.yaml") -> dict[str, Any]: |
| root = Path(__file__).resolve().parents[2] |
| config_path = root / "configs" / config_name |
| return _read_yaml(config_path) |
|
|
|
|
| def load_project_env(path: Path | None = None) -> None: |
| """Load simple KEY=VALUE pairs from .env without overriding shell env.""" |
|
|
| root = Path(__file__).resolve().parents[2] |
| env_path = path or root / ".env" |
| if not env_path.exists(): |
| return |
| for raw_line in env_path.read_text(encoding="utf-8").splitlines(): |
| line = raw_line.strip() |
| if not line or line.startswith("#") or "=" not in line: |
| continue |
| key, value = line.split("=", 1) |
| key = key.strip() |
| if not key or key in os.environ: |
| continue |
| os.environ[key] = value.strip().strip('"').strip("'") |
|
|
|
|
| def env_bool(name: str, default: bool = False) -> bool: |
| raw = os.getenv(name) |
| if raw is None: |
| return default |
| return raw.strip().lower() in {"1", "true", "yes", "on"} |
|
|
|
|
| def env_int(name: str, default: int) -> int: |
| raw = os.getenv(name) |
| if raw is None: |
| return default |
| try: |
| return int(raw) |
| except ValueError: |
| return default |
|
|