48 lines
1.4 KiB
Python
48 lines
1.4 KiB
Python
"""
|
|
config/config.py — config loader: file → AppConfig.
|
|
Stored as JSON in the same directory as the app.
|
|
"""
|
|
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
from .schema import AppConfig, DatabaseConfig, SecurityConfig, ServerConfig, TenorConfig
|
|
|
|
CONFIG_FILE = os.environ.get("CHAT_CONFIG", "chat_config.json")
|
|
|
|
def _config_path() -> str:
|
|
"""Return absolute path to config file."""
|
|
p = Path(CONFIG_FILE)
|
|
if p.is_absolute():
|
|
return str(p)
|
|
return str(Path(__file__).resolve().parent.parent / CONFIG_FILE)
|
|
|
|
|
|
def load() -> AppConfig:
|
|
path = _config_path()
|
|
cfg = AppConfig()
|
|
if os.path.exists(path):
|
|
with open(path) as f:
|
|
data = json.load(f)
|
|
if "db" in data:
|
|
cfg.db = DatabaseConfig(**{**cfg.db.__dict__, **data["db"]})
|
|
if "security" in data:
|
|
cfg.security = SecurityConfig(**{**cfg.security.__dict__, **data["security"]})
|
|
if "server" in data:
|
|
cfg.server = ServerConfig(**{**cfg.server.__dict__, **data["server"]})
|
|
if "tenor" in data:
|
|
cfg.tenor = TenorConfig(**{**cfg.tenor.__dict__, **data["tenor"]})
|
|
return cfg
|
|
|
|
|
|
def save(cfg: AppConfig):
|
|
path = _config_path()
|
|
data = {
|
|
"db": cfg.db.__dict__,
|
|
"security": cfg.security.__dict__,
|
|
"server": cfg.server.__dict__,
|
|
"tenor": cfg.tenor.__dict__,
|
|
}
|
|
with open(path, "w") as f:
|
|
json.dump(data, f, indent=2)
|