[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/uniqueid/ Added query methods.

Jim Fulton jim at zope.com
Sun Aug 29 16:55:45 EDT 2004


Log message for revision 27343:
  Added query methods.
  
  Fixed hash method to return an integer, as required.
  
  Added an object-add event subscriber to register added
  objects and to propigate a unique-id-added event.
  


Changed:
  U   Zope3/trunk/src/zope/app/uniqueid/__init__.py
  U   Zope3/trunk/src/zope/app/uniqueid/configure.zcml
  U   Zope3/trunk/src/zope/app/uniqueid/ftests.py
  U   Zope3/trunk/src/zope/app/uniqueid/interfaces.py
  U   Zope3/trunk/src/zope/app/uniqueid/tests.py


-=-
Modified: Zope3/trunk/src/zope/app/uniqueid/__init__.py
===================================================================
--- Zope3/trunk/src/zope/app/uniqueid/__init__.py	2004-08-29 20:55:38 UTC (rev 27342)
+++ Zope3/trunk/src/zope/app/uniqueid/__init__.py	2004-08-29 20:55:45 UTC (rev 27343)
@@ -21,20 +21,23 @@
 $Id$
 """
 import random
-
-from BTrees import OIBTree, IOBTree
+from BTrees import IOBTree, OIBTree
+from ZODB.interfaces import IConnection
 from persistent import Persistent
-from ZODB.interfaces import IConnection
 
+from zope.event import notify
+from zope.interface import implements
+from zope.security.proxy import removeSecurityProxy
+
 from zope.app.container.contained import Contained
 from zope.app.uniqueid.interfaces import IUniqueIdUtility, IReference
 from zope.app.uniqueid.interfaces import UniqueIdRemovedEvent
-from zope.interface import implements
+from zope.app.uniqueid.interfaces import UniqueIdAddedEvent
 from zope.app import zapi
-from zope.security.proxy import removeSecurityProxy
-from zope.event import notify
+from zope.app.location.interfaces import ILocation
 
 
+
 class UniqueIdUtility(Persistent, Contained):
     """This utility provides a two way mapping between objects and
     integer ids.
@@ -58,10 +61,20 @@
     def getObject(self, id):
         return self.refs[id]()
 
+    def queryObject(self, id, default=None):
+        r = self.refs.get(id)
+        if r is not None:
+            return r()
+        return default
+
     def getId(self, ob):
         ref = IReference(ob)
         return self.ids[ref]
 
+    def queryId(self, ob, default=None):
+        ref = IReference(ob)
+        return self.ids.get(ref, default)
+
     def _generateId(self):
         """Generate an id which is not yet taken.
 
@@ -79,6 +92,7 @@
             self._v_nextid = None
 
     def register(self, ob):
+        # Note that we'll still need to keep this proxy removal.
         ob = removeSecurityProxy(ob)
         ref = IReference(ob)
         if ref in self.ids:
@@ -103,21 +117,21 @@
     implements(IReference)
 
     def __init__(self, object):
-        self.object = object
         if not getattr(object, '_p_oid', None):
             IConnection(object).add(object)
+        self.object = object
 
     def __call__(self):
         return self.object
 
     def __hash__(self):
-        return self.object._p_oid
+        return hash(self.object._p_oid)
 
     def __cmp__(self, other):
         if not isinstance(other, ReferenceToPersistent):
             raise TypeError("Cannot compare ReferenceToPersistent with %r" %
                             (other,))
-        return cmp(self.__hash__(), other.__hash__())
+        return cmp(self.object._p_oid, other.object._p_oid)
 
 
 def connectionOfPersistent(ob):
@@ -152,3 +166,13 @@
         except KeyError:
             pass
 
+def addUniqueIdSubscriber(event):
+    """A subscriber to ObjectAddedEvent
+
+    Registers the object added in all unique id utilities and fires
+    an event for the catalogs.
+    """
+    for utility in zapi.getAllUtilitiesRegisteredFor(IUniqueIdUtility):
+        utility.register(event.object)
+
+    notify(UniqueIdAddedEvent(event))

Modified: Zope3/trunk/src/zope/app/uniqueid/configure.zcml
===================================================================
--- Zope3/trunk/src/zope/app/uniqueid/configure.zcml	2004-08-29 20:55:38 UTC (rev 27342)
+++ Zope3/trunk/src/zope/app/uniqueid/configure.zcml	2004-08-29 20:55:45 UTC (rev 27343)
@@ -2,10 +2,15 @@
 
   <adapter
       for="persistent.interfaces.IPersistent"
-      provides="zope.app.uniqueid.interfaces.IReference"
-      factory="zope.app.uniqueid.ReferenceToPersistent"
+      provides=".interfaces.IReference"
+      factory=".ReferenceToPersistent"
+      trusted="y"
       />
 
+  <class class=".ReferenceToPersistent">
+    <require permission="zope.Public" interface=".interfaces.IReference" />
+  </class>
+
   <adapter
       for="persistent.interfaces.IPersistent"
       provides="ZODB.interfaces.IConnection"
@@ -41,18 +46,16 @@
 
   </content>
 
-  <content class=".ReferenceToPersistent">
-    <require
-        permission="zope.Public"
-        interface=".interfaces.IReference"
-        />
-  </content>
-
   <subscriber
       factory=".removeUniqueIdSubscriber"
       for="zope.app.container.interfaces.IObjectRemovedEvent"
       />
 
+  <subscriber
+      factory=".addUniqueIdSubscriber"
+      for="zope.app.container.interfaces.IObjectAddedEvent"
+      />
+
   <!-- Views -->
   <include package=".browser" />
 

Modified: Zope3/trunk/src/zope/app/uniqueid/ftests.py
===================================================================
--- Zope3/trunk/src/zope/app/uniqueid/ftests.py	2004-08-29 20:55:38 UTC (rev 27342)
+++ Zope3/trunk/src/zope/app/uniqueid/ftests.py	2004-08-29 20:55:45 UTC (rev 27343)
@@ -61,7 +61,8 @@
         response = self.publish(self.basepath + '/uniqueid/@@index.html',
                                 basic='mgr:mgrpw')
         self.assertEquals(response.getStatus(), 200)
-        self.assert_(response.getBody().find('0 objects') > 0)
+        # The utility registers in itself when it is being added
+        self.assert_(response.getBody().find('1 objects') > 0)
         self.assert_('<a href="/++etc++site">/++etc++site</a>'
                      not in response.getBody())
 
@@ -73,7 +74,7 @@
                                 basic='mgr:mgrpw')
         self.assertEquals(response.getStatus(), 200)
         body = response.getBody()
-        self.assert_('2 objects' in body)
+        self.assert_('3 objects' in body)
         self.assert_('<a href="/++etc++site">/++etc++site</a>' in body)
         self.checkForBrokenLinks(body, response.getPath(), basic='mgr:mgrpw')
 

Modified: Zope3/trunk/src/zope/app/uniqueid/interfaces.py
===================================================================
--- Zope3/trunk/src/zope/app/uniqueid/interfaces.py	2004-08-29 20:55:38 UTC (rev 27342)
+++ Zope3/trunk/src/zope/app/uniqueid/interfaces.py	2004-08-29 20:55:45 UTC (rev 27343)
@@ -26,10 +26,20 @@
 
     def getId(ob):
         """Get a unique id of an object.
+        """
 
-        If the id for an object is unknown, ValueError is raised.
+    def queryObject(uid, default=None):
+        """Return an object by its unique id
+
+        Return the default if the uid isn't registered
         """
 
+    def queryId(ob, default=None):
+        """Get a unique id of an object.
+
+        Return the default if the object isn't registered
+        """
+
 class IUniqueIdUtilitySet(Interface):
 
     def register(ob):
@@ -80,3 +90,19 @@
 
     def __init__(self, event):
         self.original_event = event
+
+
+class IUniqueIdAddedEvent(Interface):
+    """The event which gets sent when an object is registered in a
+    unique id utility.
+    """
+    original_event = Attribute("The ObjectAddedEvent related to this event")
+
+
+class UniqueIdAddedEvent:
+    """The event which gets sent when an object is registered in a
+    unique id utility.
+    """
+    implements(IUniqueIdAddedEvent)
+    def __init__(self, event):
+        self.original_event = event

Modified: Zope3/trunk/src/zope/app/uniqueid/tests.py
===================================================================
--- Zope3/trunk/src/zope/app/uniqueid/tests.py	2004-08-29 20:55:38 UTC (rev 27342)
+++ Zope3/trunk/src/zope/app/uniqueid/tests.py	2004-08-29 20:55:45 UTC (rev 27343)
@@ -70,9 +70,16 @@
         obj = P()
         obj._p_jar = ConnectionStub()
 
+        self.assert_(u.queryId(obj) is None)
+        self.assert_(u.queryId(obj, 42) is 42)
+        self.assert_(u.queryObject(42) is None)
+        self.assert_(u.queryObject(42, obj) is obj)
+
         uid = u.register(obj)
         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)
         self.assertEquals(uid, uid2)
@@ -191,7 +198,7 @@
         self.assertRaises(ValueError, connectionOfPersistent, object())
 
 
-class TestRemoveSubscriber(ReferenceSetupMixin, unittest.TestCase):
+class TestSubscribers(ReferenceSetupMixin, unittest.TestCase):
 
     def setUp(self):
         from zope.app.uniqueid.interfaces import IUniqueIdUtility
@@ -215,7 +222,7 @@
         self.utility1 = setup.addUtility(sm1_1, '2', IUniqueIdUtility,
                                          UniqueIdUtility())
 
-    def test(self):
+    def test_removeUniqueIdSubscriber(self):
         from zope.app.uniqueid import removeUniqueIdSubscriber
         from zope.app.container.contained import ObjectRemovedEvent
         from zope.app.uniqueid.interfaces import IUniqueIdRemovedEvent
@@ -239,13 +246,34 @@
         self.assertEquals(len(events), 1)
         self.assertEquals(events[0].original_event.object, folder)
 
+    def test_addUniqueIdSubscriber(self):
+        from zope.app.uniqueid import addUniqueIdSubscriber
+        from zope.app.container.contained import ObjectAddedEvent
+        from zope.app.uniqueid.interfaces import IUniqueIdAddedEvent
+        folder = self.root['folder1']['folder1_1']['folder1_1_1']
+        setSite(self.folder1_1)
 
+        events = []
+        ztapi.handle([IUniqueIdAddedEvent], events.append)
+
+        # This should unregister the object in all utilities, not just the
+        # nearest one.
+        addUniqueIdSubscriber(ObjectAddedEvent(folder))
+
+        # Check that the folder got registered
+        id = self.utility.getId(folder)
+        id1 = self.utility1.getId(folder)
+
+        self.assertEquals(len(events), 1)
+        self.assertEquals(events[0].original_event.object, folder)
+
+
 def test_suite():
     suite = unittest.TestSuite()
     suite.addTest(unittest.makeSuite(TestUniqueIdUtility))
     suite.addTest(unittest.makeSuite(TestReferenceToPersistent))
     suite.addTest(unittest.makeSuite(TestConnectionOfPersistent))
-    suite.addTest(unittest.makeSuite(TestRemoveSubscriber))
+    suite.addTest(unittest.makeSuite(TestSubscribers))
     return suite
 
 if __name__ == '__main__':



More information about the Zope3-Checkins mailing list