Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from zopefoundation/classhandler
Classhandler
- Loading branch information
Showing
5 changed files
with
127 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
Class-based event handlers | ||
========================== | ||
|
||
A light-weight event-handler framework based on event classes is | ||
provided by the ``zope.event.classhandler`` module. | ||
|
||
Handlers are registered for event classes: | ||
|
||
>>> import zope.event.classhandler | ||
|
||
>>> class MyEvent(object): | ||
... def __repr__(self): | ||
... return self.class.__name__ | ||
|
||
>>> def handler1(event): | ||
... print("handler1 %r" % event) | ||
|
||
>>> zope.event.classhandler.handler(MyEvent, handler1) | ||
|
||
Descriptor syntax: | ||
|
||
>>> @zope.event.classhandler.handler(MyEvent) | ||
... def handler2(event): | ||
... print("handler2 %r" % event) | ||
|
||
>>> class MySubEvent(MyEvent): | ||
... pass | ||
|
||
>>> @zope.event.classhandler.handler(MySubEvent) | ||
... def handler3(event): | ||
... print("handler3 %r" % event) | ||
|
||
|
||
Subscribers are called in class method-resolution order, so only | ||
new-style event classes are supported, and then by order of registry. | ||
|
||
>>> import zope.event | ||
>>> zope.event.notify(MySubEvent()) | ||
handler3 MySubEvent | ||
handler1 MySubEvent | ||
handler2 MySubEvent | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ Contents: | |
usage | ||
theory | ||
api | ||
classhandler | ||
hacking | ||
|
||
Indices and tables | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
"""Class-based event handlers | ||
A light-weight event-handler framework based on event classes. | ||
Handlers are registered for event classes: | ||
>>> import zope.event.classhandler | ||
>>> class MyEvent(object): | ||
... def __repr__(self): | ||
... return self.__class__.__name__ | ||
>>> def handler1(event): | ||
... print("handler1 %r" % event) | ||
>>> zope.event.classhandler.handler(MyEvent, handler1) | ||
Descriptor syntax: | ||
>>> @zope.event.classhandler.handler(MyEvent) | ||
... def handler2(event): | ||
... print("handler2 %r" % event) | ||
>>> class MySubEvent(MyEvent): | ||
... pass | ||
>>> @zope.event.classhandler.handler(MySubEvent) | ||
... def handler3(event): | ||
... print("handler3 %r" % event) | ||
Subscribers are called in class method-resolution order, so only | ||
new-style event classes are supported, and then by order of registry. | ||
>>> import zope.event | ||
>>> zope.event.notify(MySubEvent()) | ||
handler3 MySubEvent | ||
handler1 MySubEvent | ||
handler2 MySubEvent | ||
""" | ||
import zope.event | ||
|
||
registry = {} | ||
|
||
def handler(event_class, handler_=None, decorator=False): | ||
"""Define an event handler for a (new-style) class. | ||
This can be called with a class and a handler, or with just a | ||
class and the result used as a handler decorator. | ||
""" | ||
if handler_ is None: | ||
return lambda func: handler(event_class, func, True) | ||
|
||
if not registry: | ||
zope.event.subscribers.append(dispatch) | ||
|
||
if event_class not in registry: | ||
registry[event_class] = [handler_] | ||
else: | ||
registry[event_class].append(handler_) | ||
|
||
if decorator: | ||
return handler | ||
|
||
def dispatch(event): | ||
for event_class in event.__class__.__mro__: | ||
for handler in registry.get(event_class, ()): | ||
handler(event) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters