This repository has been archived by the owner on Jul 2, 2019. It is now read-only.
forked from Dawnnnnnn/bilibili-live-tools
-
Notifications
You must be signed in to change notification settings - Fork 23
/
configloader.py
96 lines (65 loc) · 3.01 KB
/
configloader.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
92
93
94
import toml
# "#969696"
def hex_to_rgb_percent(hex_str):
return tuple(int(n, 16)/255 for n in (hex_str[1:3], hex_str[3:5], hex_str[5:7]))
# [255 255 255]
def rgb_to_percent(rgb_list):
return rgb_list[0]/255, rgb_list[1]/255, rgb_list[2]/255
class ConfigLoader():
instance = None
def __new__(cls, fileDir=None):
if not cls.instance:
cls.instance = super(ConfigLoader, cls).__new__(cls)
colorfile = f'{fileDir}/conf/color.toml'
userfile = f'{fileDir}/conf/user.toml'
bilibilifile = f'{fileDir}/conf/bilibili.toml'
titlefile = f'{fileDir}/conf/title.toml'
cls.instance.colorfile = colorfile
cls.instance.dic_color = cls.instance.load_color()
# print(cls.instance.dic_color)
cls.instance.userfile = userfile
cls.instance.dic_user = cls.instance.load_user()
# print(cls.instance.dic_user)
cls.instance.bilibilifile = bilibilifile
cls.instance.dic_bilibili = cls.instance.load_bilibili()
# print(cls.instance.dic_bilibili)
cls.instance.titlefile = titlefile
cls.instance.dic_title = cls.instance.load_title()
print("# 初始化完成")
return cls.instance
def write2bilibili(self, dic):
with open(self.bilibilifile, encoding="utf-8") as f:
dic_bilibili = toml.load(f)
for i in dic.keys():
dic_bilibili['saved-session'][i] = dic[i]
with open(self.bilibilifile, 'w', encoding="utf-8") as f:
toml.dump(dic_bilibili, f)
def load_bilibili(self):
with open(self.bilibilifile, encoding="utf-8") as f:
dic_bilibili = toml.load(f)
if not dic_bilibili['account']['username']:
username = input("# 输入帐号: ")
password = input("# 输入密码: ")
dic_bilibili['account']['username'] = username
dic_bilibili['account']['password'] = password
with open(self.bilibilifile, 'w', encoding="utf-8") as f:
toml.dump(dic_bilibili, f)
return dic_bilibili
def load_color(self):
with open(self.colorfile, encoding="utf-8") as f:
dic_color = toml.load(f)
for i in dic_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 dic_color
def load_user(self):
with open(self.userfile, encoding="utf-8") as f:
dic_user = toml.load(f)
return dic_user
def load_title(self):
with open(self.titlefile, encoding="utf-8") as f:
dic_title = toml.load(f)
return dic_title