[Zope3-checkins] CVS: Zope3/lib/python/Zope/ComponentArchitecture - GlobalAdapterService.py:1.6 GlobalUtilityService.py:1.5 IAdapterService.py:1.3 IComponentArchitecture.py:1.3 IUtilityService.py:1.4 __init__.py:1.10

Jim Fulton jim@zope.com
Sun, 15 Dec 2002 15:16:39 -0500


Update of /cvs-repository/Zope3/lib/python/Zope/ComponentArchitecture
In directory cvs.zope.org:/tmp/cvs-serv15449/Zope/ComponentArchitecture

Modified Files:
	GlobalAdapterService.py GlobalUtilityService.py 
	IAdapterService.py IComponentArchitecture.py 
	IUtilityService.py __init__.py 
Log Message:
Utilities and adapters can now be named.

For example:

  x = getAdapter(self, Ifoo, name="spam")

but unnamed adapters and utilities are still the norm and default.


=== Zope3/lib/python/Zope/ComponentArchitecture/GlobalAdapterService.py 1.5 => 1.6 ===
--- Zope3/lib/python/Zope/ComponentArchitecture/GlobalAdapterService.py:1.5	Wed Dec  4 03:39:26 2002
+++ Zope3/lib/python/Zope/ComponentArchitecture/GlobalAdapterService.py	Sun Dec 15 15:16:38 2002
@@ -20,7 +20,7 @@
 
 class IGlobalAdapterService(IAdapterService):
 
-    def provideAdapter(forInterface, providedInterface, maker):
+    def provideAdapter(forInterface, providedInterface, maker, name=''):
         """Provide an adapter
 
         An adapter provides an interface for objects that have another
@@ -35,8 +35,19 @@
         maker -- a callable object that gets an adapter component for
         a context component.
         """
-    def getRegisteredMatching(for_interface=None, provide_interface=None):
-        """To get all the globally registered adapters.
+    def getRegisteredMatching(for_interface=None, provide_interface=None,
+                              name=None):
+        """Return information about registered data
+
+        A four-tuple is returned containing:
+
+          - registered name,
+
+          - registered for interface
+
+          - registered provided interface, and
+
+          - registered data
         """
 
 class GlobalAdapterService:
@@ -44,9 +55,9 @@
     __implements__ = IGlobalAdapterService
     
     def __init__(self):
-        self.__adapters = AdapterRegistry()
+        self.__adapters = {}
     
-    def provideAdapter(self, forInterface, providedInterface, maker):
+    def provideAdapter(self, forInterface, providedInterface, maker, name=''):
         """see IGlobalAdapterService interface"""
 
         if not isinstance(maker, (list, tuple)):
@@ -57,24 +68,33 @@
         if not maker == filter(callable, maker):
             raise TypeError("The registered component callable is not "
                             "callable")
+
+        registry = self.__adapters.get(name)
+        if registry is None:
+            registry = AdapterRegistry()
+            self.__adapters[name] = registry
         
-        self.__adapters.register(forInterface, providedInterface, maker)
+        registry.register(forInterface, providedInterface, maker)
 
-    def getAdapter(self, object, interface):
+    def getAdapter(self, object, interface, name=''):
         """see IAdapterService interface"""
-        result = self.queryAdapter(object, interface)
+        result = self.queryAdapter(object, interface, None, name)
         if result is None:
             raise ComponentLookupError(object, interface)
 
         return result
     
     
-    def queryAdapter(self, object, interface, default=None):
+    def queryAdapter(self, object, interface, default=None, name=''):
         """see IAdapterService interface"""
-        if interface.isImplementedBy(object):
+        if (not name) and interface.isImplementedBy(object):
             return object
+
+        registry = self.__adapters.get(name)
+        if registry is None:
+            return default
         
-        makers = self.__adapters.getForObject(object, interface)
+        makers = registry.getForObject(object, interface)
 
         if makers is None:
             return default
@@ -86,16 +106,28 @@
         return result
 
     def getRegisteredMatching(self,
-                              required_interfaces=None,
+                              for_interfaces=None,
                               provided_interfaces=None,
+                              name = None,
                               ):
-        
-        return self.__adapters.getRegisteredMatching(required_interfaces,
-                                                     provided_interfaces)
-        
-  
 
-    
+        if name is not None:
+            registry = self.__adapters.get(name)
+            if registry is None:
+                return ()
+            return [(name, for_, provided, data)
+                    for (for_, provided, data)
+                    in registry.getRegisteredMatching(for_interfaces,
+                                                      provided_interfaces)
+                    ]
+
+        result = []
+        for name in self.__adapters:
+            r = self.getRegisteredMatching(
+                for_interfaces, provided_interfaces, name)
+            result.extend(r)
+
+        return result
 
     _clear = __init__
 


=== Zope3/lib/python/Zope/ComponentArchitecture/GlobalUtilityService.py 1.4 => 1.5 ===
--- Zope3/lib/python/Zope/ComponentArchitecture/GlobalUtilityService.py:1.4	Thu Aug  1 11:33:44 2002
+++ Zope3/lib/python/Zope/ComponentArchitecture/GlobalUtilityService.py	Sun Dec 15 15:16:38 2002
@@ -22,7 +22,7 @@
 
 class IGlobalUtilityService(IUtilityService):
 
-    def provideUtility(providedInterface, component):
+    def provideUtility(providedInterface, component, name=''):
         """Provide a utility
 
         A utility is a component that provides an interface.
