Repository navigation
Expand file tree
/
Copy pathdispatch.py
More file actions
169 lines (154 loc) · 5.05 KB
/
Copy pathdispatch.py
File metadata and controls
169 lines (154 loc) · 5.05 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import logging
import inspect
from uliweb.utils.common import import_attr
import six
__all__ = ['HIGH', 'MIDDLE', 'LOW', 'bind', 'call', 'get', 'unbind', 'call_once', 'get_once']
HIGH = 1 #plugin high
MIDDLE = 2
LOW = 3
_receivers = {}
_called = {}
def bind(topic, signal=None, kind=MIDDLE, nice=-1):
"""
This is a decorator function, so you should use it as:
@bind('init')
def process_init(a, b):
...
"""
def f(func):
if not topic in _receivers:
receivers = _receivers[topic] = []
else:
receivers = _receivers[topic]
if nice == -1:
if kind == MIDDLE:
n = 500
elif kind == HIGH:
n = 100
else:
n = 900
else:
n = nice
if six.callable(func):
func_name = func.__module__ + '.' + func.__name__
func = func
else:
func_name = func
func = None
_f = (n, {'func':func, 'signal':signal, 'func_name':func_name})
receivers.append(_f)
return func
return f
def unbind(topic, func):
"""
Remove receiver function
"""
if topic in _receivers:
receivers = _receivers[topic]
for i in range(len(receivers)-1, -1, -1):
nice, f = receivers[i]
if (six.callable(func) and f['func'] == func) or (f['func_name'] == func):
del receivers[i]
return
def _test(kwargs, receiver):
signal = kwargs.get('signal', None)
f = receiver['func']
args = inspect.getargspec(f)[0]
if 'signal' not in args:
kwargs.pop('signal', None)
_signal = receiver.get('signal')
flag = True
if _signal:
if isinstance(_signal, (tuple, list)):
if signal not in _signal:
flag = False
elif _signal!=signal:
flag = False
return flag
def call(sender, topic, *args, **kwargs):
"""
Invoke receiver functions according topic, it'll invoke receiver functions one by one,
and it'll not return anything, so if you want to return a value, you should
use get function.
"""
if not topic in _receivers:
return
items = _receivers[topic]
items.sort(key=lambda x:x[0])
i = 0
while i<len(items):
nice, f = items[i]
i = i + 1
_f = f['func']
if not _f:
try:
_f = import_attr(f['func_name'])
except (ImportError, AttributeError) as e:
logging.error("Can't import function %s" % f['func_name'])
raise
f['func'] = _f
if six.callable(_f):
kw = kwargs.copy()
if not _test(kw, f):
continue
try:
_f(sender, *args, **kw)
except:
func = _f.__module__ + '.' + _f.__name__
logging.exception('Calling dispatch point [%s] %s(%r, %r) error!' % (topic, func, args, kw))
raise
else:
raise Exception("Dispatch point [%s] %r can't been invoked" % (topic, _f))
def call_once(sender, topic, *args, **kwargs):
signal = kwargs.get('signal')
if (topic, signal) in _called:
return
else:
call(sender, topic, *args, **kwargs)
_called[(topic, signal)] = True
def get(sender, topic, *args, **kwargs):
"""
Invoke receiver functions according topic, it'll invoke receiver functions one by one,
and if one receiver function return non-None value, it'll return it and break
the loop.
"""
if not topic in _receivers:
return
items = _receivers[topic]
def _cmp(x, y):
return cmp(x[0], y[0])
items.sort(_cmp)
for i in range(len(items)):
nice, f = items[i]
_f = f['func']
if not _f:
try:
_f = import_attr(f['func_name'])
except ImportError:
logging.error("Can't import function %s" % f['func_name'])
raise
f['func'] = _f
if six.callable(_f):
if not _test(kwargs, f):
continue
try:
v = _f(sender, *args, **kwargs)
except:
func = _f.__module__ + '.' + _f.__name__
logging.exception('Calling dispatch point [%s] %s(%r,%r) error!' % (topic, func, args, kwargs))
raise
if v is not None:
return v
else:
raise "Dispatch point [%s] %r can't been invoked" % (topic, _f)
def get_once(sender, topic, *args, **kwargs):
signal = kwargs.get('signal')
if (topic, signal) in _called:
return _called[(topic, signal)]
else:
r = get(sender, topic, *args, **kwargs)
_called[(topic, signal)] = r
return r
def print_topics():
import pprint
pprint.pprint(_receivers)