Simplify code
This commit is contained in:
parent
eada9049fa
commit
d03879c5f6
|
|
@ -3,7 +3,6 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import os
|
import os
|
||||||
from pathlib import Path
|
|
||||||
from typing import Dict
|
from typing import Dict
|
||||||
|
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
|
|
@ -15,32 +14,6 @@ class ConfigError(RuntimeError):
|
||||||
"""Raised when critical configuration is missing."""
|
"""Raised when critical configuration is missing."""
|
||||||
|
|
||||||
|
|
||||||
LEGACY_KEY_MAP = {
|
|
||||||
"immich": "IMMICH_API_KEY",
|
|
||||||
"openai": "OPENAI_API_KEY",
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def _load_legacy_api_keys(path: Path) -> Dict[str, str]:
|
|
||||||
"""Parse the legacy ``api_keys`` file if it exists."""
|
|
||||||
|
|
||||||
if not path.exists():
|
|
||||||
return {}
|
|
||||||
|
|
||||||
values: Dict[str, str] = {}
|
|
||||||
for line in path.read_text().splitlines():
|
|
||||||
line = line.strip()
|
|
||||||
if not line or line.startswith("#"):
|
|
||||||
continue
|
|
||||||
if "=" not in line:
|
|
||||||
continue
|
|
||||||
key, value = [piece.strip() for piece in line.split("=", 1)]
|
|
||||||
mapped_key = LEGACY_KEY_MAP.get(key.lower())
|
|
||||||
if mapped_key and value:
|
|
||||||
values[mapped_key] = value
|
|
||||||
return values
|
|
||||||
|
|
||||||
|
|
||||||
def load_settings() -> Dict[str, str]:
|
def load_settings() -> Dict[str, str]:
|
||||||
"""Load configuration from environment variables and ``api_keys``."""
|
"""Load configuration from environment variables and ``api_keys``."""
|
||||||
|
|
||||||
|
|
@ -62,11 +35,6 @@ def load_settings() -> Dict[str, str]:
|
||||||
"MASTODON_CLIENT_SECRET": os.getenv("MASTODON_CLIENT_SECRET", ""),
|
"MASTODON_CLIENT_SECRET": os.getenv("MASTODON_CLIENT_SECRET", ""),
|
||||||
}
|
}
|
||||||
|
|
||||||
legacy_values = _load_legacy_api_keys(Path("api_keys"))
|
|
||||||
for key, value in legacy_values.items():
|
|
||||||
if not settings.get(key):
|
|
||||||
settings[key] = value
|
|
||||||
|
|
||||||
missing = [
|
missing = [
|
||||||
key for key in ("IMMICH_API_KEY", "OPENAI_API_KEY") if not settings.get(key)
|
key for key in ("IMMICH_API_KEY", "OPENAI_API_KEY") if not settings.get(key)
|
||||||
]
|
]
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue