[Zope3-checkins] CVS: Zope3/src/zope/app/services - cache.py:1.18 connection.py:1.19 menu.py:1.5 utility.py:1.16

Stephan Richter srichter at cosmos.phy.tufts.edu
Tue Aug 19 20:11:12 EDT 2003


Update of /cvs-repository/Zope3/src/zope/app/services
In directory cvs.zope.org:/tmp/cvs-serv7817/services

Modified Files:
	cache.py connection.py menu.py utility.py 
Log Message:
Cleaned up the recent code significantly now that I understand the local
utility service better. Using registeredMatching() was just not the right
method to use. 

Added a new method called getLocalUtitliesFor() so that it is easier to 
grab the locally defined utilities only.


=== Zope3/src/zope/app/services/cache.py 1.17 => 1.18 ===
--- Zope3/src/zope/app/services/cache.py:1.17	Tue Aug 19 13:34:24 2003
+++ Zope3/src/zope/app/services/cache.py	Tue Aug 19 19:11:05 2003
@@ -17,15 +17,12 @@
 """
 from persistence import Persistent
 from zope.app import zapi
-from zope.app.component.nextservice import queryNextService
 from zope.app.interfaces.cache import ICache
 from zope.app.interfaces.event import IObjectModifiedEvent
 from zope.app.interfaces.services.cache import ILocalCachingService
 from zope.app.interfaces.services.service import ISimpleService
 from zope.app.services.event import ServiceSubscriberEventChannel
-from zope.app.services.servicenames import Caching, Utilities
-from zope.component import getService
-from zope.context import ContextMethod
+from zope.app.services.servicenames import Utilities
 from zope.interface import implements
 
 class CachingService(ServiceSubscriberEventChannel, Persistent):
@@ -40,36 +37,18 @@
     def getCache(self, name):
         'See ICachingService'
         utilities = zapi.getService(self, Utilities)
-        matching = utilities.getRegisteredMatching(ICache)
-        matching = filter(lambda m: m[1] == name, matching)
-        if matching and matching[0][2].active() is not None:
-            return matching[0][2].active().getComponent()
-        service = queryNextService(self, Caching)
-        if service is not None:
-            return service.getCache(name)
-        raise KeyError, name
-    getCache = ContextMethod(getCache)
+        return utilities.getUtility(ICache, name)
+    getCache = zapi.ContextMethod(getCache)
 
     def queryCache(self, name, default=None):
         'See ICachingService'
-        try:
-            return self.getCache(name)
-        except KeyError:
-            return default
-    queryCache = ContextMethod(queryCache)
+        utilities = zapi.getService(self, Utilities)
+        return utilities.queryUtility(ICache, default, name)
+    queryCache = zapi.ContextMethod(queryCache)
 
     def getAvailableCaches(self):
         'See ICachingService'
-        caches = []
         utilities = zapi.getService(self, Utilities)
-        matching = utilities.getRegisteredMatching(ICache)
-        for match in matching:
-            if match[2].active() is not None:
-                caches.append(match[1])
-        service = queryNextService(self, Caching)
-        if service is not None:
-            for name in service.getAvailableCaches():
-                if name not in caches:
-                    caches.append(name)
-        return caches
-    getAvailableCaches = ContextMethod(getAvailableCaches)
+        caches = utilities.getUtilitiesFor(ICache)
+        return map(lambda c: c[0], caches)
+    getAvailableCaches = zapi.ContextMethod(getAvailableCaches)


=== Zope3/src/zope/app/services/connection.py 1.18 => 1.19 ===
--- Zope3/src/zope/app/services/connection.py:1.18	Tue Aug 19 03:09:53 2003
+++ Zope3/src/zope/app/services/connection.py	Tue Aug 19 19:11:05 2003
@@ -17,12 +17,10 @@
 """
 from persistence import Persistent
 from zope.app import zapi
-from zope.app.component.nextservice import queryNextService
 from zope.app.interfaces.rdb import IZopeDatabaseAdapter
 from zope.app.interfaces.services.connection import ILocalConnectionService
-from zope.app.interfaces.services.registration import ActiveStatus
 from zope.app.interfaces.services.service import ISimpleService
-from zope.app.services.servicenames import SQLDatabaseConnections, Utilities
+from zope.app.services.servicenames import Utilities
 from zope.interface import implements
 
 class ConnectionService(Persistent):
@@ -33,39 +31,18 @@
     def getConnection(self, name):
         'See IConnectionService'
         utilities = zapi.getService(self, Utilities)
