forked from rabbit2rabbit/bili2.0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_rsp_ctrl.py
194 lines (162 loc) · 5.99 KB
/
json_rsp_ctrl.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
from enum import IntEnum
import attr
@attr.s(slots=True, frozen=True)
class In:
key = attr.ib(validator=attr.validators.instance_of(str))
value = attr.ib(
default=None,
validator=attr.validators.optional(attr.validators.instance_of(str)))
def match(self, dict_var: dict):
if self.key in dict_var:
value = dict_var[self.key]
if self.value is None:
return True
if isinstance(value, str):
return self.value in value
return False
@attr.s(slots=True, frozen=True)
class Equal:
key = attr.ib(validator=attr.validators.instance_of(str))
value = attr.ib()
def match(self, dict_var: dict):
return self.key in dict_var and self.value == dict_var[self.key]
@attr.s(slots=True, frozen=True)
class DeepIn:
keys = attr.ib(validator=attr.validators.deep_iterable(
member_validator=attr.validators.instance_of(str),
iterable_validator=None))
value = attr.ib(
default=None,
validator=attr.validators.optional(attr.validators.instance_of(str)))
def match(self, dict_var: dict):
tracking = dict_var
for key in self.keys:
if isinstance(tracking, dict) and key in tracking:
tracking = tracking[key]
else:
return False
if self.value is None:
return True
if isinstance(tracking, str):
return self.value in tracking
return False
@attr.s(slots=True, frozen=True)
class DeepEqual:
keys = attr.ib(validator=attr.validators.deep_iterable(
member_validator=attr.validators.instance_of(str),
iterable_validator=None))
value = attr.ib()
def match(self, dict_var: dict):
tracking = dict_var
for key in self.keys:
if isinstance(tracking, dict) and key in tracking:
tracking = tracking[key]
else:
return False
return self.value == tracking
# others内元素判断是AND,必须全部满足才match
@attr.s(slots=True, frozen=True)
class CtrlElem:
code = attr.ib(
default=None,
validator=attr.validators.optional(attr.validators.instance_of(int)))
others = attr.ib(
default=None,
validator=attr.validators.optional(attr.validators.deep_iterable(
member_validator=attr.validators.instance_of((Equal, In, DeepEqual, DeepIn)),
iterable_validator=None)))
def match(self, dict_var: dict) -> bool:
if self.code is not None and self.code != dict_var['code']:
return False
if self.others is None:
return True
for i in self.others:
if not i.match(dict_var):
return False
return True
class JsonRspType(IntEnum):
NONE = 0
OK = 1
LOGOUT = 2
IGNORE = 3
# 这里面的verifier(例如ok_verifiers有很多verifier)之间关系为OR,一个满足就可以了
@attr.s(slots=True, frozen=True)
class BaseCtrl:
# 可以直接返回
ok_verifiers = attr.ib(
default=None,
validator=attr.validators.optional(attr.validators.deep_iterable(
member_validator=attr.validators.instance_of(CtrlElem),
iterable_validator=None)))
# 忽略可以直接retry(1024、频繁等),建议sleep 1s
ignore_verifiers = attr.ib(
default=None,
validator=attr.validators.optional(attr.validators.deep_iterable(
member_validator=attr.validators.instance_of(CtrlElem),
iterable_validator=None)))
# raise登出
logout_verifiers = attr.ib(
default=None,
validator=attr.validators.optional(attr.validators.deep_iterable(
member_validator=attr.validators.instance_of(CtrlElem),
iterable_validator=None)))
def verify(self, dict_var: dict) -> JsonRspType:
if self.ok_verifiers is not None:
for verifier in self.ok_verifiers:
if verifier.match(dict_var):
return JsonRspType.OK
if self.ignore_verifiers is not None:
for verifier in self.ignore_verifiers:
if verifier.match(dict_var):
return JsonRspType.IGNORE
if self.logout_verifiers is not None:
for verifier in self.logout_verifiers:
if verifier.match(dict_var):
return JsonRspType.LOGOUT
return JsonRspType.NONE
# 一个全局的ctrl,所有的ctrl都要基于这个
DEFAULT_BASE_CTRL = BaseCtrl(
ignore_verifiers=[CtrlElem(code=1024)],
)
@attr.s(slots=True, frozen=True)
class Ctrl:
verifiers = attr.ib(
validator=attr.validators.instance_of(BaseCtrl))
# 是否支持全局的那个配置(1024之类的)
global_verifiers = attr.ib(
default=DEFAULT_BASE_CTRL,
validator=attr.validators.optional(
attr.validators.instance_of(BaseCtrl))
)
# 如果都不匹配该怎么办
default_result = attr.ib(
default=JsonRspType.IGNORE,
validator=attr.validators.instance_of(JsonRspType)
)
def verify(self, dict_var: dict) -> JsonRspType:
if self.global_verifiers is not None:
result = self.global_verifiers.verify(dict_var)
if result != JsonRspType.NONE:
return result
result = self.verifiers.verify(dict_var)
return result if result != JsonRspType.NONE else self.default_result
DEFAULT_CTRL = Ctrl(
verifiers=DEFAULT_BASE_CTRL,
global_verifiers=None,
default_result=JsonRspType.OK
)
# TODO: delete
# 这是为了过渡上一代的(code == 3 or code == -401 or code == 1003 or code == -101 or code == 401)这样的
TMP_DEFAULT_CTRL = Ctrl(
verifiers=BaseCtrl(
logout_verifiers=[
CtrlElem(code=3),
CtrlElem(code=-401),
CtrlElem(code=1003),
CtrlElem(code=-101),
CtrlElem(code=401),
# CtrlElem(code=-500),
]),
global_verifiers=DEFAULT_BASE_CTRL,
default_result=JsonRspType.OK
)