-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
95 lines (73 loc) · 2.65 KB
/
Copy pathconfig.py
File metadata and controls
95 lines (73 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"""Application configuration (MoLiM-1n6).
Loads API keys and runtime settings from environment variables. If a `.env`
file is present in the project root, `python-dotenv` is used to populate
`os.environ` from it before reading. `.env` is gitignored - copy
`.env.example` and fill in your keys.
Required keys (see https://www.omdbapi.com/apikey.aspx and
https://www.themoviedb.org/settings/api):
OMDB_API_KEY
TMDB_API_KEY
Usage:
from config import get_settings
settings = get_settings()
settings.require_api_keys() # raises ConfigError if missing
"""
from __future__ import annotations
import os
from dataclasses import dataclass
from functools import lru_cache
from pathlib import Path
try:
from dotenv import load_dotenv as _load_dotenv
except ImportError: # python-dotenv not installed yet (pre-setup)
_load_dotenv = None
PROJECT_ROOT = Path(__file__).resolve().parent
ENV_FILE = PROJECT_ROOT / ".env"
ENV_EXAMPLE = PROJECT_ROOT / ".env.example"
class ConfigError(RuntimeError):
"""Raised when required configuration is missing."""
@dataclass(frozen=True)
class Settings:
omdb_api_key: str
tmdb_api_key: str
def require_api_keys(self) -> None:
missing = []
if not self.omdb_api_key:
missing.append("OMDB_API_KEY")
if not self.tmdb_api_key:
missing.append("TMDB_API_KEY")
if missing:
raise ConfigError(
"Missing required API key(s): "
+ ", ".join(missing)
+ ". Copy .env.example to .env and fill in values, "
"or set them as environment variables. "
"Get keys at https://www.omdbapi.com/apikey.aspx and "
"https://www.themoviedb.org/settings/api"
)
def _load_env_file() -> None:
if _load_dotenv is None:
return
if ENV_FILE.is_file():
_load_dotenv(ENV_FILE, override=False)
@lru_cache(maxsize=1)
def get_settings() -> Settings:
"""Return cached Settings populated from env (and optional .env)."""
_load_env_file()
return Settings(
omdb_api_key=os.environ.get("OMDB_API_KEY", "").strip(),
tmdb_api_key=os.environ.get("TMDB_API_KEY", "").strip(),
)
def reset_cache() -> None:
"""Clear cached Settings (useful in tests)."""
get_settings.cache_clear()
if __name__ == "__main__":
s = get_settings()
print(f"OMDB_API_KEY: {'set' if s.omdb_api_key else 'MISSING'}")
print(f"TMDB_API_KEY: {'set' if s.tmdb_api_key else 'MISSING'}")
try:
s.require_api_keys()
print("OK - both keys present.")
except ConfigError as e:
print(f"ERROR: {e}")
raise SystemExit(2)