[Zope3-checkins] SVN: Zope3/trunk/ Added a `getAdapters()` method to `zapi` that exposes `lookupAll()` to the

Stephan Richter srichter at cosmos.phy.tufts.edu
Fri Sep 24 14:04:51 EDT 2004


Log message for revision 27675:
  Added a `getAdapters()` method to `zapi` that exposes `lookupAll()` to the 
  public API better. I need this to change the menu code to named adapters.
  


Changed:
  U   Zope3/trunk/doc/CHANGES.txt
  U   Zope3/trunk/src/zope/component/__init__.py
  U   Zope3/trunk/src/zope/component/interfaces.py
  U   Zope3/trunk/src/zope/component/tests/test_api.py


-=-
Modified: Zope3/trunk/doc/CHANGES.txt
===================================================================
--- Zope3/trunk/doc/CHANGES.txt	2004-09-24 18:02:23 UTC (rev 27674)
+++ Zope3/trunk/doc/CHANGES.txt	2004-09-24 18:04:51 UTC (rev 27675)
@@ -10,6 +10,10 @@
 
     New features
 
+      - Added new API method `getAdapters(objects, provided)` to `zapi`. It
+        will return all available adapters for this configuration. Note that
+        every name will be only returned once.
+
       - When parsing a DSN, allow the host to be specified without a port.
 
       - Removed Browser Menu Service and implemented menus as subscriber

Modified: Zope3/trunk/src/zope/component/__init__.py
===================================================================
--- Zope3/trunk/src/zope/component/__init__.py	2004-09-24 18:02:23 UTC (rev 27674)
+++ Zope3/trunk/src/zope/component/__init__.py	2004-09-24 18:04:51 UTC (rev 27675)
@@ -170,6 +170,17 @@
 
     return adapters.queryMultiAdapter(objects, interface, name, default)
 
+def getAdapters(objects, provided, context=None):
+    try:
+        adapters = getService(Adapters, context)
+    except ComponentLookupError:
+        # Oh blast, no adapter service. We're probably just running from a test
+        return []
+    return [(name, adapter(*objects))
+            for name, adapter in adapters.lookupAll(map(providedBy, objects),
+                                                    provided)
+            ]
+
 def subscribers(objects, interface, context=None):
     try:
         adapters = getService(Adapters, context=context)

Modified: Zope3/trunk/src/zope/component/interfaces.py
===================================================================
--- Zope3/trunk/src/zope/component/interfaces.py	2004-09-24 18:02:23 UTC (rev 27674)
+++ Zope3/trunk/src/zope/component/interfaces.py	2004-09-24 18:04:51 UTC (rev 27675)
@@ -208,6 +208,20 @@
         named adapter methods with an empty string for a name.
         """
 
+    def getAdapters(objects, provided, context=None):
+        """Look for all matching adapters to a provided interface for objects
+
+        Return a list of adapters that match. If an adapter is named, only the
+        most specific adapter of a given name is returned.
+
+        If context is None, an application-defined policy is used to choose
+        an appropriate service manager from which to get an 'Adapters'
+        service.
+
+        If 'context' is not None, context is adapted to IServiceService,
+        and this adapter's 'Adapters' service is used.
+        """
+
     def subscribers(required, provided, context=None):
         """Get subscribers
 

Modified: Zope3/trunk/src/zope/component/tests/test_api.py
===================================================================
--- Zope3/trunk/src/zope/component/tests/test_api.py	2004-09-24 18:02:23 UTC (rev 27674)
+++ Zope3/trunk/src/zope/component/tests/test_api.py	2004-09-24 18:04:51 UTC (rev 27675)
@@ -19,7 +19,7 @@
 
 from zope import component
 from zope.component import servicenames
-from zope.component import getAdapter, queryAdapter
+from zope.component import getAdapter, queryAdapter, getAdapters
 from zope.component import getAdapterInContext, queryAdapterInContext
 from zope.component import getService
 from zope.component import getUtility, queryUtility
@@ -266,6 +266,15 @@
         self.assertEquals(c.__class__, Comp)
         self.assertEquals(c.context, ob)
 
+    def testgetAdapters(self):
+        getService(Adapters).register([I1], I2, '', Comp)
+        getService(Adapters).register([None], I2, 'foo', Comp)
+        c = getAdapters((ob,), I2)
+        c.sort()
+        self.assertEquals([(name, adapter.__class__, adapter.context)
+                           for name, adapter in c],
+                          [('', Comp, ob), ('foo', Comp, ob)])
+
     def testUtility(self):
         self.assertRaises(ComponentLookupError, getUtility, I1, context=ob)
         self.assertRaises(ComponentLookupError, getUtility, I2, context=ob)



More information about the Zope3-Checkins mailing list