[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/ Registered the unique id utility in zope/app/configure.zcml.

Albertas Agejevas alga at pov.lt
Thu Jun 10 14:14:53 EDT 2004


Log message for revision 25339:
Registered the unique id utility in zope/app/configure.zcml.
Added a view for UniqueIdUtility showing the indexed objects.
Fixed a few bugs.



-=-
Modified: Zope3/trunk/src/zope/app/configure.zcml
===================================================================
--- Zope3/trunk/src/zope/app/configure.zcml	2004-06-10 18:07:11 UTC (rev 25338)
+++ Zope3/trunk/src/zope/app/configure.zcml	2004-06-10 18:14:52 UTC (rev 25339)
@@ -9,7 +9,7 @@
 
   <!-- Note that we need to do this early, as later startup
        subscribers may break without fixups -->
- 
+
   <include package="zope.app.component" />
 
   <include package=".generations" file="subscriber.zcml" />
@@ -22,7 +22,7 @@
   <include package="zope.app.annotation" />
   <include package="zope.app.dependable" />
   <include package="zope.app.content" />
-  
+
   <include file="menus.zcml" />
 
   <include package="zope.app.copypastemove" />
@@ -34,7 +34,7 @@
   <include package="zope.app.traversing" />
   <include package="zope.app.pagetemplate" />
   <include package=".generations" />
-  
+
   <!-- Views -->
   <include package="zope.app.http" />
 
@@ -45,18 +45,19 @@
 
 
   <!-- Local Component Registration -->
-  <include package="zope.app.registration" />  
+  <include package="zope.app.registration" />
 
   <!-- Services -->
-  <include package="zope.app.errorservice" />  
-  <include package="zope.app.pluggableauth" />  
-  <include package="zope.app.site" />  
+  <include package="zope.app.errorservice" />
+  <include package="zope.app.pluggableauth" />
+  <include package="zope.app.site" />
   <include package="zope.app.adapter" />
-  <include package="zope.app.utility" />  
-  <include package="zope.app.principalannotation" />  
+  <include package="zope.app.utility" />
+  <include package="zope.app.principalannotation" />
 
   <!-- Utilities -->
   <include package="zope.app.schema" />
+  <include package="zope.app.uniqueid" />
 
   <!-- Misc. Service Manager objects -->
   <include package="zope.app.module" />
@@ -67,8 +68,8 @@
 
   <!-- Database boostrapping -->
   <include package=".appsetup" />
-  
 
+
   <!-- Skins -->
 
   <include package="zope.app.basicskin" />

Modified: Zope3/trunk/src/zope/app/uniqueid/__init__.py
===================================================================
--- Zope3/trunk/src/zope/app/uniqueid/__init__.py	2004-06-10 18:07:11 UTC (rev 25338)
+++ Zope3/trunk/src/zope/app/uniqueid/__init__.py	2004-06-10 18:14:52 UTC (rev 25339)
@@ -27,20 +27,30 @@
 from ZODB.interfaces import IConnection
 from BTrees import OIBTree, IOBTree
 from zope.app import zapi
+from zope.app.location.interfaces import ILocation
+from zope.security.proxy import trustedRemoveSecurityProxy
 
-
 class UniqueIdUtility:
     """This utility provides a two way mapping between objects and
     integer ids.
 
     IReferences to objects are stored in the indexes.
     """
-    implements(IUniqueIdUtility)
+    implements(IUniqueIdUtility, ILocation)
 
+    __parent__ = None
+    __name__ = None
+
     def __init__(self):
         self.ids = OIBTree.OIBTree()
         self.refs = IOBTree.IOBTree()
 
+    def __len__(self):
+        return len(self.ids)
+
+    def items(self):
+        return list(self.refs.items())
+
     def getObject(self, id):
         return self.refs[id]()
 
@@ -51,10 +61,11 @@
     def _generateId(self):
         while True:
             uid = random.randint(0, 2**31)
-            if uid not in self.ids:
+            if uid not in self.refs:
                 return uid
 
     def register(self, ob):
+        ob = trustedRemoveSecurityProxy(ob)
         ref = zapi.getAdapter(ob, IReference)
         if ref in self.ids:
             return self.ids[ref]
@@ -104,5 +115,5 @@
     while not getattr(cur, '_p_jar', None):
         cur = getattr(cur, '__parent__', None)
         if cur is None:
-            raise ValueError('Can not get connection of %r', (ob,))
+            raise ValueError('Can not get connection of %r' % (ob,))
     return cur._p_jar

Added: Zope3/trunk/src/zope/app/uniqueid/browser/__init__.py
===================================================================
--- Zope3/trunk/src/zope/app/uniqueid/browser/__init__.py	2004-06-10 18:07:11 UTC (rev 25338)
+++ Zope3/trunk/src/zope/app/uniqueid/browser/__init__.py	2004-06-10 18:14:52 UTC (rev 25339)
@@ -0,0 +1,33 @@
+##############################################################################
+#
+# 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.
+#
+##############################################################################
+"""
+Unique id utility views.
+
+$Id$
+"""
+from zope.security.proxy import trustedRemoveSecurityProxy
+from zope.app import zapi
+
+class UniqueIdUtilityView:
+
+    def len(self):
+        return len(trustedRemoveSecurityProxy(self.context).refs)
+
+    def populate(self):
+        self.context.register(zapi.traverse(self.context, "/"))
+        self.context.register(zapi.traverse(self.context, "/++etc++site"))
+
+    def items(self):
+        return [(uid, zapi.getPath(ref())) for uid, ref in self.context.items()]
+


Property changes on: Zope3/trunk/src/zope/app/uniqueid/browser/__init__.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: Zope3/trunk/src/zope/app/uniqueid/browser/configure.zcml
===================================================================
--- Zope3/trunk/src/zope/app/uniqueid/browser/configure.zcml	2004-06-10 18:07:11 UTC (rev 25338)
+++ Zope3/trunk/src/zope/app/uniqueid/browser/configure.zcml	2004-06-10 18:14:52 UTC (rev 25339)
@@ -0,0 +1,29 @@
+<zope:configure
+  xmlns:zope="http://namespaces.zope.org/zope"
+  xmlns="http://namespaces.zope.org/browser">
+
+  <addMenuItem
+      title="Unique Id Utility"
+      description="A utility that provides unique ids to objects"
+      class="zope.app.uniqueid.UniqueIdUtility"
+      permission="zope.ManageServices"
+    />
+
+  <page
+      name="index.html"
+      menu="zmi_views" title="Registered Objects"
+      for="zope.app.uniqueid.interfaces.IUniqueIdUtility"
+      permission="zope.ManageServices"
+      class=".UniqueIdUtilityView"
+      template="registrations.pt"
+   />
+
+  <page
+      name="populate"
+      for="zope.app.uniqueid.interfaces.IUniqueIdUtility"
+      permission="zope.ManageServices"
+      class=".UniqueIdUtilityView"
+      attribute="populate"
+   />
+
+</zope:configure>


Property changes on: Zope3/trunk/src/zope/app/uniqueid/browser/configure.zcml
___________________________________________________________________
Name: svn:eol-style
   + native

Added: Zope3/trunk/src/zope/app/uniqueid/browser/registrations.pt
===================================================================
--- Zope3/trunk/src/zope/app/uniqueid/browser/registrations.pt	2004-06-10 18:07:11 UTC (rev 25338)
+++ Zope3/trunk/src/zope/app/uniqueid/browser/registrations.pt	2004-06-10 18:14:52 UTC (rev 25339)
@@ -0,0 +1,19 @@
+<html metal:use-macro="views/standard_macros/view">
+  <body>
+  <div metal:fill-slot="body">
+  <div metal:define-macro="body">
+      <p i18n:translate=""><span tal:replace="view/len" /> objects</p>
+
+      <table id="sortable" class="listing" summary="Content listing"
+             i18n:attributes="summary">
+	<tr><th i18n:translate="">ID</th><th i18n:translate="">Object</th></tr>
+	<tr tal:repeat="row view/items">
+	  <td tal:content="python:row[0]" />
+	  <td><a tal:content="python:row[1]" tal:attributes="href python:row[1]"/></td>
+	</tr>
+      </table>
+  </div>
+  </div>
+  </body>
+
+</html>


Property changes on: Zope3/trunk/src/zope/app/uniqueid/browser/registrations.pt
___________________________________________________________________
Name: svn:eol-style
   + native

Added: Zope3/trunk/src/zope/app/uniqueid/configure.zcml
===================================================================
--- Zope3/trunk/src/zope/app/uniqueid/configure.zcml	2004-06-10 18:07:11 UTC (rev 25338)
+++ Zope3/trunk/src/zope/app/uniqueid/configure.zcml	2004-06-10 18:14:52 UTC (rev 25339)
@@ -0,0 +1,54 @@
+<configure xmlns="http://namespaces.zope.org/zope">
+
+  <adapter
+      for="persistent.interfaces.IPersistent"
+      provides="zope.app.uniqueid.interfaces.IReference"
+      factory="zope.app.uniqueid.ReferenceToPersistent"
+      />
+
+  <adapter
+      for="persistent.interfaces.IPersistent"
+      provides="ZODB.interfaces.IConnection"
+      factory="zope.app.uniqueid.connectionOfPersistent"
+      />
+
+  <content class=".UniqueIdUtility">
+    <factory
+        id="zope.app.uniqueid.UniqueIdUtility"
+        />
+
+    <implements
+        interface="zope.app.annotation.interfaces.IAttributeAnnotatable"
+        />
+
+    <implements
+        interface="zope.app.utility.interfaces.ILocalUtility"
+        />
+
+    <require
+        permission="zope.Public"
+        interface=".interfaces.IUniqueIdUtilityQuery"
+        />
+
+    <require
+        permission="zope.ManageContent"
+        interface=".interfaces.IUniqueIdUtilitySet"
+        />
+    <require
+	permission="zope.Public"
+	interface=".interfaces.IUniqueIdUtilityManage"
+	/>
+
+  </content>
+
+  <content class=".ReferenceToPersistent">
+    <require
+	permission="zope.Public"
+        interface=".interfaces.IReference"
+	/>
+  </content>
+
+  <!-- Views -->
+  <include package=".browser" />
+
+</configure>


Property changes on: Zope3/trunk/src/zope/app/uniqueid/configure.zcml
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: Zope3/trunk/src/zope/app/uniqueid/interfaces.py
===================================================================
--- Zope3/trunk/src/zope/app/uniqueid/interfaces.py	2004-06-10 18:07:11 UTC (rev 25338)
+++ Zope3/trunk/src/zope/app/uniqueid/interfaces.py	2004-06-10 18:14:52 UTC (rev 25339)
@@ -42,7 +42,18 @@
         ValueError is raised if ob is not registered previously.
         """
 
-class IUniqueIdUtility(IUniqueIdUtilitySet, IUniqueIdUtilityQuery):
+class IUniqueIdUtilityManage(Interface):
+    """Some methods used by the view"""
+
+    def __len__():
+        """Returns the number of objects indexed"""
+
+    def items():
+        """Returns a list of (id, object) pairs"""
+
+
+class IUniqueIdUtility(IUniqueIdUtilitySet, IUniqueIdUtilityQuery,
+                       IUniqueIdUtilityManage):
     """A utility that assigns unique ids to the objects
 
     Allows to query object by id and id by object.

Modified: Zope3/trunk/src/zope/app/uniqueid/tests.py
===================================================================
--- Zope3/trunk/src/zope/app/uniqueid/tests.py	2004-06-10 18:07:11 UTC (rev 25338)
+++ Zope3/trunk/src/zope/app/uniqueid/tests.py	2004-06-10 18:14:52 UTC (rev 25339)
@@ -66,6 +66,7 @@
         u = UniqueIdUtility()
         obj = P()
         obj._p_jar = ConnectionStub()
+
         uid = u.register(obj)
         self.assert_(u.getObject(uid) is obj)
         self.assertEquals(u.getId(obj), uid)
@@ -77,7 +78,39 @@
         self.assertRaises(KeyError, u.getObject, uid)
         self.assertRaises(KeyError, u.getId, obj)
 
+    def test_len_items(self):
+        from zope.app.uniqueid import UniqueIdUtility
+        from zope.app.uniqueid import ReferenceToPersistent
+        u = UniqueIdUtility()
+        obj = P()
+        obj._p_jar = ConnectionStub()
 
+        self.assertEquals(len(u), 0)
+        self.assertEquals(u.items(), [])
+
+        uid = u.register(obj)
+        ref = ReferenceToPersistent(obj)
+        self.assertEquals(len(u), 1)
+        self.assertEquals(u.items(), [(uid, ref)])
+
+        obj2 = P()
+        obj2.__parent__ = obj
+
+        uid2 = u.register(obj2)
+        ref2 = ReferenceToPersistent(obj2)
+        self.assertEquals(len(u), 2)
+        result = u.items()
+        expected = [(uid, ref), (uid2, ref2)]
+        result.sort()
+        expected.sort()
+        self.assertEquals(result, expected)
+        
+        u.unregister(obj)
+        u.unregister(obj2)
+        self.assertEquals(len(u), 0)
+        self.assertEquals(u.items(), [])
+
+
 class TestReferenceToPersistent(ReferenceSetupMixin, unittest.TestCase):
 
     def test(self):




More information about the Zope3-Checkins mailing list