-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathdata_struct.py
198 lines (155 loc) · 6.47 KB
/
data_struct.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
from __future__ import annotations
import json
from abc import ABCMeta, abstractmethod
from Crypto.Cipher import AES
from log import logger
class Object:
def __init__(self, fromDict=None):
if fromDict is None:
fromDict = {}
self.__dict__ = fromDict
def __str__(self):
return str(self.__dict__)
class AESCipher:
BLOCK_SIZE = 16 # Bytes
def __init__(self, key):
# 加密需要的key值
self.key = key.encode()
def encrypt(self, raw):
raw = self.pad(raw)
# 通过key值,使用ECB模式进行加密
cipher = AES.new(self.key, AES.MODE_ECB)
# 返回得到加密后的字符串
return cipher.encrypt(raw.encode())
def decrypt(self, enc):
# 通过key值,使用ECB模式进行解密
cipher = AES.new(self.key, AES.MODE_ECB)
return self.unpad(cipher.decrypt(enc)).decode("utf8")
# Padding for the input string --not related to encryption itself.
def pad(self, s):
return s + (self.BLOCK_SIZE - len(s) % self.BLOCK_SIZE) * chr(self.BLOCK_SIZE - len(s) % self.BLOCK_SIZE)
def unpad(self, s):
return s[: -ord(s[len(s) - 1 :])]
# 如果配置的值是dict,可以用ConfigInterface自行实现对应结构,将会自动解析
# 如果配置的值是list/set/tuple,则需要实现ConfigInterface,同时重写auto_update_config,在调用过基类的该函数后,再自行处理这三类结果
class ConfigInterface(metaclass=ABCMeta):
@abstractmethod
def __init__(self):
pass
def auto_update_config(self, raw_config: dict):
if type(raw_config) is not dict:
logger.warning(f"raw_config={raw_config} is not dict")
else:
for key, val in raw_config.items():
if hasattr(self, key):
attr = getattr(self, key)
if isinstance(attr, ConfigInterface):
config_field: ConfigInterface = attr
config_field.auto_update_config(val)
else:
setattr(self, key, val)
# 尝试填充一些数组元素
self.fill_array_fields(raw_config, self.fields_to_fill())
# 尝试填充一些字典元素
self.fill_dict_fields(raw_config, self.dict_fields_to_fill())
# re: 以后有需求的时候再增加处理set、tuple等
# 调用可能存在的回调
self.on_config_update(raw_config)
# 最终返回自己,方便链式调用
return self
def load_from_json_file(self, filepath: str):
with open(filepath, encoding="utf-8") as f:
raw_config = json.load(f)
if type(raw_config) is not dict:
logger.warning(f"raw_config={raw_config} load from {filepath} is not dict")
return
return self.auto_update_config(raw_config)
def save_to_json_file(self, filepath: str, ensure_ascii=False, indent=2):
with open(filepath, "w", encoding="utf-8") as save_file:
json.dump(to_raw_type(self), save_file, ensure_ascii=ensure_ascii, indent=indent)
def fill_array_fields(self, raw_config: dict, fields_to_fill: list[tuple[str, type[ConfigInterface]]]):
for field_name, field_type in fields_to_fill:
if field_name in raw_config:
if raw_config[field_name] is None:
setattr(self, field_name, [])
continue
if type(raw_config[field_name]) is list:
setattr(
self, field_name, [field_type().auto_update_config(item) for item in raw_config[field_name]]
)
def fields_to_fill(self) -> list[tuple[str, type[ConfigInterface]]]:
return []
def fill_dict_fields(self, raw_config: dict, fields_to_fill: list[tuple[str, type[ConfigInterface]]]):
for field_name, field_type in fields_to_fill:
if field_name in raw_config:
if raw_config[field_name] is None:
setattr(self, field_name, {})
continue
if type(raw_config[field_name]) is dict:
setattr(
self,
field_name,
{key: field_type().auto_update_config(val) for key, val in raw_config[field_name].items()},
)
def dict_fields_to_fill(self) -> list[tuple[str, type[ConfigInterface]]]:
return []
def on_config_update(self, raw_config: dict):
return
def __str__(self):
return json.dumps(to_raw_type(self), ensure_ascii=False)
def to_raw_type(v):
if isinstance(v, ConfigInterface):
return {sk: to_raw_type(sv) for sk, sv in v.__dict__.items()}
elif isinstance(v, list):
return list(to_raw_type(sv) for sk, sv in enumerate(v))
elif isinstance(v, tuple):
return tuple(to_raw_type(sv) for sk, sv in enumerate(v))
elif isinstance(v, set):
return {to_raw_type(sv) for sk, sv in enumerate(v)}
elif isinstance(v, dict):
return {sk: to_raw_type(sv) for sk, sv in v.items()}
else:
return v
def test():
class TestSubConfig(ConfigInterface):
def __init__(self):
self.val = 0
class TestConfig(ConfigInterface):
def __init__(self):
self.int_val = 0
self.str_val = ""
self.bool_val = False
self.list_int: list[int] = []
self.list_sub_config: list[TestSubConfig] = []
self.dict_str_str: dict[str, str] = {}
self.dict_str_sub_config: dict[str, TestSubConfig] = {}
def fields_to_fill(self) -> list[tuple[str, type[ConfigInterface]]]:
return [("list_sub_config", TestSubConfig)]
def dict_fields_to_fill(self) -> list[tuple[str, type[ConfigInterface]]]:
return [("dict_str_sub_config", TestSubConfig)]
test_raw_config = {
"int_val": 1,
"str_val": "test",
"bool_val": True,
"list_int": [1, 2, 3],
"list_sub_config": [
{"val": 1},
{"val": 2},
{"val": 3},
],
"dict_str_str": {
"1": "2",
"2": "4",
"3": "6",
},
"dict_str_sub_config": {
"1": {"val": 1},
"2": {"val": 2},
"3": {"val": 3},
},
}
test_config = TestConfig().auto_update_config(test_raw_config)
print(test_raw_config)
print(test_config)
if __name__ == "__main__":
test()