-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathevents.py
More file actions
365 lines (282 loc) · 12.1 KB
/
Copy pathevents.py
File metadata and controls
365 lines (282 loc) · 12.1 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
from __future__ import print_function
import sys
import types
from pydispatch import dispatcher
from weakref import ref
from .compat import class_types
subclassClones = {}
def listen(receiver, soClass, signal, alsoSubclasses=True, weak=True):
"""
Listen for the given ``signal`` on the SQLObject subclass
``soClass``, calling ``receiver()`` when ``send(soClass, signal,
...)`` is called.
If ``alsoSubclasses`` is true, receiver will also be called when
an event is fired on any subclass.
"""
dispatcher.connect(receiver, signal=signal, sender=soClass, weak=weak)
weakReceiver = ref(receiver)
subclassClones.setdefault(soClass, []).append((weakReceiver, signal))
# We export this function:
send = dispatcher.send
class Signal(object):
"""
Base event for all SQLObject events.
In general the sender for these methods is the class, not the
instance.
"""
class ClassCreateSignal(Signal):
"""
Signal raised after class creation. The sender is the superclass
(in case of multiple superclasses, the first superclass). The
arguments are ``(new_class_name, bases, new_attrs, post_funcs,
early_funcs)``. ``new_attrs`` is a dictionary and may be modified
(but ``new_class_name`` and ``bases`` are immutable).
``post_funcs`` is an initially-empty list that can have callbacks
appended to it.
Note: at the time this event is called, the new class has not yet
been created. The functions in ``post_funcs`` will be called
after the class is created, with the single arguments of
``(new_class)``. Also, ``early_funcs`` will be called at the
soonest possible time after class creation (``post_funcs`` is
called after the class's ``__classinit__``).
"""
def _makeSubclassConnections(new_class_name, bases, new_attrs,
post_funcs, early_funcs):
early_funcs.insert(0, _makeSubclassConnectionsPost)
def _makeSubclassConnectionsPost(new_class):
for cls in new_class.__bases__:
for weakReceiver, signal in subclassClones.get(cls, []):
receiver = weakReceiver()
if not receiver:
continue
listen(receiver, new_class, signal)
dispatcher.connect(_makeSubclassConnections, signal=ClassCreateSignal)
# @@: Should there be a class reload event? This would allow modules
# to be reloaded, possibly. Or it could even be folded into
# ClassCreateSignal, since anything that listens to that needs to pay
# attention to reloads (or else it is probably buggy).
class RowCreateSignal(Signal):
"""
Called before an instance is created, with the class as the
sender. Called with the arguments ``(instance, kwargs, post_funcs)``.
There may be a ``connection`` argument. ``kwargs``may be usefully
modified. ``post_funcs`` is a list of callbacks, intended to have
functions appended to it, and are called with the arguments
``(new_instance)``.
Note: this is not called when an instance is created from an
existing database row.
"""
class RowCreatedSignal(Signal):
"""
Called after an instance is created, with the class as the
sender. Called with the arguments ``(instance, kwargs, post_funcs)``.
There may be a ``connection`` argument. ``kwargs``may be usefully
modified. ``post_funcs`` is a list of callbacks, intended to have
functions appended to it, and are called with the arguments
``(new_instance)``.
Note: this is not called when an instance is created from an
existing database row.
"""
# @@: An event for getting a row? But for each row, when doing a
# select? For .sync, .syncUpdate, .expire?
class RowDestroySignal(Signal):
"""
Called before an instance is deleted. Sender is the instance's
class. Arguments are ``(instance, post_funcs)``.
``post_funcs`` is a list of callbacks, intended to have
functions appended to it, and are called with arguments ``(instance)``.
If any of the post_funcs raises an exception, the deletion is only
affected if this will prevent a commit.
You cannot cancel the delete, but you can raise an exception (which will
probably cancel the delete, but also cause an uncaught exception if not
expected).
Note: this is not called when an instance is destroyed through
garbage collection.
@@: Should this allow ``instance`` to be a primary key, so that a
row can be deleted without first fetching it?
"""
class RowDestroyedSignal(Signal):
"""
Called after an instance is deleted. Sender is the instance's
class. Arguments are ``(instance)``.
This is called before the post_funcs of RowDestroySignal
Note: this is not called when an instance is destroyed through
garbage collection.
"""
class RowUpdateSignal(Signal):
"""
Called when an instance is updated through a call to ``.set()``
(or a column attribute assignment). The arguments are
``(instance, kwargs)``. ``kwargs`` can be modified. This is run
*before* the instance is updated; if you want to look at the
current values, simply look at ``instance``.
"""
class RowUpdatedSignal(Signal):
"""
Called when an instance is updated through a call to ``.set()``
(or a column attribute assignment). The arguments are
``(instance, post_funcs)``. ``post_funcs`` is a list of callbacks,
intended to have functions appended to it, and are called with the
arguments ``(new_instance)``. This is run *after* the instance is
updated; Works better with lazyUpdate = True.
"""
class AddColumnSignal(Signal):
"""
Called when a column is added to a class, with arguments ``(cls,
connection, column_name, column_definition, changeSchema,
post_funcs)``. This is called *after* the column has been added,
and is called for each column after class creation.
post_funcs are called with ``(cls, so_column_obj)``
"""
class DeleteColumnSignal(Signal):
"""
Called when a column is removed from a class, with the arguments
``(cls, connection, column_name, so_column_obj, post_funcs)``.
Like ``AddColumnSignal`` this is called after the action has been
performed, and is called for subclassing (when a column is
implicitly removed by setting it to ``None``).
post_funcs are called with ``(cls, so_column_obj)``
"""
# @@: Signals for indexes and joins? These are mostly event consumers,
# though.
class CreateTableSignal(Signal):
"""
Called when a table is created. If ``ifNotExists==True`` and the
table exists, this event is not called.
Called with ``(cls, connection, extra_sql, post_funcs)``.
``extra_sql`` is a list (which can be appended to) of extra SQL
statements to be run after the table is created. ``post_funcs``
functions are called with ``(cls, connection)`` after the table
has been created. Those functions are *not* called simply when
constructing the SQL.
"""
class DropTableSignal(Signal):
"""
Called when a table is dropped. If ``ifExists==True`` and the
table doesn't exist, this event is not called.
Called with ``(cls, connection, extra_sql, post_funcs)``.
``post_funcs`` functions are called with ``(cls, connection)``
after the table has been dropped.
"""
class CommitSignal(Signal):
"""
Called on transaction commit
"""
class RollbackSignal(Signal):
"""
Called on transaction rollback
"""
############################################################
# Event Debugging
############################################################
def summarize_events_by_sender(sender=None, output=None, indent=0):
"""
Prints out a summary of the senders and listeners in the system,
for debugging purposes.
"""
if output is None:
output = sys.stdout
leader = ' ' * indent
if sender is None:
send_list = [
(deref(dispatcher.senders.get(sid)), listeners)
for sid, listeners in dispatcher.connections.items()
if deref(dispatcher.senders.get(sid))]
for sender, listeners in sorted_items(send_list):
real_sender = deref(sender)
if not real_sender:
continue
header = 'Sender: %r' % real_sender
print(leader + header, file=output)
print(leader + ('=' * len(header)), file=output)
summarize_events_by_sender(real_sender, output=output,
indent=indent + 2)
else:
for signal, receivers in \
sorted_items(dispatcher.connections.get(id(sender), [])):
receivers = [deref(r) for r in receivers if deref(r)]
header = 'Signal: %s (%i receivers)' % (sort_name(signal),
len(receivers))
print(leader + header, file=output)
print(leader + ('-' * len(header)), file=output)
for receiver in sorted(receivers, key=sort_name):
print(leader + ' ' + nice_repr(receiver), file=output)
def deref(value):
if isinstance(value, dispatcher.WEAKREF_TYPES):
return value()
else:
return value
def sorted_items(a_dict):
if isinstance(a_dict, dict):
a_dict = a_dict.items()
return sorted(a_dict, key=lambda t: sort_name(t[0]))
def sort_name(value):
if isinstance(value, type):
return value.__name__
elif isinstance(value, types.FunctionType):
return value.__name__
else:
return str(value)
_real_dispatcher_send = dispatcher.send
_real_dispatcher_sendExact = dispatcher.sendExact
_real_dispatcher_connect = dispatcher.connect
_real_dispatcher_disconnect = dispatcher.disconnect
_debug_enabled = False
def debug_events():
global _debug_enabled, send
if _debug_enabled:
return
_debug_enabled = True
dispatcher.send = send = _debug_send
dispatcher.sendExact = _debug_sendExact
dispatcher.disconnect = _debug_disconnect
dispatcher.connect = _debug_connect
def _debug_send(signal=dispatcher.Any, sender=dispatcher.Anonymous,
*arguments, **named):
print("send %s from %s: %s" % (
nice_repr(signal), nice_repr(sender),
fmt_args(*arguments, **named)))
return _real_dispatcher_send(signal, sender, *arguments, **named)
def _debug_sendExact(signal=dispatcher.Any, sender=dispatcher.Anonymous,
*arguments, **named):
print("sendExact %s from %s: %s" % (
nice_repr(signal), nice_repr(sender), fmt_args(*arguments, **name)))
return _real_dispatcher_sendExact(signal, sender, *arguments, **named)
def _debug_connect(receiver, signal=dispatcher.Any, sender=dispatcher.Any,
weak=True):
print("connect %s to %s signal %s" % (
nice_repr(receiver), nice_repr(signal), nice_repr(sender)))
return _real_dispatcher_connect(receiver, signal, sender, weak)
def _debug_disconnect(receiver, signal=dispatcher.Any, sender=dispatcher.Any,
weak=True):
print("disconnecting %s from %s signal %s" % (
nice_repr(receiver), nice_repr(signal), nice_repr(sender)))
return _real_dispatcher_disconnect(receiver, signal, sender, weak)
def fmt_args(*arguments, **name):
args = [repr(a) for a in arguments]
args.extend([
'%s=%r' % (n, v) for n, v in sorted(name.items())])
return ', '.join(args)
def nice_repr(v):
"""
Like repr(), but nicer for debugging here.
"""
if isinstance(v, class_types):
return v.__module__ + '.' + v.__name__
elif isinstance(v, types.FunctionType):
if '__name__' in v.__globals__:
if getattr(sys.modules[v.__globals__['__name__']],
v.__name__, None) is v:
return '%s.%s' % (v.__globals__['__name__'], v.__name__)
return repr(v)
elif isinstance(v, types.MethodType):
return '%s.%s of %s' % (
nice_repr(v.__self__.__class__), v.__func__.__name__,
nice_repr(v.__self__))
else:
return repr(v)
__all__ = ['listen', 'send']
# Use copy() to avoid 'dictionary changed' issues on python 3
for name, value in globals().copy().items():
if isinstance(value, type) and issubclass(value, Signal):
__all__.append(name)