[Zope3-checkins] CVS: Zope3/src/zope/app/catalog - catalog.py:1.26
configure.zcml:1.10 tests.py:1.7
Mark McEahern
mark at mceahern.com
Tue Mar 23 10:52:29 EST 2004
Update of /cvs-repository/Zope3/src/zope/app/catalog
In directory cvs.zope.org:/tmp/cvs-serv2164/src/zope/app/catalog
Modified Files:
catalog.py configure.zcml tests.py
Log Message:
Refactored Catalog, refactoring IAddNotifiable and IRemoveNotifiable
to use subscriber events.
=== Zope3/src/zope/app/catalog/catalog.py 1.25 => 1.26 ===
--- Zope3/src/zope/app/catalog/catalog.py:1.25 Sat Mar 13 18:54:57 2004
+++ Zope3/src/zope/app/catalog/catalog.py Tue Mar 23 10:52:27 2004
@@ -50,11 +50,32 @@
obj = self.hub.getObject(hubid)
yield obj
+class CatalogBaseAddSubscriber:
+
+ implements(ISubscriber)
+
+ def __init__(self, catalog, event):
+ self.catalog = catalog
+
+ def notify(self, event):
+ """Receive notification of add events."""
+ self.catalog.subscribeEvents(update=False)
+
+class CatalogBaseRemoveSubscriber:
+
+ implements(ISubscriber)
+
+ def __init__(self, catalog, event):
+ self.catalog = catalog
+
+ def notify(self, event):
+ """Receive notification of remove events."""
+ if self.catalog.getSubscribed():
+ self.catalog.unsubscribeEvents()
class CatalogBase(Persistent, SampleContainer):
- implements(ICatalog, ISubscriber, IRemoveNotifiable,
- IAddNotifiable, IContainer, IAttributeAnnotatable)
+ implements(ICatalog, ISubscriber, IContainer, IAttributeAnnotatable)
_subscribed = False
@@ -63,14 +84,6 @@
def getSubscribed(self):
return self._subscribed
-
- def addNotify(self, event):
- self.subscribeEvents(update=False)
-
- def removeNotify(self, event):
- " be nice, unsub ourselves in this case "
- if self._subscribed:
- self.unsubscribeEvents()
def clearIndexes(self):
for index in self.values():
=== Zope3/src/zope/app/catalog/configure.zcml 1.9 => 1.10 ===
--- Zope3/src/zope/app/catalog/configure.zcml:1.9 Sat Mar 13 18:00:47 2004
+++ Zope3/src/zope/app/catalog/configure.zcml Tue Mar 23 10:52:27 2004
@@ -28,6 +28,20 @@
/>
</content>
+<subscriber
+ factory=".catalog.CatalogBaseRemoveSubscriber"
+ provides="zope.app.event.interfaces.ISubscriber"
+ for=".interfaces.ICatalog
+ zope.app.container.interfaces.IObjectRemovedEvent"
+ />
+
+<subscriber
+ factory=".catalog.CatalogBaseAddSubscriber"
+ provides="zope.app.event.interfaces.ISubscriber"
+ for=".interfaces.ICatalog
+ zope.app.container.interfaces.IObjectAddedEvent"
+ />
+
<content class=".catalog.CatalogUtility">
<factory
id="zope.app.catalogutility"
=== Zope3/src/zope/app/catalog/tests.py 1.6 => 1.7 ===
--- Zope3/src/zope/app/catalog/tests.py:1.6 Wed Mar 17 12:59:27 2004
+++ Zope3/src/zope/app/catalog/tests.py Tue Mar 23 10:52:27 2004
@@ -19,6 +19,7 @@
$Id$
"""
import unittest
+import doctest
from zope.interface import implements
from zope.app.index.interfaces.field import IUIFieldCatalogIndex
@@ -28,6 +29,8 @@
from zope.index.interfaces import ISimpleQuery
from zope.app.catalog.catalog import Catalog
+from zope.app.catalog.catalog import CatalogBaseAddSubscriber
+from zope.app.catalog.catalog import CatalogBaseRemoveSubscriber
from zope.app.tests.placelesssetup import PlacelessSetup
from zope.component import getServiceManager
from zope.app.servicenames import HubIds
@@ -80,6 +83,59 @@
def __init__(self, **kw):
self.__dict__ = kw
+class DummyCatalog:
+
+ def __init__(self):
+ self.subscribed = False
+
+ def subscribeEvents(self, update=False):
+ self.subscribed = True
+
+ def getSubscribed(self):
+ return self.subscribed
+
+ def unsubscribeEvents(self):
+ self.subscribed = False
+
+class TestEventAdapters:
+ def test_addNotify(self):
+ """
+ First we create a dummy catalog and an adapter for it.
+
+ >>> catalog = DummyCatalog()
+ >>> adapter = CatalogBaseAddSubscriber(catalog, None)
+
+ Now call notification
+ >>> adapter.notify(None)
+
+ Check to make sure the adapter added the path
+ >>> catalog.getSubscribed()
+ True
+ """
+
+ def test_deleteNotify(self):
+ """
+ First we create a dummy catalog and an adapter for it.
+
+ >>> catalog = DummyCatalog()
+ >>> adapter = CatalogBaseAddSubscriber(catalog, None)
+
+ Now call notification
+ >>> adapter.notify(None)
+
+ Check to make sure the adapter subscribed
+ >>> catalog.getSubscribed()
+ True
+
+ Now create a removal adapter and notify it
+ >>> adapter = CatalogBaseRemoveSubscriber(catalog, None)
+ >>> adapter.notify(None)
+
+ Check to make sure the adapter unsubscribed
+ >>> catalog.getSubscribed()
+ False
+ """
+
class Test(PlacelessSetup, unittest.TestCase):
def test_catalog_add_del_indexes(self):
@@ -183,8 +239,11 @@
res = list(res)
def test_suite():
- return unittest.makeSuite(Test)
-
+ import sys
+ return unittest.TestSuite((
+ unittest.makeSuite(Test),
+ doctest.DocTestSuite(sys.modules[__name__]),
+ ))
if __name__ == "__main__":
unittest.main()
More information about the Zope3-Checkins
mailing list