Archive for November, 2005
Object event dispatching in Zope
Here are some explanations about what happens in Zope 3.2 (and Zope 2.9
when using Five) when an event notification is sent by some code, up to
a specific subscriber. It focuses more specifically on object events,
which go through some additional hoops. All this is complex because
there are many simple components that are linked together.
Let’s start with some framework code that sends an event after an object
has been added (similar to what zope.app.container.contained
actually does):
event = ObjectAddedEvent(ob, container, name) zope.event.notify(event)
In zope.event we have the definition for this function:
subscribers = [] # registered subscribers def notify(event): for subscriber in subscribers: subscriber(event)
During initialization, zope.app.event.dispatching has registered a
subscriber:
def dispatch(*event): # Iterating over subscribers assures they get executed. for ignored in zope.component.subscribers(event, None): pass zope.event.subscribers.append(dispatch)
The function zope.component.subscribers will then call all matching
subscribers.
During initialization, zope/app/event/configure.zcml has registered
a subscriber for zope.app.event.interfaces.IObjectEvent with … Read more
Events in Zope 2.9
<p>Zope 2.9 (and Zope 2.8 when using Five 1.2) introduces a big change: Zope 3 style container events.</p>
<p>With container events, you finally have the ability to react to things
happening to objects without have to subclass <tt class="docutils literal"><span class="pre">manage_afterAdd</span></tt>,
<tt class="docutils literal"><span class="pre">manage_beforeDelete</span></tt> or <tt class="docutils literal"><span class="pre">manage_afterClone</span></tt>. Instead, you just have
to register a subscriber for the appropriate event, for instance
IObjectAddedEvent, and make it do the work.</p>
<p>Indeed, the old methods like <tt class="docutils literal"><span class="pre">manage_afterAdd</span></tt> are now deprecated, you
shouldn't use them anymore.</p>
<p>Let's see how to migrate your products.</p>
<p class="section " id="old-product">
<h1><a name="old-product">Old product</a></h1>
<p>Suppose that in an old product you have code that needs to register
through a central tool whenever a document is created. Or it could be
indexing itself. Or it could initialize an attribute according to its
current path. Code like:</p>
<pre class="literal-block">class CoolDocument(…):
…
def manage_afterAdd(self, item, … Read more