| Home | Trees | Indices | Help |
|
|---|
|
|
This module is the central point where Gaphas' classes report their state changes.
Invokations of method and state changing properties are emited to all
functions (or bound methods) registered in the 'observers' set.
Use observers.add() and observers.remove() to add/remove handlers.
This module also contains a second layer: a state inverser. Instead of emiting the invoked method, it emits a signal (callable, **kwargs) that can be applied to revert the state of the object to the point before the method invokation.
For this to work the revert_handler has to be added to the observers set:
gaphas.state.observers.add(gaphas.state.revert_handler)
Version: $Revision: 2240 $
| Functions | |||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
| Variables | |
DISPATCH_BY_DEFAULT = True
|
|
subscribers =
|
|
observers =
|
|
| Function Details |
Simple observer, dispatches events to functions registered in the observers list. On the function an __observer__ property is set, which references to the observer decorator. This is nessesary, since the event handlers expect the outer most function to be returned (that's what they see). Also note that the events are dispatched before the function is invoked. This is an important feature, esp. for the reverter code. |
Enable/disable dispatching for a specific function. >>> @observed ... def callme(): ... pass >>> def handler(event): ... print 'event' >>> observers.add(handler) By default dispatching is enabled (set DISPATCH_BY_DEFAULT=False to disable this behavior). >>> callme() event >>> enable_dispatching(callme, False) >>> callme() ... and enable it again: >>> enable_dispatching(callme) >>> callme() event >>> observers.remove(handler) |
Dispatch an event to a queue of event handlers. Event handlers should have signature: handler(event). >>> def handler(event): ... print 'event handled', event >>> observers.add(handler) >>> @observed ... def callme(): ... pass >>> callme() # doctest: +ELLIPSIS event handled (<function callme at 0x...>, (), {}) >>> class Callme(object): ... @observed ... def callme(self): ... pass >>> Callme().callme() # doctest: +ELLIPSIS event handled (<function callme at 0x...), {}) >>> observers.remove(handler) >>> callme() |
Treat a pair of functions (func1 and func2) as each others inverse operation. bind1 provides arguments that can overrule the default values (or add additional values). bind2 does the same for func2. See revert_handler() for doctesting. |
Replacement for the property descriptor. In addition to creating a property instance, the property is registered as reversible and reverse events can be send out when changes occur. Cave eat: we can't handle both fset and fdel in the proper way. Therefore fdel should somehow invoke fset. (persinally, I hardly use fdel) See revert_handler() for doctesting. |
Event handler, generates undoable statements and puts them on the subscribers queue. First thing to do is to actually enable the revert_handler:
>>> observers.add(revert_handler)
First let's define our simple list: >>> class SList(object): ... def __init__(self): ... self.l = list() ... def add(self, node, before=None): ... if before: self.l.insert(self.l.index(before), node) ... else: self.l.append(node) ... add = observed(add) ... @observed ... def remove(self, node): ... self.l.remove(self.l.index(node)) >>> sl = SList() >>> sl.add(10) >>> sl.l [10] >>> sl.add(11) >>> sl.l [10, 11] >>> sl.add(12, before=11) >>> sl.l [10, 12, 11] It works, so let's add some reversible stuff: >>> reversible_pair(SList.add, SList.remove, bind1={'before': lambda self, node: self.l[self.l.index(node)+1] }) >>> _reverse[SList.add.im_func] # doctest: +ELLIPSIS (<function remove at 0x...>, (['self', 'node'], None, None, None), {}) >>> _reverse[SList.remove.im_func] # doctest: +ELLIPSIS (<function add at 0x...>, (['self', 'node', 'before'], None, None, (None,)), {'before': <function <lambda> at ...>}) >>> def handler(event): ... print 'handle', event >>> subscribers.add(handler) >>> sl.add(20) # doctest: +ELLIPSIS handle (<function remove at 0x...) Same goes for properties (more or less): >>> class PropTest(object): ... def __init__(self): self._a = 0 ... @observed ... def _set_a(self, value): self._a = value ... a = reversible_property(lambda s: s._a, _set_a) >>> pt = PropTest() >>> pt.a 0 >>> pt.a = 10 # doctest: +ELLIPSIS handle (<function _set_a at 0x...>, {'self': <gaphas.state.PropTest object at 0x...>, 'value': 0})
>>> subscribers.remove(handler)
|
|
| Home | Trees | Indices | Help |
|
|---|
| Generated by Epydoc 3.0 on Mon Mar 03 08:52:21 2008 | http://epydoc.sourceforge.net |