-
Notifications
You must be signed in to change notification settings - Fork 0
/
observe.py
406 lines (304 loc) · 13.2 KB
/
observe.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
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# -*- coding: utf-8 -*-
# observe.py
# Copyright (C) 2003-2008 Jean-Baptiste LAMY -- [email protected]
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""editobj2.observe -- Observation framework
observe(obj, listener) registers listener as a listener for obj; when obj is modified,
listener will be called (asynchronously). obj can be a Python instance (old-style or
new-style), a list or a dictionary. The listener is a function of the form:
def my_listener(obj, type, new, old):
...
where obj is the modified object, type is the type of the modification, and old and new
are the old and new values. Type can be:
- object (the Python root class): one or more attributes have changed on obj. old and
new are the old and new attribute dictionary of obj (this dictionary includes attributes
in obj.__dict__, but also Python's property and C-level getset).
If you want to know which attribute has changed, use dictdiff on new and old (see the
diffdict function docstring).
- list : one or more addition / deletion have occured on obj (which is a list). new
and old are the new and old list content of obj.
- dict : one or more assignment / deletion have occured on obj (which is a mapping).
new and old are the new and old dictionary values.
- "__class__" : the class of obj has changed. new and old are the new and old classes.
Before all, you need to start the observer daemon, either in a new thread by calling
start_observing(), or in the Tkinter thread with start_observing_tk(). You can also
call scan() yourself when you want to check for change.
This module is the successor of the deprecated eventobj; it is much much much cleaner.
Quick example :
>>> from editobj2.observe import *
>>> start_scanning()
>>> class C: pass
...
>>> c = C()
>>> def listener(obj, type, new, old):
... if type is object:
... for (attr, newvalue, oldvalue) in diffdict(new, old):
... print "c.%s was %s, is now %s" % (attr, oldvalue, newvalue)
>>> observe(c, listener)
>>> c.x = 1
c.x was None, is now 1
See observe_tree and unobserve_tree for observing nested list and / or dict structures."""
__all__ = [
"observe", "isobserved", "unobserve",
"observe_tree", "unobserve_tree",
"start_scanning", "start_scanning_tk", "start_scanning_gtk", "stop_scanning", "scan",
"diffdict",
]
from weakref import ref
_observed_seqs = {}
_observed_objects = {}
_class_2_properties = {}
def _get_class_properties(klass):
props = _class_2_properties.get(klass)
if props is None:
props = _class_2_properties[klass] = [attr for attr in dir(klass) if (not attr in _IGNORED_ATTRS) and (type(getattr(klass, attr)).__name__ in PROPERTY_TYPE_NAMES)]
return props
PROPERTY_TYPE_NAMES = ("property", "getset_descriptor")
_IGNORED_ATTRS = set(["__weakref__", "__abstractmethods__"])
def observe(o, listener):
"""observe(o, listener)
Registers LISTENER as a listener of O. When O will be changed, LISTENER will
be called (asynchronously).
See the module docstrings for more info about what argument receives a listener"""
if hasattr(o, "__observe__"): o.__observe__(listener)
i = id(o)
observation = _observed_seqs.get(i)
if observation: observation.listeners.append(listener)
else:
if isinstance(o, list): _observed_seqs[i] = ListObservation(o, [listener])
elif isinstance(o, set ): _observed_seqs[i] = SetObservation (o, [listener])
elif isinstance(o, dict): _observed_seqs[i] = DictObservation(o, [listener])
if hasattr(o, "__dict__"):
observation = _observed_objects.get(i)
if observation: observation.listeners.append(listener)
else: observation = _observed_objects[i] = ObjectObservation(o, [listener])
def isobserved(o, listener = None):
"""isobserved(o, listener = None)
Return true if LISTENER is observing O. If listener is None, returns the list
of listeners for O."""
i = id(o)
observation = _observed_seqs.get(i) or (hasattr(o, "__dict__") and _observed_objects.get(i))
if listener: return observation and (listener in observation.listeners)
else: return observation and observation.listeners
def unobserve(o, listener = None):
"""unobserve(o, listener = None)
Unregisters the listener LISTENER for O. If LISTENER is not listening O,
nothing is done. If LISTENER is None, unregisters *all* listeners on O."""
if hasattr(o, "__unobserve__"): o.__unobserve__(listener)
i = id(o)
if listener:
observation = _observed_seqs.get(i)
if observation:
try: observation.listeners.remove(listener)
except ValueError: pass
if not observation.listeners: del _observed_seqs[i]
if hasattr(o, "__dict__"):
observation = _observed_objects.get(i)
if observation:
try: observation.listeners.remove(listener)
except ValueError: pass
if not observation.listeners: del _observed_objects[i]
else:
if _observed_seqs .has_key(i): del _observed_seqs [i]
if _observed_objects.has_key(i): del _observed_objects[i]
class Observation(object):
def __init__(self, o, listeners):
self.object = o
self.listeners = listeners
self.old = self.current_value()
class ListObservation(Observation):
type = list
def current_value(self): return list(self.object)
class SetObservation(Observation):
type = set
def current_value(self): return set(self.object)
class DictObservation(Observation):
type = dict
def current_value(self): return dict(self.object)
class ObjectObservation(Observation):
def __init__(self, o, listeners):
self.listeners = listeners
self.props = _get_class_properties(o.__class__)
self.old = self.current_value(o)
self.old_class = o.__class__
try: self.object = weakref.ref(o)
except: self.object = o
def current_value(self, o):
if self.props:
new = dict([(prop, getattr(o, prop, None)) for prop in self.props])
new.update(o.__dict__)
return new
else: return o.__dict__.copy()
def scan():
"""scan()
Checks for changes in listened objects, and calls the corresponding listeners if needed."""
for i, observation in _observed_seqs.items():
if observation.old != observation.object:
for listener in observation.listeners[:]: listener(observation.object, observation.type, observation.object, observation.old)
observation.old = observation.current_value()
for i, observation in _observed_objects.items():
if type(observation.object) is ref:
o = observation.object()
if o is None:
del _observed_objects[i]
continue
else: o = observation.object
if observation.props:
new = observation.current_value(o)
if observation.old != new:
for listener in observation.listeners[:]: listener(o, object, new, observation.old)
observation.old = new
else:
if observation.old != o.__dict__:
for listener in observation.listeners[:]: listener(o, object, o.__dict__, observation.old)
observation.old = observation.current_value(o)
if not observation.old_class is o.__class__:
for listener in observation.listeners[:]: listener(o, "__class__", o.__class__, observation.old_class)
observation.old_class = o.__class__
SCANNING = 0
def _scan_loop(freq):
from time import sleep
while SCANNING:
scan()
sleep(freq)
def start_scanning(freq = 0.2):
"""start_scanning(freq = 0.2)
Starts the observer daemon. This thread calls scan() repetitively, each FREQ seconds."""
global SCANNING
import thread
SCANNING = 1
thread.start_new_thread(_scan_loop, (freq,))
def start_scanning_gui(freq = 0.2):
"""start_scanning_gui(freq = 0.2)
Starts the observer daemon in the GUI thread. It calls start_scanning_tk or start_scanning_gtk,
according to the current GUI (i.e. editobj2.GUI)."""
import editobj2
if editobj2.GUI == "Tk" : start_scanning_tk()
elif editobj2.GUI == "Gtk" : start_scanning_gtk()
elif editobj2.GUI == "Qt" : start_scanning_qt()
elif editobj2.GUI == "Qtopia": start_scanning_qt()
def start_scanning_tk(freq = 0.2):
"""start_scanning_tk(freq = 0.2)
Starts the observer daemon in the Tkinter thread. This thread calls scan() repetitively,
each FREQ seconds."""
global SCANNING
SCANNING = 1
_start_scanning_tk(int(freq * 1000.0))
def _start_scanning_tk(freq):
from Tkinter import _default_root as tk
scan()
if SCANNING: tk.after(freq, _start_scanning_tk2, freq)
def _start_scanning_tk2(freq):
from Tkinter import _default_root as tk
tk.after_idle(_start_scanning_tk, freq)
def start_scanning_gtk(freq = 0.2):
"""start_scanning_gtk(freq = 0.2)
Starts the observer daemon in the GTK thread. This thread calls scan() repetitively,
each FREQ seconds."""
global SCANNING
SCANNING = 1
import gobject
gobject.timeout_add(int(freq * 1000.0), _start_scanning_gtk)
def _start_scanning_gtk():
scan()
return SCANNING
def start_scanning_qt(freq = 0.2):
"""start_scanning_qt(freq = 0.2)
Starts the observer daemon in the Qt thread. This thread calls scan() repetitively,
each FREQ seconds."""
global SCANNING
SCANNING = 1
import qt
if qt.QApplication.startingUp():
import sys, editobj2
if editobj2.GUI == "Qt": qt.app = qt.QApplication(sys.argv)
elif editobj2.GUI == "Qtopia":
import qtpe
qt.app = qtpe.QPEApplication(sys.argv)
timer = start_scanning_qt.timer = qt.QTimer(None)
timer.start(freq * 1000, 0)
timer.connect(timer, qt.SIGNAL("timeout()"), scan)
def stop_scanning():
"""stop_scanning()
Stops the observer daemon (started by start_scanning(), start_scanning_tk(), start_scanning_gtk(),...)."""
SCANNING = 0
def diffdict(new, old, inexistent_value = None):
"""diffdict(new, old) -> [(key, new_value, old_value),...]
Returns the differences between two dictionaries.
In case of addition or deletion, old or new values are None."""
changes = []
for key, val in old.iteritems():
new_val = new.get(key, Ellipsis)
if new_val is Ellipsis: changes.append((key, inexistent_value, val))
elif new_val != val: changes.append((key, new_val, val))
for key, val in new.iteritems():
old_val = old.get(key, Ellipsis)
if old_val is Ellipsis: changes.append((key, val, inexistent_value))
return changes
def find_all_children(o):
if isinstance(o, list): l = o
elif isinstance(o, set) or isinstance(o, tuple) or isinstance(o, frozenset): l = list(o)
elif isinstance(o, dict): l = o.keys() + o.values()
else: l = []
if hasattr(o, "__dict__"): l += _get_class_properties(o.__class__) + o.__dict__.values()
return l
def observe_tree(o, listener, find_children = find_all_children):
"""observe_tree(o, listener)
Observes O with LISTENER, as well as any item in O (if O is a list, a dict,
or have a "children" or "items" attribute / method). Items added to or removed from O
or one of its items are automatically observed or unobserved.
Although called "observe_tree", it works with any nested structure of lists and dicts,
including cyclic ones.
You must use unobserve_tree to remove the listener."""
_observe_tree(o, _TreeListener(o, listener, find_children))
def _observe_tree(o, listener):
if not isobserved(o, listener): # Avoid troubles with cyclic list / dict
observe(o, listener)
children = listener.find_children(o)
for child in children:
_observe_tree(child, listener)
def unobserve_tree(o, listener, find_children = find_all_children, already = None):
"""unobserve_tree(o, listener)
Unregisters the tree listener LISTENER for O."""
if already is None: already = set()
if not id(o) in already:
already.add(id(o))
unobserve(o, listener)
#if isobserved(o, listener): # Avoid troubles with cyclic list / dict
# unobserve(o, listener)
children = find_children(o)
for child in children:
if not id(child) in already:
unobserve_tree(child, listener, find_children, already)
class _TreeListener:
def __init__(self, o, listener, find_children = find_all_children):
self.object = o
self.listener = listener
self.find_children = find_children
def __eq__(self, other): return other == self.listener
def __call__(self, o, type, new, old):
if type is list:
for item in old:
if not item in new: unobserve_tree(item, self)
for item in new:
if not item in old: _observe_tree (item, self)
elif (type is dict) or (type is object):
_new = new.values()
_old = old.values()
for item in _old:
if not item in _new: unobserve_tree(item, self)
for item in _new:
if not item in _old: _observe_tree (item, self)
self.listener(o, type, new, old)