-
Notifications
You must be signed in to change notification settings - Fork 0
/
patch_dispatcher.py
75 lines (61 loc) · 2.72 KB
/
patch_dispatcher.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
import sys
import inspect
import traceback
from logger.logger import log, LogMode
import pyrogram
from pyrogram.handlers import RawUpdateHandler
def exception_hook(exc_type, exc_value, tb):
res = ""
for line in traceback.TracebackException(exc_type, exc_value, tb, limit=None).format(chain=True):
res += line
log(res.strip(), LogMode.ERROR)
class PatchedDispatcher:
async def handler_worker(self, lock):
while True:
packet = await self.updates_queue.get()
if packet is None:
break
try:
update, users, chats = packet
parser = self.update_parsers.get(type(update), None)
parsed_update, handler_type = (
await parser(update, users, chats)
if parser is not None
else (None, type(None))
)
async with lock:
for group in self.groups.values():
for handler in group:
args = None
if isinstance(handler, handler_type):
try:
if await handler.check(self.client, parsed_update):
args = (parsed_update,)
except Exception as e:
exception_hook(*sys.exc_info())
continue
elif isinstance(handler, RawUpdateHandler):
args = (update, users, chats)
if args is None:
continue
try:
if inspect.iscoroutinefunction(handler.callback):
await handler.callback(self.client, *args)
else:
await self.loop.run_in_executor(
self.client.executor,
handler.callback,
self.client,
*args
)
except pyrogram.StopPropagation:
raise
except pyrogram.ContinuePropagation:
continue
except Exception as e:
exception_hook(*sys.exc_info())
break
except pyrogram.StopPropagation:
pass
except Exception as e:
exception_hook(*sys.exc_info())