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

Jim Fulton jim at zope.com
Wed Aug 18 15:18:31 EDT 2004


Log message for revision 27174:
  Added query methods.
  


Changed:
  U   Zope3/branches/jim-index/src/zope/app/uniqueid/__init__.py
  U   Zope3/branches/jim-index/src/zope/app/uniqueid/interfaces.py
  U   Zope3/branches/jim-index/src/zope/app/uniqueid/tests.py


-=-
Modified: Zope3/branches/jim-index/src/zope/app/uniqueid/__init__.py
===================================================================
--- Zope3/branches/jim-index/src/zope/app/uniqueid/__init__.py	2004-08-18 19:17:49 UTC (rev 27173)
+++ Zope3/branches/jim-index/src/zope/app/uniqueid/__init__.py	2004-08-18 19:18:30 UTC (rev 27174)
@@ -31,6 +31,8 @@
 from BTrees import OIBTree, IOBTree
 from zope.app import zapi
 from zope.app.location.interfaces import ILocation
+
+# XXX get rid of this 
 from zope.security.proxy import trustedRemoveSecurityProxy
 from zope.event import notify
 
@@ -60,10 +62,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 = zapi.getAdapter(ob, IReference)
         return self.ids[ref]
 
+    def queryId(self, ob, default=None):
+        ref = zapi.getAdapter(ob, IReference)
+        return self.ids.get(ref, default)
+
     def _generateId(self):
         """Generate an id which is not yet taken.
 
@@ -81,6 +93,8 @@
             self._v_nextid = None
 
     def register(self, ob):
+
+        # Note that we'll still need to keep this proxy removal.
         ob = trustedRemoveSecurityProxy(ob)
         ref = zapi.getAdapter(ob, IReference)
         if ref in self.ids:
@@ -105,9 +119,11 @@
     implements(IReference)
 
     def __init__(self, object):
+        # XXX This should be a trusted adapter
+        object = trustedRemoveSecurityProxy(object)
+        if not getattr(object, '_p_oid', None):
+            IConnection(object).add(object)
         self.object = object
-        if not getattr(object, '_p_oid', None):
-            zapi.getAdapter(object, IConnection).add(object)
 
     def __call__(self):
         return self.object

Modified: Zope3/branches/jim-index/src/zope/app/uniqueid/interfaces.py
===================================================================
--- Zope3/branches/jim-index/src/zope/app/uniqueid/interfaces.py	2004-08-18 19:17:49 UTC (rev 27173)
+++ Zope3/branches/jim-index/src/zope/app/uniqueid/interfaces.py	2004-08-18 19:18:30 UTC (rev 27174)
@@ -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):

Modified: Zope3/branches/jim-index/src/zope/app/uniqueid/tests.py
===================================================================
--- Zope3/branches/jim-index/src/zope/app/uniqueid/tests.py	2004-08-18 19:17:49 UTC (rev 27173)
+++ Zope3/branches/jim-index/src/zope/app/uniqueid/tests.py	2004-08-18 19:18:30 UTC (rev 27174)
@@ -71,9 +71,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)



More information about the Zope3-Checkins mailing list