[Checkins] SVN: five.localsitemanager/trunk/src/five/localsitemanager/ Utilities returned by the site manager now get their aq chain updated so

Rocky Burt rocky at serverzen.com
Mon Feb 26 08:18:32 EST 2007


Log message for revision 72810:
  Utilities returned by the site manager now get their aq chain updated so
  that their acquired parents are now the ISite that owns the site manager.
  

Changed:
  U   five.localsitemanager/trunk/src/five/localsitemanager/localsitemanager.txt
  U   five.localsitemanager/trunk/src/five/localsitemanager/registry.py

-=-
Modified: five.localsitemanager/trunk/src/five/localsitemanager/localsitemanager.txt
===================================================================
--- five.localsitemanager/trunk/src/five/localsitemanager/localsitemanager.txt	2007-02-26 06:44:18 UTC (rev 72809)
+++ five.localsitemanager/trunk/src/five/localsitemanager/localsitemanager.txt	2007-02-26 13:18:30 UTC (rev 72810)
@@ -100,6 +100,38 @@
     >>> ITestAdapter(Foo('foo'))
     <Adapter TestAdapter adapting "foo">
 
+Acquisition
+-----------
+
+Now to mix a little required Zope 2 confusion into everything, we must ensure
+that the aq chain is predictable.  And based on consensus we decided that
+the acquired parent of a returned utility should be the ``ISite`` that
+owns the ``ISiteManager`` that returned the utility.  We need to ensure
+all the ways of getting a utility have been covered.
+
+    >>> import Acquisition
+
+    >>> comp = sitemanager.queryUtility(ITestUtility, name=u'hello_world')
+    >>> Acquisition.aq_parent(comp) is site
+    True
+
+    >>> comp = sitemanager.getUtility(ITestUtility, name=u'hello_world')
+    >>> Acquisition.aq_parent(comp) is site
+    True
+
+    >>> utils = [x for x in sitemanager.getUtilitiesFor(ITestUtility)]
+    >>> len(utils)
+    1
+    >>> name, comp = utils[0]
+    >>> Acquisition.aq_parent(comp) is site
+    True
+
+    >>> utils = [x for x in sitemanager.getAllUtilitiesRegisteredFor(ITestUtility)]
+    >>> len(utils)
+    1
+    >>> Acquisition.aq_parent(comp) is site
+    True
+
 Nested Sites
 ------------
 

Modified: five.localsitemanager/trunk/src/five/localsitemanager/registry.py
===================================================================
--- five.localsitemanager/trunk/src/five/localsitemanager/registry.py	2007-02-26 06:44:18 UTC (rev 72809)
+++ five.localsitemanager/trunk/src/five/localsitemanager/registry.py	2007-02-26 13:18:30 UTC (rev 72810)
@@ -1,9 +1,45 @@
+import Acquisition
 import zope.component.persistentregistry
 import OFS.ObjectManager
 
+_marker = object()
+
 class PersistentComponents \
           (zope.component.persistentregistry.PersistentComponents,
            OFS.ObjectManager.ObjectManager):
     """An implementation of a component registry that can be persisted
-    and looks like a standard ObjectManager.
+    and looks like a standard ObjectManager.  It also ensures that all
+    utilities have the the parent of this site manager (which should be
+    the ISite) as their acquired parent.
     """
+
+    def _wrap(self, comp):
+        """Return an aq wrapped component with the site as the parent.
+        """
+        parent = Acquisition.aq_parent(self)
+        if parent is None:
+            raise ValueError('Not enough context to acquire parent')
+
+        base = Acquisition.aq_base(comp)
+
+        return Acquisition.ImplicitAcquisitionWrapper(base, parent)
+
+    def queryUtility(self, provided, name=u'', default=None):
+        comp = self.utilities.lookup((), provided, name, default)
+        if comp is not default:
+            comp = self._wrap(comp)
+        return comp
+
+    def getUtility(self, provided, name=u''):
+        utility = self.queryUtility(provided, name, _marker)
+        if utility is _marker:
+            raise interfaces.ComponentLookupError(provided, name)
+        return utility
+
+    def getUtilitiesFor(self, interface):
+        return ((name, self._wrap(utility))
+                for name, utility in self.utilities.lookupAll((), interface))
+
+    def getAllUtilitiesRegisteredFor(self, interface):
+        return (self._wrap(x)
+                for x in self.utilities.subscriptions((), interface))



More information about the Checkins mailing list