[Zope-Checkins] CVS: Zope/lib/python/ComponentArchitecture - Adapter.py:1.1.4.2 Content.py:1.1.4.2 Presentation.py:1.1.4.4 __init__.py:1.1.4.2

Shane Hathaway shane@digicool.com
Wed, 26 Sep 2001 13:36:35 -0400


Update of /cvs-repository/Zope/lib/python/ComponentArchitecture
In directory cvs.zope.org:/tmp/cvs-serv15117

Modified Files:
      Tag: ComponentArchitecture-branch
	Adapter.py Content.py Presentation.py __init__.py 
Log Message:
Tweaked presentation, Content interface, and adapters to more closely
match the ComponentArchitecture wiki.


=== Zope/lib/python/ComponentArchitecture/Adapter.py 1.1.4.1 => 1.1.4.2 ===
 ##############################################################################
 """(simple) Adapter management
-
-If an adapter provides an output for an input:
-
-  - It provides bases of the output for the input.
-
-  - It provides the output (and bases of the output)
-    for interfaces that extend the
-    input.
-
 """
 
-from types import TupleType
+from IToIRegistry import IToIRegistry
+from Interface import objectImplements
 
 import Errors
 
 _marker = []  # Create a new marker object.
 
-class GlobalAdapterRegistry:
+class GlobalAdapterRegistry (IToIRegistry):
     
-    def __init__(self):
-        self._adapters = {}
-
-    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 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):
-        '''
-        Finds a registered adapter given two interfaces.
-        '''
-        a = self._adapters.get((input, output), None)
-        if a is not None:
-            return a[1]
-        bases = getattr(input, '__bases__', ())
-        if bases:
-            for base in bases:
-                a = self._getAdapter(base, output)
-                if a is not None:
-                    return a
-        return None
-
-    def _getAdapterForInterfaces(self, inputs, output):
-        '''
-        Finds a registered adapter given a hierarchy of input interfaces
-        and an output interface.
-        '''
-        if type(inputs) is TupleType:
-            for input in inputs:
-                a = self._getAdapterForInterfaces(input, output)
-                if a is not None:
-                    return a
-        else:
-            # inputs is an interface object.
-            return self._getAdapter(inputs, output)
+        self.register(input, output, adapter)
 
     def getAdapter(self, object, output, default=_marker):
         '''
         Finds an adapter for an object by examining what it implements.
         '''
         if output.isImplementedBy(object): return object
-
-        inputs = getattr(object, '__implements__', _marker)
-        if inputs is not _marker:
-            a = self._getAdapterForInterfaces(inputs, output)
-        else:
-            # No input interfaces known.
-            a = self._getAdapter(None, output)
+        inputs = objectImplements(object)
+        a = None
+        if inputs:
+            for input in inputs:
+                a = self.get(input, output)
+                if a is not None:
+                    break
+        if a is None:
+            # Try to find an adapter that adapts anything.
+            a = self.get(None, output)
 
         if a is None:
             if default is not _marker:
@@ -183,4 +123,10 @@
                 raise Errors.AdapterNotFound(object, output)
         # assert output.isImplementedBy(a(object))
         return a(object)
+
+
+global_reg = GlobalAdapterRegistry()
+
+getAdapter = global_reg.getAdapter
+provideAdapter = global_reg.provideAdapter
 


=== Zope/lib/python/ComponentArchitecture/Content.py 1.1.4.1 => 1.1.4.2 ===
 
 class Content (Interface.Base):
-    '''
-    '''
+    """
+    """
+
+
+class LeafContent (Content):
+    """
+    """
 
 
 class ContentContainer (Content):
-    '''
-    '''
+    """
+    """
 
 
 provideInterface(Content)


=== Zope/lib/python/ComponentArchitecture/Presentation.py 1.1.4.3 => 1.1.4.4 ===
     """
 
-    def getUnboundComponent(inputs, output, name):
+    def getUnboundComponent(inputs, name, output):
         """
         Returns a presentation component.
         """
@@ -110,20 +110,21 @@
 
 _global_regs = {}  # name -> IToIRegistry
 
-def registerPresentation(input, output, name, unbound_comp):
+def registerPresentation(input, name, output, unbound_comp):
     reg = _global_regs.get(name, None)
     if reg is None:
         _global_regs[name] = reg = IToIRegistry()
     reg.register(input, output, unbound_comp)
 
-def getPresentation(object, output, name, default=_marker):
+
+def getPresentation(object, name, output, default=_marker):
     """
     Finds a presentation for an object by examining what it implements.
     Searches in services then the global registry.
     """
     inputs = objectImplements(object)
     c = findComponent(PRESENTATION_SERVICE_NAME, object,
-                      (inputs, output, name))
+                      (inputs, name, output))
     if c is None:
         reg = _global_regs.get(name, None)
         if reg is not None:


=== Zope/lib/python/ComponentArchitecture/__init__.py 1.1.4.1 => 1.1.4.2 ===
 '''
 
-import Adapter
-r = Adapter.GlobalAdapterRegistry()
-
-getAdapter = r.getAdapter
-provideAdapter = r.provideAdapter
+from Adapter import getAdapter, provideAdapter
 
 import Presentation
 getPresentation = Presentation.getPresentation
-providePresentation = Presentation.global_reg.provideRealization
+providePresentation = Presentation.registerPresentation
 
-from Content import Content, ContentContainer
+##from Content import Content, ContentContainer
 
-import FactoryComponents
-provideSimpleFactory = FactoryComponents.provideSimpleFactory
+##import FactoryComponents
+##provideSimpleFactory = FactoryComponents.provideSimpleFactory
 
 
-__all__ = (
-    'getAdapter', 'provideAdapter',
-    'getPresentation', 'providePresentation',
-    'Content', 'ContentContainer',
-    'provideSimpleFactory',
-    )
+##__all__ = (
+##    'getAdapter', 'provideAdapter',
+##    'getPresentation', 'providePresentation',
+##    'Content', 'ContentContainer',
+##    'provideSimpleFactory',
+##    )