-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
51 lines (42 loc) · 1.25 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
import json
import os
from common.jsonn import JsonSerializable
from common.paths import get_data_root
class Config(JsonSerializable):
def __init__(self):
self.server_setting = ServerSetting()
def load_dict(self, dic: dict):
super(Config, self).load_dict(dic)
content = dic.get('server_setting', None)
if content:
setting = ServerSetting()
setting.load_dict(content)
self.server_setting = setting
@classmethod
def load(cls):
root = get_data_root()
path = os.path.join(root, 'config.json')
dic = {}
if os.path.exists(path):
with open(path, 'r') as f:
dic = json.load(f)
result = Config()
result.load_dict(dic)
return result
def save(self):
root = get_data_root()
path = os.path.join(root, 'config.json')
if not os.path.exists(root):
os.makedirs(root)
with open(path, 'w') as f:
js = self.dump_json()
f.write(js)
class ServerSetting(JsonSerializable):
def __init__(self):
self.host = '0.0.0.0'
self.port = 10000
if __name__ == '__main__':
conf = Config()
conf.save()
conf = Config.load()
pass