[Zope3-checkins] SVN: Zope3/trunk/ Change zope.app.intid.addIntIdSubscriber and .removeIntIdSubscriber to adapt it's ob argument

Dominik Huber dominik.huber at projekt01.ch
Wed Jun 8 13:34:44 EDT 2005


Log message for revision 30691:
  Change zope.app.intid.addIntIdSubscriber and .removeIntIdSubscriber to adapt it's ob argument
  to IKeyReference. They register/unregister
  the object only if it get the adapter.

Changed:
  U   Zope3/trunk/doc/CHANGES.txt
  U   Zope3/trunk/src/zope/app/intid/__init__.py
  U   Zope3/trunk/src/zope/app/intid/browser/__init__.py
  U   Zope3/trunk/src/zope/app/intid/interfaces.py
  U   Zope3/trunk/src/zope/app/intid/tests.py

-=-
Modified: Zope3/trunk/doc/CHANGES.txt
===================================================================
--- Zope3/trunk/doc/CHANGES.txt	2005-06-08 16:04:29 UTC (rev 30690)
+++ Zope3/trunk/doc/CHANGES.txt	2005-06-08 17:34:43 UTC (rev 30691)
@@ -10,6 +10,10 @@
 
     New features
 
+      - Change zope.app.intid.addIntIdSubscriber and .removeIntIdSubscriber
+        to adapt it's ob argument to IKeyReference. They register/unregister
+        the object only if it get the adapter.
+
       - Add content_factory_id attribute to the addform and addwizard
         directive so that named factories can be invoked too.
 

Modified: Zope3/trunk/src/zope/app/intid/__init__.py
===================================================================
--- Zope3/trunk/src/zope/app/intid/__init__.py	2005-06-08 16:04:29 UTC (rev 30690)
+++ Zope3/trunk/src/zope/app/intid/__init__.py	2005-06-08 17:34:43 UTC (rev 30691)
@@ -73,12 +73,12 @@
 
     def getId(self, ob):
         try:
-            ref = IKeyReference(ob)
+            key = IKeyReference(ob)
         except NotYet:
             raise KeyError(ob)
 
         try:
-            return self.ids[ref]
+            return self.ids[key]
         except KeyError:
             raise KeyError(ob)
 
@@ -104,22 +104,22 @@
                 return uid
             self._v_nextid = None
 
-    def register(self, ob):
+    def register(self, key):
         # Note that we'll still need to keep this proxy removal.
-        ob = removeSecurityProxy(ob)
-        ref = IKeyReference(ob)
-        if ref in self.ids:
-            return self.ids[ref]
+        key = removeSecurityProxy(key)
+        if key in self.ids:
+            return self.ids[key]
         uid = self._generateId()
-        self.refs[uid] = ref
-        self.ids[ref] = uid
+        self.refs[uid] = key
+        self.ids[key] = uid
         return uid
 
-    def unregister(self, ob):
-        ref = IKeyReference(ob)
-        uid = self.ids[ref]
+    def unregister(self, key):
+        # Note that we'll still need to keep this proxy removal.
+        key = removeSecurityProxy(key)
+        uid = self.ids[key]
         del self.refs[uid]
-        del self.ids[ref]
+        del self.ids[key]
 
 
 def removeIntIdSubscriber(ob, event):
@@ -129,16 +129,20 @@
     id utilities.
     """
 
-    # Notify the catalogs that this object is about to be removed.
-    notify(IntIdRemovedEvent(ob, event))
+    utilities = tuple(zapi.getAllUtilitiesRegisteredFor(IIntIds))
+    if utilities:
+        key = IKeyReference(ob, None)
+        # Register only objects that adapt to key reference
+        if key is not None:
+            # Notify the catalogs that this object is about to be removed.
+            notify(IntIdRemovedEvent(ob, event))
+            for utility in utilities:
+                try:
+                    utility.unregister(key)
+                except KeyError:
+                    pass
 
-    for utility in zapi.getAllUtilitiesRegisteredFor(IIntIds, ob):
-        try:
-            utility.unregister(ob)
-        except KeyError:
-            pass
 
-
 def addIntIdSubscriber(ob, event):
     """A subscriber to ObjectAddedEvent
 
@@ -146,13 +150,16 @@
     an event for the catalogs.
     """
 
-    for utility in zapi.getAllUtilitiesRegisteredFor(IIntIds):
-        utility.register(ob)
+    utilities = tuple(zapi.getAllUtilitiesRegisteredFor(IIntIds))
+    if utilities: # assert that there are any utilites
+        key = IKeyReference(ob, None)
+        # Register only objects that adapt to key reference
+        if key is not None:
+            for utility in utilities:
+                utility.register(key)
+            # Notify the catalogs that this object was added.
+            notify(IntIdAddedEvent(ob, event))
 
-    # Notify the catalogs that this object was added.
-    notify(IntIdAddedEvent(ob, event))
-
-
 # BBB
 UniqueIdUtility = IntIds
 import zope.app.keyreference.persistent

Modified: Zope3/trunk/src/zope/app/intid/browser/__init__.py
===================================================================
--- Zope3/trunk/src/zope/app/intid/browser/__init__.py	2005-06-08 16:04:29 UTC (rev 30690)
+++ Zope3/trunk/src/zope/app/intid/browser/__init__.py	2005-06-08 17:34:43 UTC (rev 30691)
@@ -17,6 +17,7 @@
 """
 from zope.security.proxy import removeSecurityProxy
 from zope.app import zapi
+from zope.app.keyreference.interfaces import IKeyReference
     
 
 class IntIdsView(object):
