[Zope-Checkins] CVS: Zope2 - Presentation.py:1.1.2.1 Adapter.py:1.1.2.5 Errors.py:1.1.2.2 __init__.py:1.1.2.3

shane@digicool.com shane@digicool.com
Fri, 29 Jun 2001 12:12:20 -0400 (EDT)


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

Modified Files:
      Tag: NR-branch
	Adapter.py Errors.py __init__.py 
Added Files:
      Tag: NR-branch
	Presentation.py 
Log Message:
Added Presentation and clarified things



--- Added File Presentation.py in package Zope2 ---
##############################################################################
# 
# Zope Public License (ZPL) Version 1.0
# -------------------------------------
# 
# Copyright (c) Digital Creations.  All rights reserved.
# 
# This license has been certified as Open Source(tm).
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
# 
# 1. Redistributions in source code must retain the above copyright
#    notice, this list of conditions, and the following disclaimer.
# 
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions, and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
# 
# 3. Digital Creations requests that attribution be given to Zope
#    in any manner possible. Zope includes a "Powered by Zope"
#    button that is installed by default. While it is not a license
#    violation to remove this button, it is requested that the
#    attribution remain. A significant investment has been put
#    into Zope, and this effort will continue if the Zope community
#    continues to grow. This is one way to assure that growth.
# 
# 4. All advertising materials and documentation mentioning
#    features derived from or use of this software must display
#    the following acknowledgement:
# 
#      "This product includes software developed by Digital Creations
#      for use in the Z Object Publishing Environment
#      (http://www.zope.org/)."
# 
#    In the event that the product being advertised includes an
#    intact Zope distribution (with copyright and license included)
#    then this clause is waived.
# 
# 5. Names associated with Zope or Digital Creations must not be used to
#    endorse or promote products derived from this software without
#    prior written permission from Digital Creations.
# 
# 6. Modified redistributions of any form whatsoever must retain
#    the following acknowledgment:
# 
#      "This product includes software developed by Digital Creations
#      for use in the Z Object Publishing Environment
#      (http://www.zope.org/)."
# 
#    Intact (re-)distributions of any official Zope release do not
#    require an external acknowledgement.
# 
# 7. Modifications are encouraged but must be packaged separately as
#    patches to official Zope releases.  Distributions that do not
#    clearly separate the patches from the original work must be clearly
#    labeled as unofficial distributions.  Modifications which do not
#    carry the name Zope may be packaged in any form, as long as they
#    conform to all of the clauses above.
# 
# 
# Disclaimer
# 
#   THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
#   EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
#   PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
#   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
#   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
#   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
#   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
#   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
#   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
#   SUCH DAMAGE.
# 
# 
# This software consists of contributions made by Digital Creations and
# many individuals on behalf of Digital Creations.  Specific
# attributions are listed in the accompanying credits file.
# 
##############################################################################
"""(simple) Presentation component management
"""

from types import TupleType

import Errors

_marker = []  # Create a new marker object.

class GlobalPresentationRegistry:
    
    def __init__(self):
        self._pres = {}

    def providePresentation(self, input, name, p):
        '''
        Registers a presentation component.
        '''
        self._pres[(input, name)] = p

    def _getPresentation(self, input, name):
        '''
        Finds a registered presentation given an interface and a name.
        '''
        p = self._pres.get((input, name), None)
        if p is not None:
            return p
        bases = getattr(input, '__bases__', ())
        if bases:
            for base in bases:
                p = self._getPresentation(base, name)
                if p is not None:
                    return p
        return None

    def _getPresentationForInterfaces(self, inputs, name):
        '''
        Finds a registered presentaion given a hierarchy of input interfaces
        and a presentation name.
        '''
        if type(inputs) is TupleType:
            for input in inputs:
                p = self._getPresentationForInterfaces(input, name)
                if p is not None:
                    return p
        else:
            # inputs is an interface object.
            return self._getPresentation(inputs, name)

    def getPresentation(self, object, name, default=_marker):
        '''
        Finds a presentation for an object by examining what it implements.
        '''
        inputs = getattr(object, '__implements__', _marker)
        if inputs is not _marker:
            p = self._getPresentationForInterfaces(inputs, name)
        else:
            # No input interfaces known.
            p = self._getPresentation(None, name)

        if p is None:
            if default is not _marker:
                return default
            else:
                raise Errors.PresentationNotFound(object, name)
        return p(object)


--- Updated File Adapter.py in package Zope2 --
--- Adapter.py	2001/06/28 21:46:58	1.1.2.4
+++ Adapter.py	2001/06/29 16:12:19	1.1.2.5
@@ -94,8 +94,12 @@
 
 """
 
+from types import TupleType
+
 import Errors
 
+_marker = []  # Create a new marker object.
+
 class GlobalAdapterRegistry:
     
     def __init__(self):
@@ -137,23 +141,26 @@
         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
+        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, TupleType=type(())):
+    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)
+                a = self._getAdapterForInterfaces(input, output)
                 if a is not None:
                     return a
         else:
+            # inputs is an interface object.
             return self._getAdapter(inputs, output)
 
     def getAdapter(self, object, output, default=_marker):
@@ -162,14 +169,17 @@
         '''
         if output.isImplementedBy(object): return object
 
-        a = getattr(object, '__implements__', _marker)
-        if a is not _marker:
-            a = self.getAdapterForInterfaces(a, output)
+        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)
 
-        if a is _marker:
-            raise Errors.AdapterNotFound(object, output)
+        if a is None:
+            if default is not _marker:
+                return default
+            else:
+                raise Errors.AdapterNotFound(object, output)
         return a(object)
-
 

--- Updated File Errors.py in package Zope2 --
--- Errors.py	2001/06/28 16:03:16	1.1.2.1
+++ Errors.py	2001/06/29 16:12:19	1.1.2.2
@@ -84,6 +84,27 @@
 ##############################################################################
 ''' '''
 
-class AdapterNotFound(Exception):
+class AdapterNotFound (Exception):
     """Could not find the necessary adapter for a component"""
+
+    def __init__(self, object, output):
+        self.object = object
+        self.output = output
+
+    def __str__(self):
+        return 'Object: %s, output interface: %s' % (`self.object`,
+                                                     `self.output`)
+
+
+class PresentationNotFound (Exception):
+    """Could not find the necessary presentation for a component"""
+
+    def __init__(self, object, name):
+        self.object = object
+        self.name = name
+
+    def __str__(self):
+        return 'Object: %s, name: %s' % (`self.object`,
+                                         `self.name`)
+
 

--- Updated File __init__.py in package Zope2 --
--- __init__.py	2001/06/28 16:03:41	1.1.2.2
+++ __init__.py	2001/06/29 16:12:19	1.1.2.3
@@ -82,10 +82,25 @@
 # attributions are listed in the accompanying credits file.
 # 
 ##############################################################################
-''' '''
+'''
+$Id$
+'''
 
-import Adapter; f=Adapter.GlobalAdapterFactory(); del Adapter
-getAdapter=f.getAdapter
-provideAdapter=f.provideAdapter
-del f
+import Adapter
+r = Adapter.GlobalAdapterRegistry()
+
+getAdapter = r.getAdapter
+provideAdapter = r.provideAdapter
+
+import Presentation
+r = Adapter.GlobalPresentationRegistry()
+
+getPresentation = r.getPresentation
+providePresentation = r.providePresentation
+
+
+__all__ = (
+    'getAdapter', 'provideAdapter',
+    'getPresentation', 'providePresentation',
+    )