[Checkins] SVN: zc.async/trunk/s generate events when some
interesting objects are added
Fred L. Drake, Jr.
fdrake at gmail.com
Fri Apr 18 18:30:05 EDT 2008
Log message for revision 85479:
generate events when some interesting objects are added
Changed:
U zc.async/trunk/setup.py
U zc.async/trunk/src/zc/async/CHANGES.txt
U zc.async/trunk/src/zc/async/interfaces.py
U zc.async/trunk/src/zc/async/subscribers.py
U zc.async/trunk/src/zc/async/subscribers.txt
-=-
Modified: zc.async/trunk/setup.py
===================================================================
--- zc.async/trunk/setup.py 2008-04-18 22:19:06 UTC (rev 85478)
+++ zc.async/trunk/setup.py 2008-04-18 22:30:04 UTC (rev 85479)
@@ -15,7 +15,7 @@
setup(
name='zc.async',
- version='1.0',
+ version='1.1',
packages=find_packages('src'),
package_dir={'':'src'},
zip_safe=False,
Modified: zc.async/trunk/src/zc/async/CHANGES.txt
===================================================================
--- zc.async/trunk/src/zc/async/CHANGES.txt 2008-04-18 22:19:06 UTC (rev 85478)
+++ zc.async/trunk/src/zc/async/CHANGES.txt 2008-04-18 22:30:04 UTC (rev 85479)
@@ -1,4 +1,11 @@
+1.1 (unreleased)
+================
+
+Fired events when the IQueues and IQueue objects are installed by the
+QueueInstaller.
+
+
1.0 (2008-04-09)
================
-Initial release.
\ No newline at end of file
+Initial release.
Modified: zc.async/trunk/src/zc/async/interfaces.py
===================================================================
--- zc.async/trunk/src/zc/async/interfaces.py 2008-04-18 22:19:06 UTC (rev 85478)
+++ zc.async/trunk/src/zc/async/interfaces.py 2008-04-18 22:30:04 UTC (rev 85479)
@@ -104,6 +104,23 @@
class DispatcherDeactivated(AbstractObjectEvent):
zope.interface.implements(IDispatcherDeactivated)
+class IObjectAdded(IObjectEvent):
+ """Object was added to the database"""
+
+ parent = zope.interface.Attribute(
+ 'container to which the object was added')
+
+ name = zope.interface.Attribute(
+ 'name of the object within the container')
+
+class ObjectAdded(AbstractObjectEvent):
+ zope.interface.implements(IObjectAdded)
+
+ def __init__(self, object, parent, name):
+ super(ObjectAdded, self).__init__(object)
+ self.parent = parent
+ self.name = name
+
class AbortedError(Exception):
"""An explicit abort, as generated by the default behavior of
IJob.fail"""
Modified: zc.async/trunk/src/zc/async/subscribers.py
===================================================================
--- zc.async/trunk/src/zc/async/subscribers.py 2008-04-18 22:19:06 UTC (rev 85478)
+++ zc.async/trunk/src/zc/async/subscribers.py 2008-04-18 22:30:04 UTC (rev 85479)
@@ -3,6 +3,7 @@
import transaction
import twisted.internet.selectreactor
import zope.component
+import zope.event
import zc.twist
import zc.async.interfaces
@@ -40,13 +41,18 @@
else:
queues = zc.async.queue.Queues()
root[zc.async.interfaces.KEY] = queues
+ zope.event.notify(zc.async.interfaces.ObjectAdded(
+ queues, root, zc.async.interfaces.KEY))
tm.commit()
zc.async.utils.log.info('queues collection added')
else:
queues = root[zc.async.interfaces.KEY]
for queue_name in self.queues:
if queue_name not in queues:
- queues[queue_name] = self.factory(conn, queue_name)
+ queue = self.factory(conn, queue_name)
+ queues[queue_name] = queue
+ zope.event.notify(zc.async.interfaces.ObjectAdded(
+ queue, queues, queue_name))
tm.commit()
zc.async.utils.log.info('queue %r added', queue_name)
except:
@@ -131,4 +137,4 @@
self.agent_name,
dispatcher.parent.name)
-agent_installer = AgentInstaller('main')
\ No newline at end of file
+agent_installer = AgentInstaller('main')
Modified: zc.async/trunk/src/zc/async/subscribers.txt
===================================================================
--- zc.async/trunk/src/zc/async/subscribers.txt 2008-04-18 22:19:06 UTC (rev 85478)
+++ zc.async/trunk/src/zc/async/subscribers.txt 2008-04-18 22:30:04 UTC (rev 85479)
@@ -96,6 +96,20 @@
>>> zc.async.subscribers.agent_installer.size
3
+When an IQueues or IQueue is installed, an event is fired that provides the
+object being added, the container it is added to, and the name under which it
+is added. Let's add a subscriber that shows us these events:
+
+ >>> def show_object_added(event):
+ ... if zc.async.interfaces.IObjectAdded.providedBy(event):
+ ... print "----"
+ ... print event.object.__class__, "object"
+ ... print "added to", event.parent.__class__
+ ... print "with name", repr(event.name)
+
+ >>> import zope.event
+ >>> zope.event.subscribers.append(show_object_added)
+
Now we can install the subscribers and give it a try. As we said above,
normally the database opened event only fires once; this is just for purpose of
demonstration. We unregister the previous handler so nothing gets confused.
@@ -108,6 +122,14 @@
>>> zope.component.provideHandler(
... zc.async.subscribers.agent_installer)
>>> zope.event.notify(zc.async.interfaces.DatabaseOpened(db))
+ ----
+ <class 'zc.async.queue.Queues'> object
+ added to <class 'persistent.mapping.PersistentMapping'>
+ with name 'zc.async'
+ ----
+ <class 'zc.async.queue.Queue'> object
+ added to <class 'zc.async.queue.Queues'>
+ with name ''
Now if we look in the database, we'll find a queues collection in another
database, with a queue, with a dispatcher, with an agent.
@@ -166,6 +188,10 @@
>>> bool(da.activated)
False
+Let's clean up the subscriber we added:
+
+ >>> zope.event.subscribers.remove(show_object_added)
+
.. ......... ..
.. Footnotes ..
.. ......... ..
More information about the Checkins
mailing list