-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.py
38 lines (31 loc) · 1.15 KB
/
config.py
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
import logging
import os
import tomllib
from pathlib import Path
class Config:
def __init__(self, file: str, prefix: str) -> None:
self._file = file
self._prefix = prefix
def __load__(self, file: str) -> dict[str, dict] | None:
try:
with open(Path(__file__).with_name(file), "rb") as config_file:
return tomllib.load(config_file)
except FileNotFoundError as e:
logging.warning(f"Missing {e.filename}.")
return None
def load(self) -> dict[str, dict]:
ret = self.__load__("template." + self._file)
if not ret:
raise Exception(f"File template.{self._file} required.")
# overwrite template with config, if exists
config = self.__load__(self._file)
if config:
for k, v in config.items():
for kk, vv in v.items():
ret[k][kk] = vv
# overwrite with environment variables, if exist
for k, v in ret.items():
for kk, vv in v.items():
key = f"{self._prefix}_{k}_{kk}".upper()
ret[k][kk] = os.environ.get(key, vv)
return ret