[Zope3-checkins] CVS: Zope3/src/zope/app/services - interface.py:1.12 utility.py:1.13

Sidnei da Silva sidnei at x3ng.com.br
Wed Aug 6 18:17:17 EDT 2003


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

Modified Files:
	interface.py utility.py 
Log Message:
Finally got my own mini-geddon.

Changes:

 - Spiced up a bit the Utility service getRegisteredMatching to allow querying by interface and name.
 - Added getUtilitiesFor(interface) to the utility service, to allow querying for a list of utilities that provide an interface.
 - Made the local and global interface services delegate to the utility service when querying for available interfaces. This is done by querying for utilities that provide IInterface. In the future (read: later this week) this will allow us to have persistent interfaces registered as local utilities, a.k.a. TTW Schema.
 - Added tests for all of this, and run the test suite twice to make sure :)



=== Zope3/src/zope/app/services/interface.py 1.11 => 1.12 ===
--- Zope3/src/zope/app/services/interface.py:1.11	Thu Jul  3 18:46:15 2003
+++ Zope3/src/zope/app/services/interface.py	Wed Aug  6 17:16:41 2003
@@ -23,13 +23,16 @@
 from zodb.code.patch import registerWrapper, Wrapper
 from zope.interface.interface import InterfaceClass
 
+from zope.component import getService
 from zope.app.component.nextservice import getNextService
 from zope.app.interfaces.services.service import ISimpleService
 from zope.app.interfaces.component import IInterfaceService
 from zope.app import zapi
-from zope.app.services.servicenames import Interfaces
+from zope.app.services.servicenames import Interfaces, Utilities
 from zope.component import ComponentLookupError
 from zope.interface import implements
+from zope.interface.interfaces import IInterface
+from zope.app.interfaces.services.registration import IRegistrationStack
 
 class PersistentInterfaceClass(Persistent, InterfaceClass):
     pass
@@ -70,19 +73,54 @@
     def queryInterface(self, id, default=None):
         # Return the interface registered for the given id
         next = getNextService(self, Interfaces)
-        return next.queryInterface(id, default)
+        iface = next.queryInterface(id, default)
+        if iface is default:
+            utility_service = getService(self, Utilities)
+            utility = utility_service.queryUtility(IInterface, name=id)
+            if utility is not None:
+                return utility
+            return default
+        return iface
 
     def searchInterface(self, search_string="", base=None):
         # Return the interfaces that match the search criteria
+        ifaces = {}
         next = getNextService(self, Interfaces)
-        return next.searchInterface(search_string, base)
+        for iface in next.searchInterface(search_string, base):
+            ifaces[iface] = None
+        for item in self._queryUtilityInterfaces(base, search_string):
+            if not ifaces.has_key(item[1]):
+                ifaces[item[1]] = None
+        return ifaces.keys()
 
     def searchInterfaceIds(self, search_string="", base=None):
         # Return the ids of the interfaces that match the search criteria.
+        ids = {}
         next = getNextService(self, Interfaces)
-        return next.searchInterfaceIds(search_string, base)
+        for id in next.searchInterfaceIds(search_string, base):
+            ids[id] = None
+        for item in self._queryUtilityInterfaces(base, search_string):
+            if not ids.has_key(item[0]):
+                ids[item[0]] = None
+        return ids.keys()
 
     def items(self, search_string="", base=None):
         # Return id, interface pairs for all items matching criteria.
+        items = {}
         next = getNextService(self, Interfaces)
-        return next.items(search_string, base)
+        for item in next.items(search_string, base):
+            items[item] = None
+        for item in self._queryUtilityInterfaces(base, search_string):
+            items[item] = item
+        return items.keys()
+
+    def _queryUtilityInterfaces(self, interface=None, search_string=None):
+        if interface is None:
+            interface = IInterface
+        utilities = getService(self, Utilities)
+        matching = utilities.getUtilitiesFor(interface)
+        if search_string is not None:
+            return [match for match in matching
+                    if match[0].find(search_string) > -1]
+        return matching
+


=== Zope3/src/zope/app/services/utility.py 1.12 => 1.13 ===
--- Zope3/src/zope/app/services/utility.py:1.12	Sun Jun 22 20:31:31 2003
+++ Zope3/src/zope/app/services/utility.py	Wed Aug  6 17:16:41 2003
@@ -94,16 +94,37 @@
         return ContextWrapper(stack, self)
     createRegistrations = ContextMethod(createRegistrations)
 
-    def getRegisteredMatching(self):
+    def getRegisteredMatching(self, interface=None, name=None):
         L = []
-        for name in self._utilities:
-            for iface, cr in self._utilities[name].getRegisteredMatching():
+        for reg_name in self._utilities:
+            for iface, cr in self._utilities[reg_name].getRegisteredMatching():
                 if not cr:
                     continue
-                L.append((iface, name, ContextWrapper(cr, self)))
+                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[reg_name].getRegisteredMatching():
+                if not cr:
+                    continue
+                if interface and not iface is interface:
+                    continue
+                utility = cr.active().getComponent()
+                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