acupedia-ops/acupedia_ops/common/config.py

21 lines
748 B
Python

from __future__ import annotations
from dataclasses import dataclass
from pathlib import Path
from typing import Any
import yaml
@dataclass(frozen=True)
class OpsConfig:
environment: str
values: dict[str, Any]
def load_config(environment: str, config_root: Path | None = None) -> OpsConfig:
root = config_root or Path(__file__).resolve().parents[2] / "config"
path = root / f"{environment}.yaml"
if not path.exists():
raise FileNotFoundError(f"Configuration not found: {path}")
values = yaml.safe_load(path.read_text(encoding="utf-8")) or {}
if not isinstance(values, dict):
raise ValueError(f"Configuration root must be a mapping: {path}")
return OpsConfig(environment=environment, values=values)