[Zope3-checkins] CVS: Zope3/src/zope/app/adapter - adapter.py:1.5 tests.py:1.5

Jim Fulton jim at zope.com
Wed Apr 7 15:19:28 EDT 2004


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

Modified Files:
	adapter.py tests.py 
Log Message:
Implemented a new interface, IComponentRegistry, that defines a method,
registrations, for getting all of the registrations from a registry as
objects. This simple method is meant to replace getRegisteredMatching.


=== Zope3/src/zope/app/adapter/adapter.py 1.4 => 1.5 ===
--- Zope3/src/zope/app/adapter/adapter.py:1.4	Mon Mar 15 15:42:24 2004
+++ Zope3/src/zope/app/adapter/adapter.py	Wed Apr  7 15:18:57 2004
@@ -67,14 +67,21 @@
 
     zope.interface.implements(
         zope.app.registration.interfaces.IRegistry,
+        zope.component.interfaces.IComponentRegistry,
         )
     
     _surrogateClass = LocalSurrogate
+
+    # Next local registry, may be None
     next = None
+
     subs = ()
 
     def __init__(self, base, next=None):
+
+        # Base registry. This is always a global registry
         self.base = base
+
         self.adapters = {}
         self.stacks = PersistentDict()
         AdapterRegistry.__init__(self)
@@ -168,6 +175,19 @@
 
     notifyActivated = notifyDeactivated = adaptersChanged
 
