Adding the new rewrite
This commit is contained in:
1 parent
4aca9f08d1
commit
9b48b01d1d
62 files changed
+4510
-1406
No files matched your search
+23
-14
@@ -1,31 +1,39 @@
|
||||
"""Redis-backed configuration storage replacing YAML file.
|
||||
"""Redis-backed configuration storage.
|
||||
|
||||
Schema:
|
||||
Key ups:config:json -> JSON object: {"ups": [...], "smtp": {...}|null}
|
||||
|
||||
Migration:
|
||||
On first load if redis key missing and legacy YAML present, import it.
|
||||
Key ``ups:config:json`` holds JSON: ``{"ups": [...], "smtp": {...}|null, "ui": {...}}``.
|
||||
Legacy YAML import runs once if Redis is empty.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
from typing import Optional
|
||||
from pathlib import Path
|
||||
|
||||
import json
|
||||
import logging
|
||||
from .storage import get_redis
|
||||
from .config import AppConfig, CONFIG_PATH
|
||||
from pathlib import Path
|
||||
|
||||
import yaml
|
||||
|
||||
from .config import CONFIG_PATH, AppConfig
|
||||
from .storage import get_redis
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
REDIS_CONFIG_KEY = "ups:config:json"
|
||||
|
||||
|
||||
def _load_legacy_yaml(path: Path) -> Optional[AppConfig]:
|
||||
def _strip_smtp_password(data: dict) -> dict:
|
||||
"""Strip persisted SMTP passwords (migration from older schema)."""
|
||||
smtp = data.get("smtp")
|
||||
if isinstance(smtp, dict) and "password" in smtp:
|
||||
smtp.pop("password", None)
|
||||
return data
|
||||
|
||||
|
||||
def _load_legacy_yaml(path: Path) -> AppConfig | None:
|
||||
if not path.exists():
|
||||
return None
|
||||
try:
|
||||
with path.open() as f:
|
||||
raw = yaml.safe_load(f) or {}
|
||||
raw = _strip_smtp_password(raw)
|
||||
return AppConfig(**raw)
|
||||
except Exception as e: # pragma: no cover
|
||||
logger.warning("Failed to import legacy YAML config: %s", e)
|
||||
@@ -40,14 +48,13 @@ def load_config_redis() -> AppConfig:
|
||||
data = json.loads(raw)
|
||||
except json.JSONDecodeError:
|
||||
data = {}
|
||||
data = _strip_smtp_password(data)
|
||||
return AppConfig(**data)
|
||||
# Migration path
|
||||
legacy = _load_legacy_yaml(CONFIG_PATH)
|
||||
if legacy:
|
||||
save_config_redis(legacy)
|
||||
logger.info("Imported legacy YAML config into Redis")
|
||||
return legacy
|
||||
# If nothing exists, create empty scaffold
|
||||
empty = AppConfig(ups=[], smtp=None)
|
||||
save_config_redis(empty)
|
||||
return empty
|
||||
@@ -55,4 +62,6 @@ def load_config_redis() -> AppConfig:
|
||||
|
||||
def save_config_redis(cfg: AppConfig) -> None:
|
||||
r = get_redis()
|
||||
r.set(REDIS_CONFIG_KEY, json.dumps(cfg.model_dump(exclude_none=True)))
|
||||
data = cfg.model_dump(exclude_none=True)
|
||||
data = _strip_smtp_password(data)
|
||||
r.set(REDIS_CONFIG_KEY, json.dumps(data))
|
||||
Reference in new issue
Block a user