-        matching = utilities.getRegisteredMatching(IZopeDatabaseAdapter)
-        matching = filter(lambda m: m[1] == name, matching)
-        if matching and matching[0][2].active() is not None:
-            return matching[0][2].active().getComponent()
-        service = queryNextService(self, SQLDatabaseConnections)
-        if service is not None:
-            return service.getConnection(name)
-        raise KeyError, name
-
+        return utilities.getUtility(IZopeDatabaseAdapter, name)
     getConnection = zapi.ContextMethod(getConnection)
 
     def queryConnection(self, name, default=None):
         'See IConnectionService'
-        try:
-            return self.getConnection(name)
-        except KeyError:
-            return default
-
+        utilities = zapi.getService(self, Utilities)
+        return utilities.queryUtility(IZopeDatabaseAdapter, default, name)
     queryConnection = zapi.ContextMethod(queryConnection)
 
     def getAvailableConnections(self):
         'See IConnectionService'
-        connections = []
         utilities = zapi.getService(self, Utilities)
-        matching = utilities.getRegisteredMatching(IZopeDatabaseAdapter)
-        for match in matching:
-            if match[2].active() is not None:
-                connections.append(match[1])
-        service = queryNextService(self, SQLDatabaseConnections)
-        if service is not None:
-            for name in service.getAvailableConnections():
-                if name not in connections:
-                    connections.append(name)
-        return connections
-
+        connections = utilities.getUtilitiesFor(IZopeDatabaseAdapter)
+        return map(lambda c: c[0], connections)
     getAvailableConnections = zapi.ContextMethod(getAvailableConnections)


=== Zope3/src/zope/app/services/menu.py 1.4 => 1.5 ===
--- Zope3/src/zope/app/services/menu.py:1.4	Sun Aug 17 02:08:11 2003
+++ Zope3/src/zope/app/services/menu.py	Tue Aug 19 19:11:05 2003
@@ -119,8 +119,8 @@
     def getAllLocalMenus(self):
         """See zope.app.interfaces.publisher.browser.IBrowserMenuService"""
         utilities = zapi.getService(self, Utilities)
-        matching = utilities.getRegisteredMatching(ILocalBrowserMenu)
-        return map(lambda m: m[2].active().getComponent(), matching)
+        menus = utilities.getLocalUtilitiesFor(ILocalBrowserMenu)
+        return map(lambda m: m[1], menus)
     getAllLocalMenus = ContextMethod(getAllLocalMenus)
 
 
@@ -136,9 +136,10 @@
     def queryLocalMenu(self, menu_id, default=None):
         """See zope.app.interfaces.services.menu.ILocalBrowserMenuService"""
         utilities = zapi.getService(self, Utilities)
-        matching = utilities.getRegisteredMatching(ILocalBrowserMenu, menu_id)
-        if matching and matching[0][2].active():
-            return matching[0][2].active().getComponent()
+        menus = utilities.getLocalUtilitiesFor(ILocalBrowserMenu)
+        for name, menu in menus:
+            if name == menu_id:
+                return menu
         return default
     queryLocalMenu = ContextMethod(queryLocalMenu)
 


