-
Notifications
You must be signed in to change notification settings - Fork 0
/
packages.py
84 lines (67 loc) · 2.33 KB
/
packages.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
async def NULL_HOOK(package):
pass
class BasePackage:
def __init__(self, core, hook: callable):
self.core = core
self.hook = hook
self.data = {}
self.completed = False
async def run_hook(self):
if not self.completed:
await self.hook(self)
self.completed = True
class TextPackage(BasePackage):
def __init__(self, input_text, core, hook, for_filter=None):
super().__init__(core, hook)
self.input_text = input_text
self.for_filter = for_filter or input_text
@property
def text(self):
if "text" in self.data:
return self.data["text"]
return None
@text.setter
def text(self, value):
if isinstance(value, str):
self.data["text"] = value
else:
raise TypeError(f".text must be str, got {type(value)}")
class TelegramAnswerPackage(TextPackage):
def __init__(self, input_text, core, answer: callable, for_filter=None):
self.answer = answer
super(TelegramAnswerPackage, self).__init__(input_text, core, for_filter=for_filter)
class HookExtendPackage(BasePackage):
"""
Расширяет вызов основного крюка двумя вызовами перед и после основного.
"""
def __init__(self, input_text, core, hook):
super().__init__(core, hook)
self.input_text = input_text
@property
def pre_hook(self):
if "pre_hook" in self.data:
return self.data["pre_hook"]
return None
@pre_hook.setter
def pre_hook(self, value):
if isinstance(value, callable):
self.data["pre_hook"] = value
else:
raise TypeError(f".pre_hook must be callable, got {type(value)}")
@property
def post_hook(self):
if "post_hook" in self.data:
return self.data["post_hook"]
return None
@post_hook.setter
def post_hook(self, value):
if isinstance(value, callable):
self.data["post_hook"] = value
else:
raise TypeError(f".post_hook must be callable, got {type(value)}")
async def run_hook(self):
if not self.completed:
await self.pre_hook(self)
await self.hook(self)
await self.post_hook(self)
self.completed = True