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
|
==============================================
``zope.component.events``: Event dispatching
==============================================
.. currentmodule:: zope.component.event
The Component Architecture provides a way to dispatch events to event
handlers using :func:`zope.event.notify`. Event handlers are
registered as *subscribers* a.k.a. *handlers*.
Before we can start we need to import ``zope.component.event`` to make
the dispatching effective:
.. doctest::
>>> import zope.component.event
Consider two event classes:
.. doctest::
>>> class Event1(object):
... pass
>>> class Event2(Event1):
... pass
Now consider two handlers for these event classes:
.. doctest::
>>> called = []
>>> import zope.component
>>> @zope.component.adapter(Event1)
... def handler1(event):
... called.append(1)
>>> @zope.component.adapter(Event2)
... def handler2(event):
... called.append(2)
We can register them with the Component Architecture:
.. doctest::
>>> zope.component.provideHandler(handler1)
>>> zope.component.provideHandler(handler2)
Now let's go through the events. We'll see that the handlers have been
called accordingly:
.. doctest::
>>> from zope.event import notify
>>> notify(Event1())
>>> called
[1]
>>> del called[:]
>>> notify(Event2())
>>> called.sort()
>>> called
[1, 2]
Object events
=============
The ``objectEventNotify`` function is a subscriber to dispatch
ObjectEvents to interested adapters.
.. autofunction:: objectEventNotify
.. note:: This function is automatically registered as a
subscriber for
:class:`zope.interface.interfaces.IObjectEvent`
when the ZCML configuration for this package is loaded.
First create an object class:
.. doctest::
>>> class IUseless(zope.interface.Interface):
... """Useless object"""
>>> @zope.interface.implementer(IUseless)
... class UselessObject(object):
... """Useless object"""
Then create an event class:
.. doctest::
>>> class IObjectThrownEvent(zope.interface.interfaces.IObjectEvent):
... """An object has been thrown away"""
>>> @zope.interface.implementer(IObjectThrownEvent)
... class ObjectThrownEvent(zope.interface.interfaces.ObjectEvent):
... """An object has been thrown away"""
Create an object and an event:
.. doctest::
>>> hammer = UselessObject()
>>> event = ObjectThrownEvent(hammer)
Then notify the event to the subscribers.
Since the subscribers list is empty, nothing happens.
.. doctest::
>>> zope.component.event.objectEventNotify(event)
Now create an handler for the event:
.. doctest::
>>> events = []
>>> def record(*args): #*
... events.append(args)
>>> zope.component.provideHandler(record, [IUseless, IObjectThrownEvent])
The event is notified to the subscriber:
.. doctest::
>>> zope.component.event.objectEventNotify(event)
>>> events == [(hammer, event)]
True
Following test demonstrates how a subscriber can raise an exception
to prevent an action.
.. doctest::
>>> zope.component.provideHandler(zope.component.event.objectEventNotify)
Let's create a container:
.. doctest::
>>> class ToolBox(dict):
... def __delitem__(self, key):
... notify(ObjectThrownEvent(self[key]))
... return super(ToolBox,self).__delitem__(key)
>>> container = ToolBox()
And put the object into the container:
.. doctest::
>>> container['Red Hammer'] = hammer
Create an handler function that will raise an error when called:
.. doctest::
>>> class Veto(Exception):
... pass
>>> def callback(item, event):
... assert(item == event.object)
... raise Veto
Register the handler:
.. doctest::
>>> zope.component.provideHandler(callback, [IUseless, IObjectThrownEvent])
Then if we try to remove the object, an ObjectThrownEvent is fired:
.. doctest::
>>> del container['Red Hammer']
... # doctest: +NORMALIZE_WHITESPACE
Traceback (most recent call last):
...
raise Veto
Veto
|