-
Notifications
You must be signed in to change notification settings - Fork 51
/
config_loader.py
81 lines (60 loc) · 2.47 KB
/
config_loader.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
70
71
72
73
74
75
76
77
78
79
80
from matplotlib import colors
import toml
# "#969696"
def hex_to_rgb_percent(hex_str):
rgb_pct_color = colors.hex2color(hex_str)
return rgb_pct_color
# "255 255 255"
def rgb_to_percent(rgb_list):
hex_color = f'#{rgb_list[0]:02x}{rgb_list[1]:02x}{rgb_list[2]:02x}'
# hex_1 = '#%02x%02x%02x' % (rgb_0[0], rgb_0[1], rgb_0[2])
rgb_pct_color = colors.hex2color(hex_color)
return rgb_pct_color
class ConfigLoader():
inst = None
def __new__(cls, file_color=None, file_user=None, file_bili=None, file_ip=None):
if not cls.inst:
cls.inst = super(ConfigLoader, cls).__new__(cls)
cls.inst.file_color = file_color
# cls.inst.dict_color = cls.inst.read_color()
# print(cls.inst.dict_color)
cls.inst.file_user = file_user
# cls.inst.dict_user = cls.inst.read_user()
# print(cls.inst.dict_user)
cls.inst.file_bili = file_bili
# cls.inst.dict_bili = cls.inst.read_bili()
# print(cls.inst.dict_bili)
# print("# 初始化完成")
cls.inst.file_ip = file_ip
# cls.inst.dict_ip = cls.inst.read_ip()
# print(cls.inst.dict_ip)
return cls.inst
def write_user(self, dict_new, user_id):
with open(self.file_user, encoding="utf-8") as f:
dict_user = toml.load(f)
for i, value in dict_new.items():
dict_user['users'][user_id][i] = value
with open(self.file_user, 'w', encoding="utf-8") as f:
toml.dump(dict_user, f)
def read_bili(self):
with open(self.file_bili, encoding="utf-8") as f:
dict_bili = toml.load(f)
return dict_bili
def read_ip(self):
with open(self.file_ip, encoding="utf-8") as f:
dict_ip = toml.load(f)
return dict_ip
def read_color(self):
with open(self.file_color, encoding="utf-8") as f:
dict_color = toml.load(f)
for i in dict_color.values():
for j in i.keys():
if isinstance(i[j], str):
i[j] = hex_to_rgb_percent(i[j])
else:
i[j] = rgb_to_percent(i[j])
return dict_color
def read_user(self):
with open(self.file_user, encoding="utf-8") as f:
dict_user = toml.load(f)
return dict_user