@@ -33,29 +33,40 @@
     __implements__=IGlobalUtilityService
     
     def __init__(self):
-        self.__utilities=ImplementorRegistry()
+        self.__utilities = {}
 
-    def provideUtility(self, providedInterface, component):
+    def provideUtility(self, providedInterface, component, name=''):
         """See IGlobalUtilityService interface"""
 
         if not providedInterface.isImplementedBy(component):
             raise Invalid("The registered component doesn't implement "
                           "the promised interface.")
+
+        registry = self.__utilities.get(name)
+        if registry is None:
+            registry = ImplementorRegistry()
+            self.__utilities[name] = registry
         
-        self.__utilities.register(providedInterface, component)
+        registry.register(providedInterface, component)
 
-    def getUtility(self, interface):
+    def getUtility(self, interface, name=''):
         """See IUtilityService interface"""
-        c = self.queryUtility(interface)
+        c = self.queryUtility(interface, None, name)
         if c is None:
             raise ComponentLookupError(interface)
         return c
 
-    def queryUtility(self, interface, default=None):
+    def queryUtility(self, interface, default=None, name=''):
         """See IUtilityService interface"""
-        c = self.__utilities.get(interface)
+
+        registry = self.__utilities.get(name)
+        if registry is None:
+            return default
+
+        c = registry.get(interface)
         if c is None:
             c = default
+
         return c
     
     _clear = __init__


=== Zope3/lib/python/Zope/ComponentArchitecture/IAdapterService.py 1.2 => 1.3 ===
--- Zope3/lib/python/Zope/ComponentArchitecture/IAdapterService.py:1.2	Mon Jun 10 19:29:23 2002
+++ Zope3/lib/python/Zope/ComponentArchitecture/IAdapterService.py	Sun Dec 15 15:16:38 2002
@@ -20,15 +20,21 @@
 
 class IAdapterService(Interface):
 
-    def getAdapter(object, interface):
+    def getAdapter(object, interface, name=''):
         """Look up an adapter that provides an interface for an object
 
+        If name is empty and the object already implements the
+        interface, then the object will be returned.
+
         A Zope.ComponentArchitecture.ComponentLookupError will be
         raised if the component can't be found.
         """
 
-    def queryAdapter(object, interface, default=None):
+    def queryAdapter(object, interface, default=None, name=''):
         """Look up an adapter that provides an interface for an object
+
+        If name is empty and the object already implements the
+        interface, then the object will be returned.
 
         The default will be returned if the component can't be found.
         """


