[Zope3-checkins] SVN: Zope3/trunk/ - Deprecated ``zope.app.component.get/queryNextSiteManager``

Stephan Richter srichter at cosmos.phy.tufts.edu
Tue Sep 26 04:56:49 EDT 2006


Log message for revision 70382:
  - Deprecated ``zope.app.component.get/queryNextSiteManager``
    function. Fixed ``zope.app.component.get/queryNextUtility`` to work
    correctly with multiple bases.
  
  

Changed:
  U   Zope3/trunk/doc/CHANGES.txt
  U   Zope3/trunk/src/zope/app/apidoc/utilitymodule/utilitymodule.py
  U   Zope3/trunk/src/zope/app/component/README.txt
  U   Zope3/trunk/src/zope/app/component/__init__.py

-=-
Modified: Zope3/trunk/doc/CHANGES.txt
===================================================================
--- Zope3/trunk/doc/CHANGES.txt	2006-09-26 08:44:45 UTC (rev 70381)
+++ Zope3/trunk/doc/CHANGES.txt	2006-09-26 08:56:49 UTC (rev 70382)
@@ -125,6 +125,10 @@
 
     Bug fixes
 
+      - Deprecated ``zope.app.component.get/queryNextSiteManager``
+        function. Fixed ``zope.app.component.get/queryNextUtility`` to work
+        correctly with multiple bases.
+
       - Generate an ObjectCreatedEvent when the root object is created.
 
       - More or less fixed the static apidoc generator. (fix + workaround).

Modified: Zope3/trunk/src/zope/app/apidoc/utilitymodule/utilitymodule.py
===================================================================
--- Zope3/trunk/src/zope/app/apidoc/utilitymodule/utilitymodule.py	2006-09-26 08:44:45 UTC (rev 70381)
+++ Zope3/trunk/src/zope/app/apidoc/utilitymodule/utilitymodule.py	2006-09-26 08:56:49 UTC (rev 70382)
@@ -120,13 +120,24 @@
             return UtilityInterface(self, key, getattr(mod, parts[-1], default))
 
     def items(self):
-        sm = zope.component.getSiteManager()
+        # Use a list of site managers, since we want to support multiple bases
+        smlist = [zope.component.getSiteManager()]
+        seen = []
         ifaces = {}
-        while sm is not None:
+        while smlist:
+            # Get the next site manager
+            sm = smlist.pop()
+            # If we have already looked at this site manager, then skip it
+            if sm in seen:
+                continue
+            # Add the current site manager to the list of seen ones
+            seen.append(sm)
+            # Add the bases of the current site manager to the list of site
+            # managers to be processed
+            smlist += list(sm.__bases__)
             for reg in sm.registeredUtilities():
                 path = getPythonPath(reg.provided)
                 ifaces[path] = UtilityInterface(self, path, reg.provided)
-            sm = queryNextSiteManager(sm)
 
         items = ifaces.items()
         items.sort(lambda x, y: cmp(x[0].split('.')[-1], y[0].split('.')[-1]))

Modified: Zope3/trunk/src/zope/app/component/README.txt
===================================================================
--- Zope3/trunk/src/zope/app/component/README.txt	2006-09-26 08:44:45 UTC (rev 70381)
+++ Zope3/trunk/src/zope/app/component/README.txt	2006-09-26 08:56:49 UTC (rev 70382)
@@ -32,10 +32,15 @@
 Local Component Architecture API
 --------------------------------
 
-While the component architecture API provided by `zope.component` is
+While the component architecture API provided by ``zope.component`` is
 sufficient most of the time, there are a couple of functions that are useful
-in the context of multiple sites. 
+in the context of multiple sites and base component registries.
 
+--- BBB: Deprecated on 9/26/2006 --
+
+  >>> import zope.deprecation
+  >>> zope.deprecation.__show__.off()
+
 A very common use case is to get the nearest site manager in a given
 context. Sometimes, however, one wants the next higher-up site manager. First,
 let's create a folder tree and create some sites from it:
@@ -44,7 +49,7 @@
   >>> root = setup.buildSampleFolderTree()
   >>> root_sm = setup.createSiteManager(root)
   >>> folder1_sm = setup.createSiteManager(root['folder1'])
- 
+
 If we ask `folder1` for its nearest site manager, we get
 
   >>> from zope.app import zapi
@@ -77,30 +82,33 @@
   >>> component.queryNextSiteManager(object(), 'default')
   'default'
 
-A related use case to the above is to find the next available utility
-providing a certain interface and name. This is often used when a utility
-delegates a query to the next one. Let's start by creating a utility and
-inserting it in our folder hiearchy:
+  >>> zope.deprecation.__show__.on()
 
+--- BBB: End of block --
+
+It is common for a utility to delegate its answer to a utility providing the
+same interface in one of the component registry's bases. Let's start by
+creating a utility and inserting it in our folder hiearchy:
+
   >>> import zope.interface
   >>> class IMyUtility(zope.interface.Interface):
   ...     pass