+    def registrations(self):
+        for stacks in self.stacks.itervalues():
+            for stack in stacks.itervalues():
+                for info in stack.info():
+                    yield info['registration']
+
+        next = self.next
+        if next is None:
+            next = self.base
+
+        for registration in next.registrations():
+            yield registration
+
 class LocalAdapterBasedService(
     zope.app.container.contained.Contained,
     Persistent,
@@ -230,7 +250,6 @@
         LocalAdapterRegistry.__init__(
             self, zapi.getService(None, zapi.servicenames.Adapters)
             )
-
 
 class IAdapterRegistration(
     zope.app.registration.interfaces.IRegistration):


=== Zope3/src/zope/app/adapter/tests.py 1.4 => 1.5 ===
--- Zope3/src/zope/app/adapter/tests.py:1.4	Mon Mar 15 15:42:24 2004
+++ Zope3/src/zope/app/adapter/tests.py	Wed Apr  7 15:18:57 2004
@@ -13,14 +13,18 @@
 ##############################################################################
 """Local Adapter Tests
 
-   Local surrogates and surrogate registries share declarations with
+   Local surrogates and adapter registries share declarations with
    those "above" them.
 
-   Suppose we have a global AdapterRegistry:
+   Local adapter registries have "base" registries that mist be
+   IComponentRegistry objects.
 
-   >>> G = AdapterRegistry()
+   Suppose we have a global adapter service, which is a type of
+   adapter registry that is an IComponentRegistry:
 
-   we also have a local surrogate registry, with G as it's base:
+   >>> G = GlobalAdapterService()
+
+   we also have a local adapter registry, with G as it's base:
 
    >>> L1 = LocalAdapterRegistry(G)
 
@@ -124,16 +128,38 @@
    >>> L2.lookup([IF2], IB0)
    'A10G'
 
+   We can ask for all of the registrations locally:
+
+   >>> registrations = list(L1.registrations())
+   >>> registrations.sort()
+   >>> for registration in registrations:
+   ...     print registration
+   AdapterRegistration(('IF1',), 'IB1', '', 'A10G', '')
+   Registration('IF0', (), 'IB1', u'', 'A011')
+
+   This shows the local registrations in L1 and the global registrations.
+
+   If we ask L2, we'll see the registrations from G, L1, and L2:
+   
+   >>> registrations = list(L2.registrations())
+   >>> registrations.sort()
+   >>> for registration in registrations:
+   ...     print registration
+   AdapterRegistration(('IF1',), 'IB1', '', 'A10G', '')
+   Registration('IF0', (), 'IB1', u'', 'A011')
+   Registration('IF1', (), 'IB0', u'', 'A102')
+   Registration('IF1', (), 'IB1', u'', 'A112')
+
    $Id$
    """
 
 def test_named_adapters():
     """
-    Suppose we have a global AdapterRegistry:
+    Suppose we have a GlobalAdapterService:
 
-    >>> G = AdapterRegistry()
+    >>> G = GlobalAdapterService()
 
-    we also have a local surrogate registry, with G as it's base:
+    we also have a local adapter registry, with G as it's base:
 
     >>> L1 = LocalAdapterRegistry(G)
 
@@ -258,11 +284,11 @@
 
 def test_multi_adapters():
     """
-    Suppose we have a global AdapterRegistry:
+    Suppose we have a globalGlobalAdapterService:
 
-    >>> G = AdapterRegistry()
+    >>> G = GlobalAdapterService()
 
-    we also have a local surrogate registry, with G as it's base:
+    we also have a local adapter registry, with G as it's base:
 
     >>> L1 = LocalAdapterRegistry(G)
 
@@ -544,7 +570,7 @@
 
 def test_local_default():
     """
-    >>> G = AdapterRegistry()
+    >>> G = GlobalAdapterService()
     >>> L1 = LocalAdapterRegistry(G)
     >>> r = Registration(required = None, provided=IB1, factory='Adapter')
     >>> L1.createRegistrationsFor(r).activate(r)
@@ -555,7 +581,7 @@
 
 def test_changing_next():
     """
-    >>> G = AdapterRegistry()
+    >>> G = GlobalAdapterService()
     >>> L1 = LocalAdapterRegistry(G)
     >>> L2 = LocalAdapterRegistry(G, L1)
     >>> f2 = F2()
@@ -691,7 +717,7 @@
 
 import unittest
 from zope.testing.doctestunit import DocTestSuite
-from zope.interface.adapter import AdapterRegistry
+from zope.component.adapter import GlobalAdapterService
 from zope.app.adapter.adapter import LocalAdapterRegistry
 from zope.app.adapter.adapter import LocalAdapterBasedService
 import zope.interface
@@ -740,15 +766,26 @@
         self.__dict__.update(kw)
 
     def __repr__(self):
-        return "<Registration %s>" % self.__dict__
+        return "Registration(%r, %s, %r, %r, %r)" % (
+            getattr(self.required, '__name__', None),
+            tuple([i.__name__ for i in self.with]),
+            self.provided.__name__, self.name, self.factory
+            )
+
+    def __cmp__(self, other):
+        if self.__class__ != other.__class__:
+            return cmp(repr(self.__class__), repr(other.__class__))
+
+        return cmp(repr(self), repr(other))
+    
 
     def factories(self):
         return self.factory,
     factories = property(factories)
 
 # Create a picklable global registry. The pickleability of other
-# global surrogate registries is beyond the scope of these tests:
-class GlobalAdapterRegistry(AdapterRegistry):
+# global adapter registries is beyond the scope of these tests:
+class GlobalAdapterRegistry(GlobalAdapterService):
     def __reduce__(self):
         return 'globalAdapterRegistry'
 
@@ -756,12 +793,15 @@
 
 class TestStack:
     registration = None
+    registrations = ()
 
     def __init__(self, parent):
         self.__parent__ = parent
 
     def activate(self, registration):
         self.registration = registration
+        if registration not in self.registrations:
+            self.registrations += (registration,)
         self.__parent__.notifyActivated(self, registration)
 
     def deactivate(self, registration):
@@ -770,7 +810,10 @@
 
     def active(self):
         return self.registration
-    
+
+    def info(self):
+        for registration in self.registrations:
+            yield {'registration': registration}
 
 class LocalAdapterRegistry(LocalAdapterRegistry):
     """For testing, use custom stack type




More information about the Zope3-Checkins mailing list