[Zope3-checkins] CVS: Zope3/src/zope/interface - adapter.py:1.13 adapter.txt:1.3 interfaces.py:1.24

Jim Fulton jim at zope.com
Tue Mar 30 16:40:01 EST 2004


Update of /cvs-repository/Zope3/src/zope/interface
In directory cvs.zope.org:/tmp/cvs-serv6685/src/zope/interface

Modified Files:
	adapter.py adapter.txt interfaces.py 
Log Message:
Added an api to the adapter registry to get all of the names and
values registered for given required and provided interfaces.  This is
used to get all of the names for a given type of adapter.  This will
be handy for creating menus and for documentation purposes.


=== Zope3/src/zope/interface/adapter.py 1.12 => 1.13 ===
--- Zope3/src/zope/interface/adapter.py:1.12	Sun Mar 28 18:42:28 2004
+++ Zope3/src/zope/interface/adapter.py	Tue Mar 30 16:40:00 2004
@@ -397,6 +397,61 @@
 
         return value
 
+    def lookupAll(self, required, provided):
+        order = len(required)
+        if order == 1:
+            # Simple adapter:
+            s = self.get(required[0])
+            byname = s.get(provided)
+            if byname:
+                for item in byname.iteritems():
+                    yield item
+
+            defbyname = self._default.get(provided)
+            if defbyname:
+                for name, value in defbyname.iteritems():
+                    if name in byname:
+                        continue
+                    yield name, value
+
+            return
+
+        elif order == 0:
+            # null adapter
+            byname = self._null.get(provided)
+            if byname:
+                for item in byname.iteritems():
+                    yield item
+
+            return
+
+
+        # Multi adapter
+
+        with = required[1:]
+        key = provided, order
+        first = ()
+
+        for surrogate in self.get(required[0]), self._default:
+            byname = surrogate.get(key)
+            if not byname:
+                continue
+
+            for name, bywith in byname.iteritems():
+                if not bywith or name in first:
+                    continue
+
+                for rwith, value in bywith:
+                    for rspec, spec in zip(rwith, with):
+                        if not spec.isOrExtends(rspec):
+                            break # This one is no good
+                    else:
+                        # Got this far, we have a match
+                        yield name, value
+                        break
+
+            first = byname
+
 
     def subscribe(self, required, provided, value):
         if required:


=== Zope3/src/zope/interface/adapter.txt 1.2 => 1.3 ===
--- Zope3/src/zope/interface/adapter.txt:1.2	Sat Mar 27 09:17:17 2004
+++ Zope3/src/zope/interface/adapter.txt	Tue Mar 30 16:40:00 2004
@@ -191,6 +191,11 @@
 
   >>> registry.lookup([IR2, IQ2], IP1, '')
   '1q2'
+
+  >>> registry.register([IR1, IQ2], IP2, '', '1q22')
+  >>> registry.lookup([IR2, IQ2], IP1, '')
+  '1q22'
+
   
 Default Adapters
 ----------------
@@ -213,6 +218,32 @@
   >>> registry.lookup([], IP1, '')
   2
 
+Listing named adapters
+----------------------
+
+Adapters are named. Sometimes, it's useful to get all of the named
+adapters for given interfaces.
+
+  >>> adapters = list(registry.lookupAll([IR1], IP1))
+  >>> adapters.sort()
+  >>> adapters
+  [(u'', 11), (u'bob', "Bob's 12")]
+
+This works for multi-adapters too:
+
+  >>> registry.register([IR1, IQ2], IP2, 'bob', '1q2 for bob')
+  >>> adapters = list(registry.lookupAll([IR2, IQ2], IP1))
+  >>> adapters.sort()
+  >>> adapters
+  [(u'', '1q22'), (u'bob', '1q2 for bob')]
+
+And even null adapters:
+
+  >>> registry.register([], IP2, 'bob', 3)
+  >>> adapters = list(registry.lookupAll([], IP1))
+  >>> adapters.sort()
+  >>> adapters
+  [(u'', 2), (u'bob', 3)]
 
 Subscriptions
 =============


=== Zope3/src/zope/interface/interfaces.py 1.23 => 1.24 ===
--- Zope3/src/zope/interface/interfaces.py:1.23	Mon Mar 15 15:41:55 2004
+++ Zope3/src/zope/interface/interfaces.py	Tue Mar 30 16:40:00 2004
@@ -714,6 +714,12 @@
         specifications, a provided interface, and a name.
         """
 
+    def lookupAll(required, provided):
+        """Find all adapters from the required to the provided interfaces
+
+        An iterable object is returned that provides name-value two-tuples.
+        """
+
     def names(required, provided):
         """Return the names for which there are registered objects
         """




More information about the Zope3-Checkins mailing list