forked from rabbit2rabbit/bili2.0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conf_loader.py
92 lines (64 loc) · 2.45 KB
/
conf_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
81
82
83
84
85
86
87
88
89
90
91
from os import path
import toml
# "#969696"
def hex_to_rgb_percent(str_hex):
return tuple(int(n, 16)/255 for n in (str_hex[1:3], str_hex[3:5], str_hex[5:7]))
# [255 255 255]
def rgb_to_percent(list_rgb):
return list_rgb[0]/255, list_rgb[1]/255, list_rgb[2]/255
class ConfLoader():
def __init__(self):
path_conf = f'{path.dirname(path.realpath(__file__))}/conf'
self.file_color = f'{path_conf}/color.toml'
self.file_user = f'{path_conf}/user.toml'
self.file_bili = f'{path_conf}/bili.toml'
self.file_ctrl = f'{path_conf}/ctrl.toml'
'''
self.dict_color = self.read_color()
print(self.dict_color)
self.dict_user = self.read_user()
print(self.dict_user)
self.dict_bili = self.read_bili()
print(self.dict_bili)
print("# 初始化完成")
'''
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_color(self):
with open(self.file_color, encoding="utf-8") as f:
dict_color = toml.load(f)
for i in dict_color.values():
for key, color in i.items():
if isinstance(color, str):
i[key] = hex_to_rgb_percent(color)
elif isinstance(color, list):
i[key] = rgb_to_percent(color)
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
def read_ctrl(self):
with open(self.file_ctrl, encoding="utf-8") as f:
dict_ctrl = toml.load(f)
return dict_ctrl
var = ConfLoader()
def write_user(dict_new, user_id):
var.write_user(dict_new, user_id)
def read_bili():
return var.read_bili()
def read_color():
return var.read_color()
def read_user():
return var.read_user()
def read_ctrl():
return var.read_ctrl()