-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathconfig.py
69 lines (59 loc) · 1.84 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
# coding: utf-8
import json
import os
import utils as u
from jsonc_parser.parser import JsoncParser as jsonp
def initJson():
'''
初始化配置 (从 example.jsonc 加载)
'''
# try:
# jsonData = jsonp.parse_file('example.jsonc', encoding='utf-8')
# with open('config.json', 'w+', encoding='utf-8') as file:
# json.dump(jsonData, file, indent=4, ensure_ascii=False)
# u.info('Generated new config file (config.json), please edit and re-run this program.')
# u.info('Example: example.jsonc / Online: https://github.com/wyf9/sleepy/blob/main/example.jsonc')
# except:
# u.error('Create config.json failed')
# raise
class config:
'''
config 类, 负责配置调用
可用 `.config['xxx']` 直接调取数据 (加载后)
'''
config: dict
def __init__(self):
if not os.path.exists('config.json'):
u.warning('config.json not exist, creating')
initJson()
self.loads()
def load(self):
pass
def loads(self):
'''
加载配置
'''
with open('config.json', 'r', encoding='utf-8') as file:
self.config = json.load(file)
# def save(self):
# '''
# 保存配置
# '''
# with open('config.json', 'w+', encoding='utf-8') as file:
# json.dump(self.data, file, indent=4, ensure_ascii=False)
# def dset(self, name, value):
# '''
# 设置一个值
# '''
# self.data[name] = value
# with open('config.json', 'w+', encoding='utf-8') as file:
# json.dump(self.data, file, indent=4, ensure_ascii=False)
def get(self, name):
'''
读取一个值
'''
try:
gotdata = self.config[name]
except:
gotdata = None
return gotdata