forked from spookybear0/laserforce_ranking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
60 lines (47 loc) · 1.6 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
import json
import os
path = os.path.dirname(os.path.realpath(__file__))
default_config = {
"db_host": "localhost",
"db_user": "root",
"db_password": "",
"db_port": 3306,
"db_database": "laserforce",
"sentry_dsn": "",
"sentry_environment": "production",
"redis": "redis://localhost",
}
config_options = list(default_config.keys())
class JsonFile:
def __init__(self, file_name: str) -> None:
self.file = None
self.file_name = file_name
if os.path.exists(file_name):
with open(file_name) as f:
self.file = json.load(f)
def get_file(self) -> dict:
"""Returns the loaded JSON file as a dict."""
return self.file
def write_file(self, new_content: dict) -> None:
"""Writes a new dict to the file."""
with open(self.file_name, "w") as f:
json.dump(new_content, f, indent=4)
self.file = new_content
jconfig = JsonFile(path + "/config.json")
config = jconfig.get_file()
if config is None:
print("Generating new config")
jconfig.write_file(default_config)
print("Generated new config! Please edit it and restart laserforce_ranking.")
raise SystemExit
# check for config updates
config_keys = list(config.keys())
updated_conf = False
for def_conf_option in config_options:
if def_conf_option not in config_keys:
updated_conf = True
config[def_conf_option] = default_config[def_conf_option]
if updated_conf:
jconfig.write_file(config)
print("Your config has been updated! Please change the new vaulues to your liking.")
raise SystemExit