[Zope3-checkins] CVS: Zope3/src/zope/app/event - configure.zcml:1.6
objectevent.py:1.8
michael dunstan
michael at elyt.com
Mon Feb 9 04:08:14 EST 2004
Update of /cvs-repository/Zope3/src/zope/app/event
In directory cvs.zope.org:/tmp/cvs-serv31166/src/zope/app/event
Modified Files:
configure.zcml objectevent.py
Log Message:
Add ObjectEventNotifier to replace IAddNotifier and friends.
It uses multiple subscription adapters to dispatch events to interested objects.
Work remains to convert existing uses of IAddNotifier, etc, so that we can then
remove them.
=== Zope3/src/zope/app/event/configure.zcml 1.5 => 1.6 ===
--- Zope3/src/zope/app/event/configure.zcml:1.5 Fri Aug 22 16:02:12 2003
+++ Zope3/src/zope/app/event/configure.zcml Mon Feb 9 04:07:43 2004
@@ -1,5 +1,6 @@
<configure
xmlns='http://namespaces.zope.org/zope'
+ xmlns:event="http://namespaces.zope.org/event"
>
<serviceType
@@ -9,5 +10,9 @@
<service
serviceType='EventPublication'
component='zope.app.event.globalservice.eventPublisher' />
+
+<event:subscribe
+ subscriber=".objectevent.objectEventNotifierInstance"
+ event_types="zope.app.interfaces.event.IObjectEvent" />
</configure>
=== Zope3/src/zope/app/event/objectevent.py 1.7 => 1.8 ===
--- Zope3/src/zope/app/event/objectevent.py:1.7 Sun Sep 21 13:32:06 2003
+++ Zope3/src/zope/app/event/objectevent.py Mon Feb 9 04:07:43 2004
@@ -23,9 +23,13 @@
from zope.app.interfaces.event import IObjectCopiedEvent
from zope.app.interfaces.event import IObjectAnnotationsModifiedEvent
from zope.app.interfaces.event import IObjectContentModifiedEvent
+from zope.app.interfaces.event import ISubscriber
from zope.app.traversing import getPath
from zope.interface import implements
from zope.app.event import publish
+from zope.component import querySubscriptionMultiAdapter
+from zope.component import getService
+from zope.app.services.servicenames import Adapters
_marker = object()
@@ -70,3 +74,41 @@
"""An object has been copied"""
implements(IObjectCopiedEvent)
+
+
+class ObjectEventNotifier:
+ """Event subscriber to dispatch ObjectEvents to interested adapters."""
+ implements(ISubscriber)
+
+ def notify(self, event):
+ assert IObjectEvent.isImplementedBy(event)
+ adapters = querySubscriptionMultiAdapter((event.object, event),
+ ISubscriber, context=None)
+ for adapter in adapters:
+ adapter.notify(event)
+
+objectEventNotifierInstance = ObjectEventNotifier()
+
+
+def objectEventCallbackHelper(callback):
+ """Build a factory implementing ISubscriber that just calls a callback
+
+ callback will be called with an ObjectEvent instance. Example usage:
+
+ factory = objectEventCallbackHelper(events.append)
+ getService(None, Adapters).provideSubscriptionAdapter(
+ MyObjectType, ISubscriber, [factory], with=[IObjectRemovedEvent]
+ )
+ """
+ class _CallbackHelper:
+ implements(ISubscriber)
+ def __init__(self, object, event):
+ self.object = object
+ self.event = event
+
+ def notify(self, event):
+ callback(event)
+
+ return _CallbackHelper
+
+
More information about the Zope3-Checkins
mailing list