@@ -26,8 +27,8 @@
 
     def populate(self):
         # TODO: I think this should be moved to the functional test.
-        self.context.register(zapi.traverse(self.context, "/"))
-        self.context.register(zapi.traverse(self.context, "/++etc++site"))
+        self.context.register(IKeyReference(zapi.traverse(self.context, "/")))
+        self.context.register(IKeyReference(zapi.traverse(self.context, "/++etc++site")))
         self.request.response.redirect('index.html')
 
     def items(self):  

Modified: Zope3/trunk/src/zope/app/intid/interfaces.py
===================================================================
--- Zope3/trunk/src/zope/app/intid/interfaces.py	2005-06-08 16:04:29 UTC (rev 30690)
+++ Zope3/trunk/src/zope/app/intid/interfaces.py	2005-06-08 17:34:43 UTC (rev 30691)
@@ -33,16 +33,18 @@
 
 class IIntIdsSet(Interface):
 
-    def register(ob):
-        """Register an object and returns a unique id generated for it.
+    def register(key):
+        """Register a key reference and returns a unique id generated for it.
 
-        If the object is already registered, its id is returned anyway.
+        If the object adapted to key reference is already registered, its 
+        id is returned anyway.
         """
 
-    def unregister(ob):
-        """Remove the object from the indexes.
+    def unregister(key):
+        """Remove the key reference from the indexes.
 
-        ValueError is raised if ob is not registered previously.
+        ValueError is raised if the object adapted to key reference is not
+        registered previously.
         """
 
 class IIntIdsManage(Interface):

Modified: Zope3/trunk/src/zope/app/intid/tests.py
===================================================================
--- Zope3/trunk/src/zope/app/intid/tests.py	2005-06-08 16:04:29 UTC (rev 30690)
+++ Zope3/trunk/src/zope/app/intid/tests.py	2005-06-08 17:34:43 UTC (rev 30691)
@@ -67,6 +67,7 @@
 
     def test(self):
         from zope.app.intid import IntIds
+        from zope.app.keyreference.interfaces import IKeyReference
 
         u = IntIds()
         obj = P()
@@ -84,21 +85,24 @@
         self.assert_(u.queryObject(42) is None)
         self.assert_(u.queryObject(42, obj) is obj)
 
-        uid = u.register(obj)
+        key = IKeyReference(obj)
+
+        uid = u.register(key)
         self.assert_(u.getObject(uid) is obj)
         self.assert_(u.queryObject(uid) is obj)
         self.assertEquals(u.getId(obj), uid)
         self.assertEquals(u.queryId(obj), uid)
 
-        uid2 = u.register(obj)
+        uid2 = u.register(key)
         self.assertEquals(uid, uid2)
 
-        u.unregister(obj)
+        u.unregister(key)
         self.assertRaises(KeyError, u.getObject, uid)
         self.assertRaises(KeyError, u.getId, obj)
 
     def test_len_items(self):
         from zope.app.intid import IntIds
+        from zope.app.keyreference.interfaces import IKeyReference
         from zope.app.keyreference.persistent import KeyReferenceToPersistent
         u = IntIds()
         obj = P()
@@ -108,7 +112,9 @@
         self.assertEquals(u.items(), [])
         self.assertEquals(list(u), [])
 
-        uid = u.register(obj)
+        key = IKeyReference(obj)
+
+        uid = u.register(key)
         ref = KeyReferenceToPersistent(obj)
         self.assertEquals(len(u), 1)
         self.assertEquals(u.items(), [(uid, ref)])
@@ -117,7 +123,9 @@
         obj2 = P()
         obj2.__parent__ = obj
 
-        uid2 = u.register(obj2)
+        key2 = IKeyReference(obj2)
+
+        uid2 = u.register(key2)
         ref2 = KeyReferenceToPersistent(obj2)
         self.assertEquals(len(u), 2)
         result = u.items()
@@ -131,8 +139,8 @@
         expected.sort()
         self.assertEquals(result, expected)
 
-        u.unregister(obj)
-        u.unregister(obj2)
+        u.unregister(key)
+        u.unregister(key2)
         self.assertEquals(len(u), 0)
         self.assertEquals(u.items(), [])
 
@@ -173,13 +181,15 @@
         self.utility1 = setup.addUtility(sm1_1, '2', IIntIds, IntIds())
 
     def test_removeIntIdSubscriber(self):
+        from zope.app.container.contained import ObjectRemovedEvent
         from zope.app.intid import removeIntIdSubscriber
-        from zope.app.container.contained import ObjectRemovedEvent
         from zope.app.intid.interfaces import IIntIdRemovedEvent
+        from zope.app.keyreference.interfaces import IKeyReference
         parent_folder = self.root['folder1']['folder1_1']
         folder = self.root['folder1']['folder1_1']['folder1_1_1']
-        id = self.utility.register(folder)
-        id1 = self.utility1.register(folder)
+        key = IKeyReference(folder)
+        id = self.utility.register(key)
+        id1 = self.utility1.register(key)
         self.assertEquals(self.utility.getObject(id), folder)
         self.assertEquals(self.utility1.getObject(id1), folder)
         setSite(self.folder1_1)
@@ -221,6 +231,7 @@
         self.assertEquals(events[0].original_event.object, parent_folder)
         self.assertEquals(events[0].object, folder)
 
+
 def test_suite():
     suite = unittest.TestSuite()
     suite.addTest(unittest.makeSuite(TestIntIds))



More information about the Zope3-Checkins mailing list