This repository has been archived by the owner on Jan 20, 2024. It is now read-only.
/
config.py
70 lines (59 loc) · 2.16 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
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
import os
import sys
from json import load
class Config:
database: str = ""
loglevel: int = 0
allow_signup: bool = False
domain: str = "*"
config = Config()
configPaths = ["config.json"]
if os.name == "posix":
configPaths.append("/etc/amber.json")
# search for every file name and load the config from the first file that exists
for testedFileName in configPaths:
if os.path.isfile(testedFileName):
# holds the actual config file name
configFileName = testedFileName
break
if not "configFileName" in globals():
print("No configuration file found, exiting")
sys.exit(1)
try:
with open(configFileName, encoding="utf8") as configFile:
# holds the new configuration variables which replace the
# default ones
loadedConfig = load(configFile)
except OSError as ioerr:
print("Could not open config file", configFileName)
print(ioerr.strerror)
sys.exit(1)
for entry in dir(config):
if entry in loadedConfig and not entry.startswith("__"):
setattr(config, entry, loadedConfig[entry])
def string_to_bool(val: str) -> bool:
"""
Converts a string containing a bool value to Python's bool. Serves as a
helper in configuration code.
"""
if val == "1" or val.lower() == "true":
return True
if val == "0" or val.lower() == "false":
return False
return False
# override config with environment variables in need, the first element of a
# tuple is the environment variable itself, the second is the corresponding
# `config` key, and the third one is the function to convert the possible values
for mapping in (
("AMBER_DATABASE", "database", lambda val: val), # str -> str
# pylint: disable=unnecessary-lambda
("AMBER_LOGLEVEL", "loglevel", lambda val: int(val)), # str -> int
("AMBER_ALLOW_SIGNUP", "allow_signup", string_to_bool), # str -> bool
("AMBER_DOMAIN", "domain", lambda val: val) # str -> str
):
env_value = os.getenv(mapping[0])
if not env_value is None:
setattr(config, mapping[1], mapping[2](env_value))
if not config.database:
print("No database specified. Exiting.")
sys.exit(1)