-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
39 lines (32 loc) · 900 Bytes
/
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
# -*- coding:utf-8 -*-
"""
基础配置文件
***重要***: 不依赖项目内任何模块
"""
import json
class ConfigFile(object):
FileName = '/config.json'
@staticmethod
def load():
try:
with open(ConfigFile.FileName, 'r') as f:
config = json.loads(f.read() or '{}')
return config
except OSError:
return {}
@staticmethod
def get(*args, **kwargs):
config = ConfigFile.load()
return config.get(*args, **kwargs)
@staticmethod
def set(key, value):
config = ConfigFile.load()
config[key] = value
with open(ConfigFile.FileName, 'w') as f:
f.write(json.dumps(config))
if __name__ == '__main__':
print(ConfigFile.get('a'))
ConfigFile.set('a', True)
print(ConfigFile.get('a'))
ConfigFile.set('a', False)
print(ConfigFile.get('a'))