=== Zope3/lib/python/Zope/ComponentArchitecture/IComponentArchitecture.py 1.2 => 1.3 ===
--- Zope3/lib/python/Zope/ComponentArchitecture/IComponentArchitecture.py:1.2	Thu Aug  1 14:42:17 2002
+++ Zope3/lib/python/Zope/ComponentArchitecture/IComponentArchitecture.py	Sun Dec 15 15:16:38 2002
@@ -42,14 +42,14 @@
 
     # Utility service
 
-    def getUtility(context, interface):
+    def getUtility(context, interface, name=''):
         """Get the utility that provides interface
 
         Returns the nearest utility to the context that implements
         the specified interface.  If one is not found, raises
         Zope.ComponentArchitecture.ComponentLookupError."""
 
-    def queryUtility(context, interface, default=None):
+    def queryUtility(context, interface, default=None, name=''):
         """Look for the utility that provides interface
 
         Returns the nearest utility to the context that implements
@@ -57,7 +57,7 @@
 
     # Adapter service
 
-    def getAdapter(object, interface, default=None, context=None):
+    def getAdapter(object, interface, default=None, name='', context=None):
         """Get adapter to interface for object
 
         Returns the nearest adapter to the context that can adapt
@@ -66,7 +66,7 @@
         adapter cannot be found, raises
         Zope.ComponentArchitecture.ComponentLookupError."""
 
-    def queryAdapter(object, interface, default=None, context=None):
+    def queryAdapter(object, interface, default=None, name='', context=None):
         """Look for adapter to interface for object
 
         Returns the nearest adapter to the context that can adapt


=== Zope3/lib/python/Zope/ComponentArchitecture/IUtilityService.py 1.3 => 1.4 ===
--- Zope3/lib/python/Zope/ComponentArchitecture/IUtilityService.py:1.3	Mon Jul  1 10:09:59 2002
+++ Zope3/lib/python/Zope/ComponentArchitecture/IUtilityService.py	Sun Dec 15 15:16:38 2002
@@ -20,14 +20,18 @@
 
 class IUtilityService(Interface):
 
-    def getUtility(interface):
-        """Look up a utility that provides interface.
+    def getUtility(interface, name=''):
+        """Look up a utility that provides an interface.
 
         If one is not found, raises
-        Zope.ComponentArchitecture.ComponentLookupError."""
+        Zope.ComponentArchitecture.ComponentLookupError.
+
+        """
 
-    def queryUtility(interface, default=None):
+    def queryUtility(interface, default=None, name=''):
         """Look up a utility that provides an interface.
 
-        If one is not found, returns default."""
+        If one is not found, returns default.
+
+        """
 


=== Zope3/lib/python/Zope/ComponentArchitecture/__init__.py 1.9 => 1.10 ===
--- Zope3/lib/python/Zope/ComponentArchitecture/__init__.py:1.9	Wed Dec  4 05:44:09 2002
+++ Zope3/lib/python/Zope/ComponentArchitecture/__init__.py	Sun Dec 15 15:16:38 2002
@@ -48,21 +48,22 @@
 
 # Utility service
 
-def getUtility(context, interface):
-    return getService(context, 'Utilities').getUtility(interface)
+def getUtility(context, interface, name=''):
+    return getService(context, 'Utilities').getUtility(interface, name)
 
-def queryUtility(context, interface, default=None):
-    return getService(context, 'Utilities').queryUtility(interface, default)
+def queryUtility(context, interface, default=None, name=''):
+    return getService(context, 'Utilities').queryUtility(
+        interface, default, name)
 
 # Adapter service
 
-def getAdapter(object, interface, context=None):
+def getAdapter(object, interface, name='', context=None):
     if context is None:
         context = object
     return getService(context, 'Adapters').getAdapter(
-        object, interface)
+        object, interface, name)
 
-def queryAdapter(object, interface, default=None, context=None):
+def queryAdapter(object, interface, default=None, name='', context=None):
     if context is None:
         context = object
     try:
@@ -72,7 +73,7 @@
         return default
     
     return adapters.queryAdapter(
-        object, interface, default)
+        object, interface, default, name)
 
 # Factory service