diff --git a/config/config.json.example b/config/config.json.example index e9bd579..4b9e98b 100644 --- a/config/config.json.example +++ b/config/config.json.example @@ -1,3 +1,4 @@ { - "key" : "INSERTKEYHERE" + "directus_base_url" : "https://fragenkatalog.darc.de/", + "no_latex": true } diff --git a/src/config.py b/src/config.py index 8705346..ae69389 100644 --- a/src/config.py +++ b/src/config.py @@ -3,28 +3,28 @@ import os class Config: + env_prefix = "OHM" + access_token: str + base_url: str + no_latex: bool def __init__(self): + if os.path.isfile("config/config.json"): + with open("config/config.json") as file: + self.config = json.load(file) - self.access_token = None + self.access_token = self.get_config_value("directus_api_key") + self.base_url = self.get_config_value("directus_api_url", "https://fragenkatalog.darc.de/") + self.no_latex = self.get_config_value("no_latex", False) - if os.path.isfile("config/config.json") : - with open ("config/config.json") as fh: - config_doc = json.load(fh) - self.access_token = config_doc["key"] - elif 'DIRECTUS_API_KEY' in os.environ.keys(): - self.access_token = os.environ['DIRECTUS_API_KEY'] + def get_config_value(self, key: str, default=None): + if key in self.config: + return self.config[key] + elif f"{self.env_prefix}_{key.upper()}" in os.environ.keys(): + return os.environ[f"{self.env_prefix}_{key.upper()}"] + elif default is not None: + return default else: - raise Exception("ERROR: Cannot find API key in config or environment!") - - if 'DIRECTUS_BASEURL' in os.environ.keys(): - self.base_url = os.environ['DIRECTUS_BASEURL'] - else: - self.base_url = "https://fragenkatalog.darc.de/" - - if not self.access_token: - raise Exception("ERROR: Cannot find API key in config or environment!") - - - - + raise Exception( + f"ERROR: Required value for '{key}' not found. Add to config.json or env as '${self.env_prefix}_{key.upper()}'" + )