21
21
22
22
def run_async (func ):
23
23
"""
24
- Function decorator that will run the function in a new thread.
24
+ Function decorator that will run the function in a new thread. A function
25
+ decorated with this will have to include **kwargs in their parameter list,
26
+ which will contain all optional parameters.
25
27
26
28
Args:
27
29
func (function): The function to run in the thread.
@@ -31,24 +33,24 @@ def run_async(func):
31
33
"""
32
34
33
35
@wraps (func )
34
- def pooled (* args , ** kwargs ):
36
+ def pooled (* pargs , ** kwargs ):
35
37
"""
36
38
A wrapper to run a thread in a thread pool
37
39
"""
38
40
global running_async , async_lock
39
- result = func (* args , ** kwargs )
41
+ result = func (* pargs , ** kwargs )
40
42
semaphore .release ()
41
43
with async_lock :
42
44
running_async -= 1
43
45
return result
44
46
45
47
@wraps (func )
46
- def async_func (* args , ** kwargs ):
48
+ def async_func (* pargs , ** kwargs ):
47
49
"""
48
50
A wrapper to run a function in a thread
49
51
"""
50
52
global running_async , async_lock
51
- thread = Thread (target = pooled , args = args , kwargs = kwargs )
53
+ thread = Thread (target = pooled , args = pargs , kwargs = kwargs )
52
54
semaphore .acquire ()
53
55
with async_lock :
54
56
running_async += 1
@@ -509,8 +511,7 @@ def dispatchStringRegex(self, update):
509
511
510
512
Args:
511
513
command (str): The command keyword
512
- update (telegram.Update): The Telegram update that contains the
513
- command
514
+ update (str): The string that contains the command
514
515
"""
515
516
516
517
matching_handlers = []
@@ -560,7 +561,7 @@ def dispatchError(self, update, error):
560
561
for handler in self .error_handlers :
561
562
handler (self .bot , update , error )
562
563
563
- def dispatchTo (self , handlers , update ):
564
+ def dispatchTo (self , handlers , update , ** kwargs ):
564
565
"""
565
566
Dispatches an update to a list of handlers.
566
567
@@ -570,9 +571,9 @@ def dispatchTo(self, handlers, update):
570
571
"""
571
572
572
573
for handler in handlers :
573
- self .call_handler (handler , update )
574
+ self .call_handler (handler , update , ** kwargs )
574
575
575
- def call_handler (self , handler , update ):
576
+ def call_handler (self , handler , update , ** kwargs ):
576
577
"""
577
578
Calls an update handler. Checks the handler for keyword arguments and
578
579
fills them, if possible.
@@ -581,20 +582,34 @@ def call_handler(self, handler, update):
581
582
handler (function): An update handler function
582
583
update (any): An update
583
584
"""
584
- kwargs = {}
585
+
586
+ target_kwargs = {}
585
587
fargs = getargspec (handler ).args
586
588
587
- if 'update_queue' in fargs :
588
- kwargs ['update_queue' ] = self .update_queue
589
+ '''
590
+ async handlers will receive all optional arguments, since we can't
591
+ their argument list.
592
+ '''
593
+
594
+ is_async = 'pargs' == getargspec (handler ).varargs
589
595
590
- if 'args' in fargs :
596
+ if is_async or 'update_queue' in fargs :
597
+ target_kwargs ['update_queue' ] = self .update_queue
598
+
599
+ if is_async or 'args' in fargs :
591
600
if isinstance (update , Update ):
592
601
args = update .message .text .split (' ' )[1 :]
593
602
elif isinstance (update , str ):
594
603
args = update .split (' ' )[1 :]
595
604
else :
596
605
args = None
597
606
598
- kwargs ['args' ] = args
607
+ target_kwargs ['args' ] = args
608
+
609
+ if is_async or 'groups' in fargs :
610
+ target_kwargs ['groups' ] = kwargs .get ('groups' , None )
611
+
612
+ if is_async or 'groupdict' in fargs :
613
+ target_kwargs ['groupdict' ] = kwargs .get ('groupdict' , None )
599
614
600
- handler (self .bot , update , ** kwargs )
615
+ handler (self .bot , update , ** target_kwargs )
0 commit comments