-
Notifications
You must be signed in to change notification settings - Fork 1
/
conf.py
46 lines (39 loc) · 1.1 KB
/
conf.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
import json
import os
def read_config_from_file():
with open("conf.json", "r") as f:
str_conf = f.read()
if str_conf == "" or str_conf == "{}":
raise EmptyConf()
conf = json.loads(str_conf)
return conf
def read_config_from_env():
try:
conf = {
"database": {
"host": os.environ["MYSQL_HOST"],
"user": os.environ["MYSQL_USER"],
"password": os.environ["MYSQL_PASSWORD"],
"database": os.environ["MYSQL_DATABASE"],
"root": "root",
"root_pwd": os.environ["MYSQL_ROOT_PASSWORD"]
}
}
except KeyError:
raise EmptyConf
return conf
def init():
global conf
conf = read_config_from_env()
#conf = read_config_from_file()
def api(parameter):
global conf
return conf["api"][parameter]
def database(parameter):
global conf
print(conf, conf["database"])
return conf["database"][parameter]
class EmptyConf(Exception):
def __init__(self):
super().__init__("Configuration file is empty")
conf = ""