=== Zope3/src/zope/app/services/utility.py 1.15 => 1.16 ===
--- Zope3/src/zope/app/services/utility.py:1.15	Fri Aug 15 20:44:08 2003
+++ Zope3/src/zope/app/services/utility.py	Tue Aug 19 19:11:05 2003
@@ -16,17 +16,17 @@
 
 $Id$
 """
-
 from zope.interface import implements
 from persistence.dict import PersistentDict
 from persistence import Persistent
 from zope.app.component.nextservice import getNextService
 from zope.app.interfaces.services.registration import IRegistry
 from zope.app.interfaces.services.service import ISimpleService
-from zope.app.interfaces.services.utility import IUtilityRegistration
-from zope.app.interfaces.services.utility import ILocalUtilityService
-from zope.app.services.registration import RegistrationStack
-from zope.app.services.registration import ComponentRegistration
+from zope.app.interfaces.services.utility import \
+     IUtilityRegistration, ILocalUtilityService
+from zope.app.services.servicenames import Utilities
+from zope.app.services.registration import \
+     RegistrationStack, ComponentRegistration
 from zope.component.exceptions import ComponentLookupError
 from zope.interface.implementor import ImplementorRegistry
 from zope.context import ContextMethod
@@ -41,6 +41,7 @@
         self._utilities = PersistentDict()
 
     def getUtility(self, interface, name=''):
+        """See zope.component.interfaces.IUtilityService"""
         utility = self.queryUtility(interface, name=name)
         if utility is None:
             raise ComponentLookupError("utility", interface, name)
@@ -48,23 +49,70 @@
     getUtility = ContextMethod(getUtility)
 
     def queryUtility(self, interface, default=None, name=''):
+        """See zope.component.interfaces.IUtilityService"""
         stack = self.queryRegistrations(name, interface)
         if stack is not None:
             registration = stack.active()
             if registration is not None:
                 return registration.getComponent()
 
-        next = getNextService(self, "Utilities")
+        next = getNextService(self, Utilities)
         return next.queryUtility(interface, default, name)
     queryUtility = ContextMethod(queryUtility)
 
+    def getUtilitiesFor(self, interface):
+        """See zope.component.interfaces.IUtilityService"""
+        utilities = self.getLocalUtilitiesFor(interface)
+        names = map(lambda u: u[0], utilities)
+
+        next = getNextService(self, Utilities)
+        for utility in next.getUtilitiesFor(interface):
+            if utility[0] not in names:
+                utilities.append(utility)
+        return utilities
+    getUtilitiesFor = ContextMethod(getUtilitiesFor)
+
+    def getLocalUtilitiesFor(self, interface):
+        """See zope.app.interfaces.services.ILocalUtilityService"""
+        utilities = []
+        for name in self._utilities:
+            for iface, cr in self._utilities[name].getRegisteredMatching():
+                cr = ContextWrapper(cr, self)
+                if not cr or cr.active() is None:
+                    continue
+                utility = cr.active().getComponent()
+                if interface and not iface.extends(interface, 0) and \
+                       removeAllProxies(utility) is not interface:
+                    continue
+                utilities.append((name, utility))
+        return utilities
+    getLocalUtilitiesFor = ContextMethod(getLocalUtilitiesFor)
+
+
+    def getRegisteredMatching(self, interface=None, name=None):
+        """See zope.app.interfaces.services.ILocalUtilityService"""
+        L = []
+        for reg_name in self._utilities:
+            for iface, cr in self._utilities[reg_name].getRegisteredMatching():
+                if not cr:
+                    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, ContextWrapper(cr, self)))
+        return L
+    getRegisteredMatching = ContextMethod(getRegisteredMatching)
+
     def queryRegistrationsFor(self, registration, default=None):
+        """zope.app.interfaces.services.registration.IRegistry"""
         return self.queryRegistrations(registration.name,
                                         registration.interface,
                                         default)
     queryRegistrationsFor = ContextMethod(queryRegistrationsFor)
 
     def queryRegistrations(self, name, interface, default=None):
+        """zope.app.interfaces.services.registration.IRegistry"""
         utilities = self._utilities.get(name)
         if utilities is None:
             return default
@@ -76,12 +124,14 @@
     queryRegistrations = ContextMethod(queryRegistrations)
 
     def createRegistrationsFor(self, registration):
+        """zope.app.interfaces.services.registration.IRegistry"""
         return self.createRegistrations(registration.name,
                                          registration.interface)
 
     createRegistrationsFor = ContextMethod(createRegistrationsFor)
 
     def createRegistrations(self, name, interface):
+        """zope.app.interfaces.services.registration.IRegistry"""
         utilities = self._utilities.get(name)
         if utilities is None:
             utilities = ImplementorRegistry(PersistentDict())
@@ -95,41 +145,6 @@
         return ContextWrapper(stack, self)
     createRegistrations = ContextMethod(createRegistrations)
 
-    def getRegisteredMatching(self, interface=None, name=None):
-        L = []
-        for reg_name in self._utilities:
-            for iface, cr in self._utilities[reg_name].getRegisteredMatching():
-                if not cr:
-                    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, ContextWrapper(cr, self)))
-        return L
-    getRegisteredMatching = ContextMethod(getRegisteredMatching)
-
-    def getUtilitiesFor(self, interface=None):
-        utilities = {}
-        for name in self._utilities:
-            for iface, cr in self._utilities[name].getRegisteredMatching():
-                if not cr:
-                    continue
-                cr = ContextWrapper(cr, self)
-                utility = cr.active().getComponent()
-                if interface and not iface.extends(interface, 0) and \
-                       removeAllProxies(utility) is not interface:
-                    continue
-                utilities[(name, utility)] = None
-
-        next = getNextService(self, "Utilities")
-
-        for utility in next.getUtilitiesFor(interface):
-            if not utilities.has_key(utility):
-                utilities[utility] = None
-        return utilities.keys()
-    
-    getUtilitiesFor = ContextMethod(getUtilitiesFor)
 
 class UtilityRegistration(ComponentRegistration):
     """Utility component registration for persistent components




More information about the Zope3-Checkins mailing list