[Zope3-checkins] SVN: Zope3/trunk/src/zope/ Move last
zope.app.event things to zope.component.event.
Florent Xicluna
laxyf at yahoo.fr
Sat Sep 16 12:04:05 EDT 2006
Log message for revision 70206:
Move last zope.app.event things to zope.component.event.
Rewrite ObjectEvents tests with doctest.
Changed:
U Zope3/trunk/src/zope/app/apidoc/enabled.zcml
U Zope3/trunk/src/zope/app/configure.zcml
D Zope3/trunk/src/zope/app/event/configure.zcml
D Zope3/trunk/src/zope/app/event/tests/test_objectevent.py
A Zope3/trunk/src/zope/component/configure.zcml
U Zope3/trunk/src/zope/component/event.txt
UU Zope3/trunk/src/zope/component/tests.py
-=-
Modified: Zope3/trunk/src/zope/app/apidoc/enabled.zcml
===================================================================
--- Zope3/trunk/src/zope/app/apidoc/enabled.zcml 2006-09-16 12:43:33 UTC (rev 70205)
+++ Zope3/trunk/src/zope/app/apidoc/enabled.zcml 2006-09-16 16:04:04 UTC (rev 70206)
@@ -9,12 +9,14 @@
title="[zope.app.apidoc.UseAPIDoc] Access Online API documentation"
/>
+ <!-- BBB 2006/09/14, to be removed after 12 months -->
<permission
id="zope.app.introspector.Introspect"
title=
"[zope.app.introspector.Introspect] Introspect Object Classes and Interfaces"
/>
+ <!-- BBB 2006/09/14, to be removed after 12 months -->
<meta:redefinePermission
from="zope.app.introspector.Introspect"
to="zope.app.apidoc.UseAPIDoc"
Modified: Zope3/trunk/src/zope/app/configure.zcml
===================================================================
--- Zope3/trunk/src/zope/app/configure.zcml 2006-09-16 12:43:33 UTC (rev 70205)
+++ Zope3/trunk/src/zope/app/configure.zcml 2006-09-16 16:04:04 UTC (rev 70206)
@@ -17,7 +17,7 @@
<!-- Ordinary Application (non-view) configuration) -->
<include package="zope.app.interface" />
<include package="zope.app.security" />
- <include package="zope.app.event" />
+ <include package="zope.component" />
<include package="zope.annotation" />
<include package="zope.app.dependable" />
<include package="zope.app.content" />
Deleted: Zope3/trunk/src/zope/app/event/configure.zcml
===================================================================
--- Zope3/trunk/src/zope/app/event/configure.zcml 2006-09-16 12:43:33 UTC (rev 70205)
+++ Zope3/trunk/src/zope/app/event/configure.zcml 2006-09-16 16:04:04 UTC (rev 70206)
@@ -1,12 +0,0 @@
-<configure xmlns="http://namespaces.zope.org/zope">
-
- <subscriber handler="zope.component.event.objectEventNotify" />
-
- <configure package="zope.component.registry">
- <subscriber handler=".dispatchUtilityRegistrationEvent" />
- <subscriber handler=".dispatchAdapterRegistrationEvent" />
- <subscriber handler=".dispatchSubscriptionAdapterRegistrationEvent" />
- <subscriber handler=".dispatchHandlerRegistrationEvent" />
- </configure>
-
-</configure>
Deleted: Zope3/trunk/src/zope/app/event/tests/test_objectevent.py
===================================================================
--- Zope3/trunk/src/zope/app/event/tests/test_objectevent.py 2006-09-16 12:43:33 UTC (rev 70205)
+++ Zope3/trunk/src/zope/app/event/tests/test_objectevent.py 2006-09-16 16:04:04 UTC (rev 70206)
@@ -1,86 +0,0 @@
-##############################################################################
-#
-# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
-# All Rights Reserved.
-#
-# This software is subject to the provisions of the Zope Public License,
-# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
-# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
-# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
-# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
-# FOR A PARTICULAR PURPOSE.
-#
-##############################################################################
-"""Object Event Tests
-
-$Id$
-"""
-import unittest
-import zope.component.event
-from zope.testing import doctest
-
-from zope.app.container.contained import Contained, ObjectRemovedEvent
-from zope.app.container.interfaces import IContained, IObjectRemovedEvent
-from zope.app.container.sample import SampleContainer
-from zope.app.testing.placelesssetup import setUp, tearDown
-from zope.app.testing import ztapi
-
-class TestObjectEventNotifications(unittest.TestCase):
-
- def setUp(self):
- self.callbackTriggered = False
- setUp()
-
- def tearDown(self):
- tearDown()
-
- def testNotify(self):
- events = []
-
- def record(*args):
- events.append(args)
-
- ztapi.subscribe([IContained, IObjectRemovedEvent], None, record)
-
- item = Contained()
- event = ObjectRemovedEvent(item)
- zope.component.event.objectEventNotify(event)
- self.assertEqual([(item, event)], events)
-
- def testNotifyNobody(self):
- # Check that notify won't raise an exception in absence of
- # of subscribers.
- events = []
- item = Contained()
- evt = ObjectRemovedEvent(item)
- zope.component.event.objectEventNotify(evt)
- self.assertEqual([], events)
-
- def testVeto(self):
- zope.component.provideHandler(zope.component.event.objectEventNotify)
- container = SampleContainer()
- item = Contained()
-
- # This will fire an event.
- container['Fred'] = item
-
- class Veto(Exception):
- pass
-
- def callback(item, event):
- self.callbackTriggered = True
- self.assertEqual(item, event.object)
- raise Veto
-
- ztapi.subscribe([IContained, IObjectRemovedEvent], None, callback)
-
- # del container['Fred'] will fire an ObjectRemovedEvent event.
- self.assertRaises(Veto, container.__delitem__, 'Fred')
-
-def test_suite():
- return unittest.TestSuite((
- unittest.makeSuite(TestObjectEventNotifications),
- ))
-
-if __name__=='__main__':
- unittest.main(defaultTest='test_suite')
Copied: Zope3/trunk/src/zope/component/configure.zcml (from rev 70173, Zope3/trunk/src/zope/app/event/configure.zcml)
===================================================================
--- Zope3/trunk/src/zope/app/event/configure.zcml 2006-09-14 12:34:21 UTC (rev 70173)
+++ Zope3/trunk/src/zope/component/configure.zcml 2006-09-16 16:04:04 UTC (rev 70206)
@@ -0,0 +1,10 @@
+<configure xmlns="http://namespaces.zope.org/zope">
+
+ <subscriber handler=".event.objectEventNotify" />
+
+ <subscriber handler=".registry.dispatchUtilityRegistrationEvent" />
+ <subscriber handler=".registry.dispatchAdapterRegistrationEvent" />
+ <subscriber handler=".registry.dispatchSubscriptionAdapterRegistrationEvent" />
+ <subscriber handler=".registry.dispatchHandlerRegistrationEvent" />
+
+</configure>
Modified: Zope3/trunk/src/zope/component/event.txt
===================================================================
--- Zope3/trunk/src/zope/component/event.txt 2006-09-16 12:43:33 UTC (rev 70205)
+++ Zope3/trunk/src/zope/component/event.txt 2006-09-16 16:04:04 UTC (rev 70206)
@@ -36,7 +36,7 @@
>>> zope.component.provideHandler(handler1)
>>> zope.component.provideHandler(handler2)
-Now let's through the events. We'll see that the handlers have been
+Now let's go through the events. We'll see that the handlers have been
called accordingly:
>>> from zope.event import notify
@@ -49,3 +49,94 @@
>>> called.sort()
>>> called
[1, 2]
+
+
+
+Object events
+-------------
+
+
+The ``objectEventNotify`` function is a subscriber to dispatch
+ObjectEvents to interested adapters.
+
+First create an object class:
+
+ >>> class IUseless(zope.interface.Interface):
+ ... """Useless object"""
+
+ >>> class UselessObject(object):
+ ... """Useless object"""
+ ... zope.interface.implements(IUseless)
+
+Then create an event class:
+
+ >>> class IObjectThrownEvent(zope.component.interfaces.IObjectEvent):
+ ... """An object has been thrown away"""
+
+ >>> class ObjectThrownEvent(zope.component.interfaces.ObjectEvent):
+ ... """An object has been thrown away"""
+ ... zope.interface.implements(IObjectThrownEvent)
+
+Create an object and an event:
+
+ >>> hammer = UselessObject()
+ >>> event = ObjectThrownEvent(hammer)
+
+Then notify the event to the subscribers.
+Since the subscribers list is empty, nothing happens.
+
+ >>> zope.component.event.objectEventNotify(event)
+
+Now create an handler for the event:
+
+ >>> events = []
+ >>> def record(*args):
+ ... events.append(args)
+
+ >>> zope.component.provideHandler(record, [IUseless, IObjectThrownEvent])
+
+The event is notified to the subscriber:
+
+ >>> zope.component.event.objectEventNotify(event)
+ >>> events == [(hammer, event)]
+ True
+
+Following test demonstrates how a subscriber can raise an exception
+to prevent an action.
+
+ >>> zope.component.provideHandler(zope.component.event.objectEventNotify)
+
+Let's create a container:
+
+ >>> 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:
+
+ >>> container['Red Hammer'] = hammer
+
+Create an handler function that will raise an error when called:
+
+ >>> class Veto(Exception):
+ ... pass
+
+ >>> def callback(item, event):
+ ... assert(item == event.object)
+ ... raise Veto
+
+Register the handler:
+
+ >>> zope.component.provideHandler(callback, [IUseless, IObjectThrownEvent])
+
+Then if we try to remove the object, an ObjectThrownEvent is fired:
+
+ >>> del container['Red Hammer']
+ ... # doctest: +NORMALIZE_WHITESPACE
+ Traceback (most recent call last):
+ ...
+ raise Veto
+ Veto
Modified: Zope3/trunk/src/zope/component/tests.py
===================================================================
--- Zope3/trunk/src/zope/component/tests.py 2006-09-16 12:43:33 UTC (rev 70205)
+++ Zope3/trunk/src/zope/component/tests.py 2006-09-16 16:04:04 UTC (rev 70206)
@@ -13,7 +13,7 @@
##############################################################################
"""Component Architecture Tests
-$Id: test_api.py 28632 2004-12-16 17:42:59Z srichter $
+$Id$
"""
import re
import unittest
Property changes on: Zope3/trunk/src/zope/component/tests.py
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
More information about the Zope3-Checkins
mailing list