-  
+
   >>> class MyUtility(object):
   ...     zope.interface.implements(IMyUtility)
   ...     def __init__(self, id):
   ...         self.id = id
   ...     def __repr__(self):
-  ...         return "%s('%s')" %(self.__class__.__name__, self.id)  
+  ...         return "%s('%s')" %(self.__class__.__name__, self.id)
 
-  >>> gutil = MyUtility('global') 
+  >>> gutil = MyUtility('global')
   >>> gsm.registerUtility(gutil, IMyUtility, 'myutil')
 
-  >>> util1 = setup.addUtility(folder1_sm, 'myutil', IMyUtility, 
+  >>> util1 = setup.addUtility(folder1_sm, 'myutil', IMyUtility,
   ...                          MyUtility('one'))
 
   >>> folder1_1_sm = setup.createSiteManager(root['folder1']['folder1_1'])
-  >>> util1_1 = setup.addUtility(folder1_1_sm, 'myutil', IMyUtility, 
+  >>> util1_1 = setup.addUtility(folder1_1_sm, 'myutil', IMyUtility,
   ...                            MyUtility('one-one'))
 
 Now, if we ask `util1_1` for its next available utility and we get
@@ -115,15 +123,35 @@
 
 However, if we ask the global utility for the next one, an error is raised
 
-  >>> component.getNextUtility(gutil, IMyUtility, 
+  >>> component.getNextUtility(gutil, IMyUtility,
   ...                          'myutil') #doctest: +NORMALIZE_WHITESPACE
   Traceback (most recent call last):
-  ...  
-  ComponentLookupError: 
-  No more utilities for <InterfaceClass __builtin__.IMyUtility>, 
+  ...
+  ComponentLookupError:
+  No more utilities for <InterfaceClass __builtin__.IMyUtility>,
   'myutil' have been found.
 
 or you can simply use the `queryNextUtility` and specify a default:
 
   >>> component.queryNextUtility(gutil, IMyUtility, 'myutil', 'default')
   'default'
+
+Let's now ensure that the function also works with multiple registries. First
+we create another base registry:
+
+  >>> from zope.component import registry
+  >>> myregistry = registry.Components()
+
+  >>> custom_util = MyUtility('my_custom_util')
+  >>> myregistry.registerUtility(custom_util, IMyUtility, 'my_custom_util')
+
+Now we add it as a base to the local site manager:
+
+  >>> folder1_sm.__bases__ = (myregistry,) + folder1_sm.__bases__
+
+Both, the ``myregistry`` and global utilities should be available:
+
+  >>> component.queryNextUtility(folder1_sm, IMyUtility, 'my_custom_util')
+  MyUtility('my_custom_util')
+  >>> component.queryNextUtility(folder1_sm, IMyUtility, 'myutil')
+  MyUtility('global')

Modified: Zope3/trunk/src/zope/app/component/__init__.py
===================================================================
--- Zope3/trunk/src/zope/app/component/__init__.py	2006-09-26 08:44:45 UTC (rev 70381)
+++ Zope3/trunk/src/zope/app/component/__init__.py	2006-09-26 08:56:49 UTC (rev 70382)
@@ -17,9 +17,16 @@
 """
 __docformat__ = "reStructuredText"
 import zope.component
+import zope.deprecation
 
 _marker = object()
 
+# BBB: Deprecated on 9/26/2006
+ at zope.deprecation.deprecate('''This function has been deprecated and will go
+away in Zope 3.6. There is no replacement for this function, since it odes not
+make sense in light of registry bases anymore. If you are using this function
+to lookup the next utility, consider using get/queryNextUtility. Otherwise, it
+is suggested to iterate through the list of bases of a registry manually.''')
 def getNextSiteManager(context):
     """Get the next site manager."""
     sm = queryNextSiteManager(context, _marker)
@@ -29,6 +36,12 @@
     return sm
 
 
+# BBB: Deprecated on 9/26/2006
+ at zope.deprecation.deprecate('''This function has been deprecated and will go
+away in Zope 3.6. There is no replacement for this function, since it odes not
+make sense in light of registry bases anymore. If you are using this function
+to lookup the next utility, consider using get/queryNextUtility. Otherwise, it
+is suggested to iterate through the list of bases of a registry manually.''')
 def queryNextSiteManager(context, default=None):
     """Get the next site manager.
 
@@ -63,8 +76,11 @@
 
     Find the next available utility providing `interface` and having the
     specified name. If no utility was found, return the specified `default`
-    value."""    
-    sm = queryNextSiteManager(context)
-    if sm is None:
-        return default
-    return sm.queryUtility(interface, name, default)
+    value."""
+    sm = zope.component.getSiteManager(context)
+    bases = sm.__bases__
+    for base in bases:
+        util = base.queryUtility(interface, name, _marker)
+        if util is not _marker:
+            return util
+    return default



More information about the Zope3-Checkins mailing list