[Zope3-dev] Re: Heads up: major event changes
Jim Fulton
jim at zope.com
Thu May 27 09:19:23 EDT 2004
This is checked in. For some reason, the checkin mailer missfired on this
one and there was no mail to zope3-checkins. (Although, perhaps the
message is just being delayed due to it's size.)
Jim
Jim Fulton wrote:
>
> This morning I'll be checking in a major simplification of Zope 3's event
> system, http://dev.zope.org/Zope/SimplifyEventSystem.
>
> The major change for application developers:
>
> 1. Any object can be an event
>
> 2. You publish events like this:
>
> from zope.event import notify
>
> ...
>
> notify(event)
>
> Calling zope.app.event.publish still works, but generates a
> deprecation warning.
>
> 3. Event handlers are now defined as functions. For example, before:
>
> from datetime import datetime
> from zope.app.dublincore.interfaces import IZopeDublinCore
> from zope.app.event.interfaces import ISubscriber
> from zope.interface import implements
>
> class DCTimeAnnotatorBase:
> """Update Dublin-Core time property
> """
> implements(ISubscriber)
>
> def notify(self, event):
> dc = IZopeDublinCore(event.object, None)
> if dc is not None:
> self.annotate(dc)
>
> def annotate(self, dc):
> raise RuntimeError, 'annotate not implemented'
>
>
> class ModifiedAnnotatorClass(DCTimeAnnotatorBase):
> """Updates DC modified when an object is modified."""
>
> def annotate(self, dc):
> dc.modified = datetime.utcnow()
>
>
> class CreatedAnnotatorClass(DCTimeAnnotatorBase):
> """Sets DC created and modified when an object is created."""
>
> def annotate(self, dc):
> now = datetime.utcnow()
> dc.created = now
> dc.modified = now
>
>
> ModifiedAnnotator = ModifiedAnnotatorClass()
> CreatedAnnotator = CreatedAnnotatorClass()
>
> After:
>
> from datetime import datetime
> from zope.app.dublincore.interfaces import IZopeDublinCore
>
> def ModifiedAnnotator(event):
> dc = IZopeDublinCore(event.object, None)
> if dc is not None:
> dc.modified = datetime.utcnow()
>
> def CreatedAnnotator(event):
> dc = IZopeDublinCore(event.object, None)
> if dc is not None:
> now = datetime.utcnow()
> dc.created = now
> dc.modified = now
>
> :)
>
> Handlers are also registered differently in ZCML. There are no longer
> event:subscriber directives. Rather, subscriber directoves from the zope
> namespace are used, as in:
>
> <subscriber
> for="zope.app.event.interfaces.IObjectModifiedEvent"
> factory=".timeannotators.ModifiedAnnotator"
> />
>
> <subscriber
> for="zope.app.event.interfaces.IObjectCreatedEvent"
> factory=".timeannotators.CreatedAnnotator"
> />
>
> Jim
>
--
Jim Fulton mailto:jim at zope.com Python Powered!
CTO (540) 361-1714 http://www.python.org
Zope Corporation http://www.zope.com http://www.zope.org
More information about the Zope3-dev
mailing list