[Zope-Checkins] CVS: Zope2 - Adapter.py:1.1.2.4

shane@digicool.com shane@digicool.com
Thu, 28 Jun 2001 17:46:58 -0400 (EDT)


Update of /cvs-repository/Zope2/lib/python/ComponentArchitecture
In directory korak.digicool.com:/tmp/cvs-serv29934

Modified Files:
      Tag: NR-branch
	Adapter.py 
Log Message:
Commentized



--- Updated File Adapter.py in package Zope2 --
--- Adapter.py	2001/06/28 16:07:50	1.1.2.3
+++ Adapter.py	2001/06/28 21:46:58	1.1.2.4
@@ -96,52 +96,77 @@
 
 import Errors
 
-class GlobalAdapterFactory:
+class GlobalAdapterRegistry:
     
-    __implements__=IAdapterFactory
-
     def __init__(self):
-        _adapters={}
+        self._adapters = {}
 
-    def _provideAdapter(self, input, output, adapter, boutput):
-        old = self._adapters[input, boutput]
+    def _provideAdapters(self, input, output, adapter, base_output):
+        '''
+        Registers an adapter using base_output as a key.
+        Also registers base interfaces of base_output unless
+        the current registry has something more general than
+        the new adapter.
+        '''
+        old = self._adapters.get((input, base_output), None)
         if old is not None:
-            oldoutput=old[0]
-            if not old.extends[output]:
-                return # The new output is not more general
-        self._adapters[input, boutput]=output, adapter
-        for b in boutput.__bases__:
-            self._provideAdapter(input, output, adapter, b)        
+            oldoutput = old[0]
+            if oldoutput is not output:
+                if not oldoutput.extends(output):
+                    # The new output is not more general, so don't
+                    # replace the adapter.
+                    # We want to provide the most specific adapter
+                    # possible but we don't want more specific
+                    # adapters to accidentally override
+                    # general adapters.
+                    return
+        self._adapters[(input, base_output)] = output, adapter
+        for b in base_output.__bases__:
+            self._provideAdapters(input, output, adapter, b)
 
     def provideAdapter(self, input, output, adapter):
+        '''
+        Registers an adapter.
+        '''
         self._provideAdapters(input, output, adapter, output)
 
     def _getAdapter(self, input, output):
-        marker=None
-        a=_adapters.get((input, output), marker)
-        if a is not marker: return a
-        for input in input.__bases__:
-            a=_getAdapter(input, output)
-            if a is not marker: return a
+        '''
+        Finds a registered adapter given two interfaces.
+        '''
+        a = self._adapters.get((input, output), None)
+        if a is not None:
+            return a[1]
+        for i in input.__bases__:
+            a = self._getAdapter(i, output)
+            if a is not None:
+                return a
         return _marker
 
     def getAdapterForInterfaces(self, inputs, output, TupleType=type(())):
-        marker=None
+        '''
+        Finds a registered adapter given a hierarchy of input interfaces
+        and an output interface.
+        '''
         if type(inputs) is TupleType:
             for input in inputs:
-                a=getAdapterForInterfaces(input, output)
-                if a is not marker: return a
+                a = self.getAdapterForInterfaces(input, output)
+                if a is not None:
+                    return a
         else:
-            return _getAdapter(inputs, output)
+            return self._getAdapter(inputs, output)
 
     def getAdapter(self, object, output, default=_marker):
-        if output.implementedBy(object): return object
+        '''
+        Finds an adapter for an object by examining what it implements.
+        '''
+        if output.isImplementedBy(object): return object
 
-        a=getattr(object, '__implements__', _marker)
+        a = getattr(object, '__implements__', _marker)
         if a is not _marker:
-            a=getAdapterForInterfaces(a, output)
+            a = self.getAdapterForInterfaces(a, output)
         else:
-            a = _getAdapter(None, output)
+            a = self._getAdapter(None, output)
 
         if a is _marker:
             raise Errors.AdapterNotFound(object, output)