-
Notifications
You must be signed in to change notification settings - Fork 18
/
json_rsp_ctrl.py
103 lines (80 loc) · 2.61 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
import enum
import attr
import pampy
@enum.unique
class JsonRspType(enum.IntEnum):
UNDEFINED = 0
OK = 1
LOGOUT = 2
IGNORE = 3
class In:
__slots__ = ('_value',)
def __init__(self, value):
self._value = value
def __call__(self, value):
return self._value in value
def patterns_actions(_, __, value):
if isinstance(value, (tuple, list)):
if not len(value) % 2:
iterator = iter(value)
for pattern, action in zip(iterator, iterator):
if not isinstance(action, JsonRspType):
raise ValueError(f'action 必须为 JsonRspType({action})')
return
raise ValueError('必须 pattern、action 配对')
raise ValueError('必须是 tuple 或 list')
BASE_CTRL = (
{'code': 0}, JsonRspType.OK, # 目前为止,0 肯定成功,如果例外,自己另写
{'code': 1024}, JsonRspType.IGNORE,
{'msg': In('操作太快')}, JsonRspType.IGNORE,
{'msg': In('繁忙')}, JsonRspType.IGNORE,
{'msg': In('频繁')}, JsonRspType.IGNORE,
{'message': In('繁忙')}, JsonRspType.IGNORE,
{'message': In('频繁')}, JsonRspType.IGNORE,
{'msg': In('登录')}, JsonRspType.LOGOUT,
{'msg': In('login')}, JsonRspType.LOGOUT,
)
@attr.s(slots=True, frozen=True)
class Ctrl:
extend = attr.ib(
validator=attr.validators.optional(patterns_actions))
# 是否支持全局的那个配置(1024之类的)
base = attr.ib(
default=BASE_CTRL,
validator=attr.validators.optional(patterns_actions)
)
# 如果都不匹配该怎么办
default = attr.ib(
default=JsonRspType.IGNORE,
validator=attr.validators.instance_of(JsonRspType)
)
def verify(self, dict_var: dict) -> JsonRspType:
if self.base is not None:
result = pampy.match(dict_var, *self.base, default=JsonRspType.UNDEFINED)
if result != JsonRspType.UNDEFINED:
return result
if self.extend is not None:
result = pampy.match(dict_var, *self.extend, default=JsonRspType.UNDEFINED)
if result != JsonRspType.UNDEFINED:
return result
return self.default
DEFAULT_CTRL = Ctrl(
extend=None,
base=BASE_CTRL,
default=JsonRspType.OK
)
ZERO_ONLY_CTRL = Ctrl(
extend=(
{'code': 0}, JsonRspType.OK,
),
base=None,
default=JsonRspType.IGNORE
)
# 经观察不少情况下 -101 表示未登录({"code":-101},没有其他东西)
LOGOUT_101_CTRL = Ctrl(
extend=(
{'code': -101}, JsonRspType.LOGOUT,
),
base=BASE_CTRL,
default=JsonRspType.OK
)