[Checkins] SVN: five.localsitemanager/branches/icemac-absolute-path-components/src/five/localsitemanager/ - better error message when trying to register acquisition wrapped utility which does not provide getPhysicalPath

Michael Howitz mh at gocept.com
Tue Aug 26 09:40:53 EDT 2008


Log message for revision 90331:
  - better error message when trying to register acquisition wrapped utility which does not provide getPhysicalPath
  - utilities which do not have an absolute path (but only a relative one) are no longer stored with path as there is nearly no chance to use this path for traversal in getUtility
  

Changed:
  U   five.localsitemanager/branches/icemac-absolute-path-components/src/five/localsitemanager/localsitemanager.txt
  U   five.localsitemanager/branches/icemac-absolute-path-components/src/five/localsitemanager/registry.py

-=-
Modified: five.localsitemanager/branches/icemac-absolute-path-components/src/five/localsitemanager/localsitemanager.txt
===================================================================
--- five.localsitemanager/branches/icemac-absolute-path-components/src/five/localsitemanager/localsitemanager.txt	2008-08-26 13:33:11 UTC (rev 90330)
+++ five.localsitemanager/branches/icemac-absolute-path-components/src/five/localsitemanager/localsitemanager.txt	2008-08-26 13:40:52 UTC (rev 90331)
@@ -242,8 +242,7 @@
     ...                               name=u'with_aq_chain',
     ...                               provided=ITestUtility)
     Traceback (most recent call last):
-    AttributeError: getPhysicalPath
-
+    AttributeError: Component <Utility AQTestUtility "util"> does not implement getPhysicalPath, so register it unwrapped or implement this method.
     >>> import OFS.SimpleItem
     >>> class SITestUtility(OFS.SimpleItem.SimpleItem, TestUtility): pass
     >>> si_util = self.app.a.b._setObject('si_util', SITestUtility('si_util'))
@@ -352,7 +351,6 @@
     >>> aqutils
     [<SITestUtility at /a/b/si_util>, <Utility AQTestUtility "test">]
 
-
 Nested Sites
 ------------
 
@@ -467,7 +465,23 @@
     >>> util1_1.aq_chain
     [<Utility AQTestUtility "util1_1">, <Folder at folder1/folder1_1>, <Folder at folder1>]
 
+Utilities stored with relative path
+-----------------------------------
 
+If we register a utility which has only a relative path, the path is
+_not_ stored and the utility is returned relative to the registry. (In
+the example we register folder_1/folder1_1/util in the registry of
+folder_1.):
+
+    >>> folder1_1._setObject('util', SITestUtility('util'), set_owner=False)
+    'util'
+    >>> sm1.registerUtility(folder1_1.util,
+    ...                     name=u'util2',
+    ...                     provided=ITestUtility)
+    >>> sm1.getUtility(ITestUtility, name='util2')
+    <SITestUtility at folder1/util>
+
+
 Acquisition Context of Global Utilities
 ---------------------------------------
 

Modified: five.localsitemanager/branches/icemac-absolute-path-components/src/five/localsitemanager/registry.py
===================================================================
--- five.localsitemanager/branches/icemac-absolute-path-components/src/five/localsitemanager/registry.py	2008-08-26 13:33:11 UTC (rev 90330)
+++ five.localsitemanager/branches/icemac-absolute-path-components/src/five/localsitemanager/registry.py	2008-08-26 13:40:52 UTC (rev 90331)
@@ -213,14 +213,22 @@
                 subscribed = True
                 break
 
-        # store path information if it exists
+        wrapped_component = component
         if hasattr(component, 'aq_parent'):
-            if Acquisition.aq_parent(component) != None:
-                path = component.getPhysicalPath()
+            # component is acquisition wrapped, so try to store path
+            if not hasattr(component, 'getPhysicalPath'):
+                raise AttributeError(
+                    'Component %r does not implement getPhysicalPath, '
+                    'so register it unwrapped or implement this method.' % 
+                    component)
+            path = component.getPhysicalPath()
+            # If the path is relative we can't store it because we
+            # have nearly no chance to use the path for traversal in
+            # getUtility.
+            if path[0] == '':
+                # We have an absolute path, so we can store it.
                 wrapped_component = ComponentPathWrapper(
                     Acquisition.aq_base(component), path)
-        else:
-            wrapped_component = component
         self._utility_registrations[(provided, name)] = wrapped_component, info
         self.utilities.register((), provided, name, wrapped_component)
 



More information about the Checkins mailing list