99from botkit .core .modules import Module
1010from botkit .routing .route import RouteHandler
1111from botkit .routing .update_types .updatetype import UpdateType
12+ from botkit .types .client import IClient
1213
1314"""
1415Indicates where the evaluation of individual updates takes place
@@ -24,7 +25,7 @@ class BotkitDispatcher:
2425 def __init__ (self ):
2526 self .callback_action_dispatchers : Dict [Client , CallbackActionDispatcher ] = dict ()
2627 self ._inline_query_factory : Any = None
27- self ._module_handlers : Dict [int , Dict [Client , List [Handler ]]] = dict ()
28+ self .module_handlers : Dict [int , Dict [Client , List [Handler ]]] = dict ()
2829
2930 self .log = logzero .setup_logger (BotkitDispatcher .__name__ )
3031
@@ -34,7 +35,9 @@ async def add_module_routes(self, module: Module):
3435 for client , routes in module .route_collection .routes_by_client .items ():
3536 for route in routes :
3637 for update_type , route_wrapper in route .handler_by_update_type .items ():
37- await self .add_route_for_update_type (module , client , update_type , route_wrapper )
38+ await self .add_route_for_update_type (
39+ module , client , update_type , route_wrapper
40+ )
3841
3942 """
4043 TODO: split this up into:
@@ -52,7 +55,8 @@ async def add_module_routes(self, module: Module):
5255 """
5356
5457 self .log .info (
55- f"({ module .group_index } ) { module .get_name ()} loaded" + (" with: " + ", " .join (log_msg ) if log_msg else "" )
58+ f"({ module .group_index } ) { module .get_name ()} loaded"
59+ + (" with: " + ", " .join (log_msg ) if log_msg else "" )
5660 )
5761
5862 async def add_route_for_update_type (
@@ -89,14 +93,14 @@ async def add_route_for_update_type(
8993 async def remove_module_routes (self , module : Module ):
9094 group = module .group_index
9195
92- for client , h in self ._module_handlers [group ].items ():
96+ for client , h in self .module_handlers [group ].items ():
9397 for handler in h :
9498 try :
9599 client .remove_handler (handler , group )
96100 except Exception :
97101 self .log .exception (f"Could not remove handler { handler } from group { group } ." )
98102
99- del self ._module_handlers [group ]
103+ del self .module_handlers [group ]
100104
101105 async def add_handler (self , group : int , client : Client , handler : Handler ):
102106 assert group is not None
@@ -106,16 +110,21 @@ async def add_handler(self, group: int, client: Client, handler: Handler):
106110 async with client .dispatcher .locks_list [- 1 ]:
107111 client .add_handler (handler , group )
108112
109- self ._module_handlers .setdefault (group , {})
110- self ._module_handlers [group ].setdefault (client , [])
111- self ._module_handlers [group ][client ].append (handler )
113+ self .module_handlers .setdefault (group , {})
114+ self .module_handlers [group ].setdefault (client , [])
115+ self .module_handlers [group ][client ].append (handler )
112116
113117 def is_registered (self , module : Module ) -> bool :
114- return module .group_index in self ._module_handlers
118+ return module .group_index in self .module_handlers
115119
116120 async def _get_or_create_action_dispatcher (self , client ) -> CallbackActionDispatcher :
121+
117122 if not (action_dispatcher := self .callback_action_dispatchers .get (client )):
118- self .callback_action_dispatchers [client ] = (action_dispatcher := CallbackActionDispatcher ())
123+ self .callback_action_dispatchers [client ] = (
124+ action_dispatcher := CallbackActionDispatcher ()
125+ )
126+
119127 # All callback queries use the same group (only one of them applies for a given update)
120128 await self .add_handler (0 , client , action_dispatcher .pyrogram_handler )
129+
121130 return action_dispatcher
0 commit comments