from functools import lru_cache from pathlib import Path import yaml from pydantic import BaseModel from pydantic_settings import BaseSettings, SettingsConfigDict class AppConfig(BaseModel): host: str = "127.0.0.1" port: int = 8000 reload: bool = True class GitConfig(BaseModel): path: Path = Path("/opt/repo/sample") url: str = "ssh://git@hattori.ztsh.eu:29418/paas/heimdall.git" branch: str = "master" remote: str = "origin" class KeePassConfig(BaseModel): file: str = "database.kdbx" secret: Path | str = "/run/secrets/kp_secret" class Settings(BaseSettings): model_config = SettingsConfigDict(env_prefix="KARL_", env_nested_delimiter="__") app: AppConfig = AppConfig() git: GitConfig = GitConfig() kp: KeePassConfig = KeePassConfig() @classmethod def from_yaml(cls, path: Path | str = "config/config.yaml") -> "Settings": p = Path(path) data = {} if p.exists(): with p.open("r", encoding="utf-8") as fh: data = yaml.safe_load(fh) or {} return cls(**data) @lru_cache def get_settings() -> Settings: return Settings.from_yaml()