[Zope3-checkins] CVS: Zope3/src/zope/component - utility.py:1.10

Jim Fulton jim at zope.com
Sun Apr 11 14:17:13 EDT 2004


Update of /cvs-repository/Zope3/src/zope/component
In directory cvs.zope.org:/tmp/cvs-serv19330/src/zope/component

Modified Files:
	utility.py 
Log Message:
Reimplemented the global utility service using adapter registries.

Added a registrations method.


=== Zope3/src/zope/component/utility.py 1.9 => 1.10 ===
--- Zope3/src/zope/component/utility.py:1.9	Wed Mar 10 06:34:01 2004
+++ Zope3/src/zope/component/utility.py	Sun Apr 11 14:16:43 2004
@@ -16,12 +16,12 @@
 $Id$
 """
 
-from zope.interface.implementor import ImplementorRegistry
-from zope.interface import implements
-from zope.component.interfaces import IUtilityService
 from zope.component.exceptions import Invalid, ComponentLookupError
+from zope.component.interfaces import IUtilityService, IComponentRegistry
+from zope.interface.adapter import AdapterRegistry
+from zope.interface import implements
 
-class IGlobalUtilityService(IUtilityService):
+class IGlobalUtilityService(IUtilityService, IComponentRegistry):
 
     def provideUtility(providedInterface, component, name=''):
         """Provide a utility
@@ -29,26 +29,27 @@
         A utility is a component that provides an interface.
         """
 
-class GlobalUtilityService:
+class GlobalUtilityService(AdapterRegistry):
 
     implements(IGlobalUtilityService)
 
     def __init__(self):
-        self.__utilities = {}
-
-    def provideUtility(self, providedInterface, component, name=''):
-        """See IGlobalUtilityService interface"""
+        AdapterRegistry.__init__(self)
+        self._registrations = {}
 
+    def provideUtility(self, providedInterface, component, name='', info=''):
+    
         if not providedInterface.providedBy(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.register((), providedInterface, name, component)
+
+        self._registrations[(providedInterface, name)] = UtilityRegistration(
+            providedInterface, name, component, info)
 
-        registry.register(providedInterface, component)
+    def registrations(self):
+        return self._registrations.itervalues()
 
     def getUtility(self, interface, name=''):
         """See IUtilityService interface"""
@@ -60,48 +61,38 @@
     def queryUtility(self, interface, default=None, name=''):
         """See IUtilityService interface"""
 
-        registry = self.__utilities.get(name)
-        if registry is None:
-            return default
+        return self.lookup((), interface, name, default)
 
-        c = registry.get(interface)
-        if c is not None:
-            return c
-
-        return default
+    def getUtilitiesFor(self, interface):
+        return AdapterRegistry.lookupAll(self, (), interface)
 
     def getRegisteredMatching(self, interface=None, name=None):
-        L = []
-        for reg_name in self.__utilities:
-            for iface, c in self.__utilities[reg_name].getRegisteredMatching():
-                if c is None:
-                    continue
-                if interface and not iface is interface:
-                    continue
-                if name is not None and reg_name.find(name) < 0:
-                    continue
-                L.append((iface, reg_name, c))
-        return L
-
-    def getUtilitiesFor(self, interface=None):
-        utilities = {}
-        for name in self.__utilities:
-            for iface, util in self.__utilities[name].getRegisteredMatching():
-                if util is None:
-                    continue
-                if interface and not iface.extends(interface, 0):
-                    continue
-                utilities[(name, util)] = None
-
-        return utilities.keys()
-
-    _clear = __init__
-
-# the global utility service instance (see component.zcml )
-utilityService = GlobalUtilityService()
-_clear         = utilityService._clear
-
-# Register our cleanup with Testing.CleanUp to make writing unit tests simpler.
-from zope.testing.cleanup import addCleanUp
-addCleanUp(_clear)
-del addCleanUp
+        # doomed method
+        lameresult = []
+        for registration in self.registrations():
+            if (interface is not None
+                and interface is not registration.provided):
+                continue
+            if (name is not None
+                and registration.name.find(name) < 0):
+                continue
+            lameresult.append((registration.provided, registration.name,
+                               registration.value))
+        return lameresult
+        
+
+class UtilityRegistration(object):
+
+    def __init__(self, provided, name, value, doc):
+        self.provided = provided
+        self.name = name
+        self.value = value
+        self.doc = doc
+
+    def __repr__(self):
+        return '%s(%r, %r, %r, %r)' % (
+            self.__class__.__name__,
+            self.provided.__name__, self.name,
+            getattr(self.value, '__name__', self.value), self.doc,
+            )
+




More information about the Zope3-Checkins mailing list