54 lines
1.3 KiB
Python
54 lines
1.3 KiB
Python
from functools import lru_cache
|
|
from pathlib import Path
|
|
|
|
import yaml
|
|
from pydantic import BaseModel
|
|
from pydantic_settings import BaseSettings, SettingsConfigDict
|
|
|
|
|
|
class LoggingConfig(BaseModel):
|
|
level: str = "INFO"
|
|
path: Path = Path("logs/karl.log")
|
|
|
|
|
|
class AppConfig(BaseModel):
|
|
host: str = "127.0.0.1"
|
|
port: int = 8081
|
|
reload: bool = False
|
|
|
|
|
|
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="__")
|
|
logging: LoggingConfig = LoggingConfig()
|
|
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 {}
|
|
else:
|
|
import sys
|
|
sys.stderr.write(f"Warning: Config file {p} not found.")
|
|
return cls(**data)
|
|
|
|
|
|
@lru_cache
|
|
def get_settings() -> Settings:
|
|
return Settings.from_yaml()
|