-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsignals.py
40 lines (34 loc) · 1.41 KB
/
signals.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
import logging
import django.dispatch
def get_subclasses(classes, level=0):
"""
Return the list of all subclasses given class (or list of classes) has.
Inspired by this question:
http://stackoverflow.com/questions/3862310/how-can-i-find-all-subclasses-of-a-given-class-in-python
"""
# for convenience, only one class can can be accepted as argument
# converting to list if this is the case
if not isinstance(classes, list):
classes = [classes]
if level < len(classes):
classes += classes[level].__subclasses__()
return get_subclasses(classes, level+1)
else:
return classes
def receiver_subclasses(signal, sender, dispatch_uid_prefix, **kwargs):
"""
A decorator for connecting receivers and all receiver's subclasses to signals. Used by passing in the
signal and keyword arguments to connect::
@receiver_subclasses(post_save, sender=MyModel)
def signal_receiver(sender, **kwargs):
...
"""
def _decorator(func):
all_senders = get_subclasses(sender)
logging.info(all_senders)
for snd in all_senders:
signal.connect(func, sender=snd, dispatch_uid=dispatch_uid_prefix+'_'+snd.__name__, **kwargs)
return func
return _decorator
## Signal to all that the files are synced
filesync_done = django.dispatch.Signal(providing_args=["repositoryName", "repositoryDir"])