-
Notifications
You must be signed in to change notification settings - Fork 2
/
publictool.py
145 lines (116 loc) · 3.68 KB
/
publictool.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
'''lupro 工具箱'''
from .typing import Any , Response
from .config import PERSISTENCE_ENABLED, PERSISTENCE_PATH
from decorator import decorator
import shelve
import chardet
# 泛属性元类
class inherit(type):
'''类泛属性'''
def __getattr__(cls, name):
if not '__general__' in dir(cls):
raise NameError("name '__general__' is not defined")
if hasattr(cls.__general__, name):
return getattr(cls.__general__, name)
return getattr(object.__getattr__, name)
# 对象持久化控制器
class persistence():
'''对象持久化控制器'''
'''是否启用对象持久化'''
ENABLED = PERSISTENCE_ENABLED
# 对象持久化元类
class kernelk(type):
'''对象持久化元类'''
pass
# shelve内核
class shelve():
'''shelve内核'''
'''对象持久化存储路径'''
dbfile = PERSISTENCE_PATH
@classmethod
def add(cls, key : str, value : Any) -> bool:
'''持久化一个新对象
Args:
`cls` : `persistence.shelve` 类对象
`key` : `str` 新增对象的键
`value` : `Any` 新增对象的值
Returns:
`bool` : True
'''
with shelve.open(persistence.shelve.dbfile) as f:
f[key] = value
return True
@classmethod
def put(cls):
'''持久化一个新对象
Args:
`cls` : `persistence.shelve` 类对象
Returns:
`shelve` : 当前路径shelve对象字典
'''
with shelve.open(persistence.shelve.dbfile) as f:
res = {i:j for i,j in f.items()}
return res
# ZODB内核
class ZODB():
'''ZODB内核'''
pass
# 辅助函数
def original(f) -> Any:
''' 默认解析处理函数.
Args:
`f` : function 自定义解析处理函数
Retuens:
function : 自定义解析处理函数
'''
return f
# 函数功能启停装饰器
@decorator
def reconfig(func, config = True, *args, **kwargs):
'''函数功能启停装饰器'''
if config:
return func(*args , **kwargs)
else:
return None
# 对象持久化装饰器
@decorator
def endurance(func, *args, **kwargs):
'''对象持久化装饰器'''
res = func(*args, **kwargs)
if persistence.ENABLED:
persistence.shelve.add(args[0].filename, res)
return res
# 异常处理装饰器
@decorator
def abnormal(func, *args, **kwargs):
'''异常处理装饰器'''
try:
return func(*args, **kwargs)
except KeyboardInterrupt:
pass
# 回调空键默认值
def putdefault(value, default) -> Any:
'''与dict.setdefault相识,但是不赋值.
Args:
`value` : `Any` 字典键值
`default` : `Any` 返回的默认值
Returns:
`Any` : 默认值`default`
'''
if value:
return value
else:
return default
# bytes编码解析
def bytescoding(response : bytes) -> str:
'''bytes编码解析'''
dend = chardet.detect(response)
return response.decode(encoding = dend['encoding'])
# response编码解析
def responsecoding(response : Response) -> str:
'''response编码解析'''
if response.apparent_encoding == None:
return bytescoding(response.content)
else:
response.encoding = response.apparent_encoding
return response.content.decode(response.encoding, 'ignore')