[Checkins]
SVN: five.localsitemanager/trunk/src/five/localsitemanager/
Updated aq wrapping to only take place when the given
component is actually
Rocky Burt
rocky at serverzen.com
Tue Feb 27 08:31:53 EST 2007
Log message for revision 72874:
Updated aq wrapping to only take place when the given component is actually
aq aware.
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-27 13:13:44 UTC (rev 72873)
+++ five.localsitemanager/trunk/src/five/localsitemanager/localsitemanager.txt 2007-02-27 13:31:52 UTC (rev 72874)
@@ -107,28 +107,74 @@
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.
+all the ways of getting a utility have been covered. Of course this should
+only happen if the utility is acquisition aware to beging with.
>>> import Acquisition
+ >>> from Acquisition.interfaces import IAcquirer
+First off, our utility isn't aq-wrapped so asking it what is aq_parent is
+should return None.
+
>>> comp = sitemanager.queryUtility(ITestUtility, name=u'hello_world')
- >>> Acquisition.aq_parent(comp) is site
+ >>> Acquisition.aq_parent(comp) is None
True
- >>> comp = sitemanager.getUtility(ITestUtility, name=u'hello_world')
+So now we setup a utility that is aq aware.
+
+ >>> class AQTestUtility(Acquisition.Explicit, TestUtility): pass
+ >>> sitemanager.registerUtility(AQTestUtility('test'),
+ ... name=u'aq_wrapped',
+ ... provided=ITestUtility)
+
+And of course the aq parents should be the site now.
+
+ >>> comp = sitemanager.getUtility(ITestUtility, name=u'aq_wrapped')
>>> Acquisition.aq_parent(comp) is site
True
+And just to mix things up a bit. Getting back multiple utilities should
+allow us to test both aq and non-aq based components.
+
+We start with getUtilitiesFor():
+
>>> utils = [x for x in sitemanager.getUtilitiesFor(ITestUtility)]
>>> len(utils)
+ 2
+
+ >>> nonaqutils = [(name, comp)
+ ... for name, comp in utils if not IAcquirer.providedBy(comp)]
+ >>> len(nonaqutils)
1
- >>> name, comp = utils[0]
+ >>> name, comp = nonaqutils[0]
+ >>> Acquisition.aq_parent(comp) is None
+ True
+
+ >>> aqutils = [(name, comp)
+ ... for name, comp in utils if IAcquirer.providedBy(comp)]
+ >>> len(aqutils)
+ 1
+ >>> name, comp = aqutils[0]
>>> Acquisition.aq_parent(comp) is site
True
+And then getAllUtilitiesRegisteredFor():
+
>>> utils = [x for x in sitemanager.getAllUtilitiesRegisteredFor(ITestUtility)]
>>> len(utils)
+ 2
+
+ >>> nonaqutils = [comp for comp in utils if not IAcquirer.providedBy(comp)]
+ >>> len(nonaqutils)
1
+ >>> comp = nonaqutils[0]
+ >>> Acquisition.aq_parent(comp) is None
+ True
+
+ >>> aqutils = [comp for comp in utils if IAcquirer.providedBy(comp)]
+ >>> len(aqutils)
+ 1
+ >>> comp = aqutils[0]
>>> Acquisition.aq_parent(comp) is site
True
Modified: five.localsitemanager/trunk/src/five/localsitemanager/registry.py
===================================================================
--- five.localsitemanager/trunk/src/five/localsitemanager/registry.py 2007-02-27 13:13:44 UTC (rev 72873)
+++ five.localsitemanager/trunk/src/five/localsitemanager/registry.py 2007-02-27 13:31:52 UTC (rev 72874)
@@ -14,16 +14,30 @@
"""
def _wrap(self, comp):
- """Return an aq wrapped component with the site as the parent.
+ """Return an aq wrapped component with the site as the parent but
+ only if the comp has an aq wrapper to begin with.
"""
- parent = Acquisition.aq_parent(self)
- if parent is None:
- raise ValueError('Not enough context to acquire parent')
- base = Acquisition.aq_base(comp)
+ # BBB: The primary reason for doing this sort of wrapping of
+ # returned utilities is to support CMF tool-like functionality where
+ # a tool expects it's aq_parent to be the portal object. New code
+ # (ie new utilities) should not rely on this predictability to
+ # get the portal object and should search out an alternate means
+ # (possibly retrieve the ISiteRoot utility). Although in most
+ # cases getting at the portal object shouldn't be the required pattern
+ # but instead looking up required functionality via other (possibly
+ # local) components.
- return Acquisition.ImplicitAcquisitionWrapper(base, parent)
+ if Acquisition.interfaces.IAcquirer.providedBy(comp):
+ parent = Acquisition.aq_parent(self)
+ if parent is None:
+ raise ValueError('Not enough context to acquire parent')
+ base = Acquisition.aq_base(comp)
+ comp = base.__of__(parent)
+
+ return comp
+
def queryUtility(self, provided, name=u'', default=None):
comp = self.utilities.lookup((), provided, name, default)
if comp is not default:
More information about the Checkins
mailing list