-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathconf_loader.py
84 lines (57 loc) · 2.15 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
from os import path
import toml
import rsa
class ConfLoader:
def __init__(self):
root_path = path.dirname(path.realpath(__file__))
path_conf = f'{root_path}/conf'
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.file_roomid = f'{path_conf}/roomid.toml'
self.key_path = f'{root_path}/key/admin_privkey.pem'
self.admin_pubkey_path = f'{root_path}/key/admin_pubkey.pem'
@staticmethod
def toml_load(path):
with open(path, encoding="utf-8") as f:
return toml.load(f)
@staticmethod
def toml_dump(object, path):
with open(path, 'w', encoding="utf-8") as f:
toml.dump(object, f)
def write_user(self, dict_new, user_id):
dict_user = self.toml_load(self.file_user)
for i, value in dict_new.items():
dict_user['users'][user_id][i] = value
self.toml_dump(dict_user, self.file_user)
def read_bili(self):
return self.toml_load(self.file_bili)
def read_user(self):
return self.toml_load(self.file_user)
def read_ctrl(self):
return self.toml_load(self.file_ctrl)
def read_roomid(self):
return self.toml_load(self.file_roomid)
def read_key(self):
with open(self.key_path, 'rb') as f:
admin_privkey = rsa.PrivateKey.load_pkcs1(f.read())
return admin_privkey
def read_pubkey(self):
with open(self.admin_pubkey_path, 'rb') as f:
admin_pubkey = rsa.PublicKey.load_pkcs1(f.read())
return admin_pubkey
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_user():
return var.read_user()
def read_ctrl():
return var.read_ctrl()
def read_key():
return var.read_key()
def read_pubkey():
return var.read_pubkey()
def read_roomid():
return var.read_roomid()