feat: improve configuration loader
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
{
|
{
|
||||||
"key" : "INSERTKEYHERE"
|
"directus_base_url" : "https://fragenkatalog.darc.de/",
|
||||||
|
"no_latex": true
|
||||||
}
|
}
|
||||||
|
|||||||
+20
-20
@@ -3,28 +3,28 @@ import os
|
|||||||
|
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
|
env_prefix = "OHM"
|
||||||
|
access_token: str
|
||||||
|
base_url: str
|
||||||
|
no_latex: bool
|
||||||
|
|
||||||
def __init__(self):
|
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") :
|
def get_config_value(self, key: str, default=None):
|
||||||
with open ("config/config.json") as fh:
|
if key in self.config:
|
||||||
config_doc = json.load(fh)
|
return self.config[key]
|
||||||
self.access_token = config_doc["key"]
|
elif f"{self.env_prefix}_{key.upper()}" in os.environ.keys():
|
||||||
elif 'DIRECTUS_API_KEY' in os.environ.keys():
|
return os.environ[f"{self.env_prefix}_{key.upper()}"]
|
||||||
self.access_token = os.environ['DIRECTUS_API_KEY']
|
elif default is not None:
|
||||||
|
return default
|
||||||
else:
|
else:
|
||||||
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()}'"
|
||||||
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!")
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user