Latest Version: 0.9.6.2
/Users/bbangert/Programming/Python/Paste/paste/cascade.py
0001# (c) 2005 Ian Bicking and contributors; written for Paste (http://pythonpaste.org)
0002# Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
0003
0004"""
0005Cascades through several applications, so long as applications
0006return ``404 Not Found``.
0007"""
0008from paste import httpexceptions
0009from paste.util import converters
0010
0011__all__ = ['Cascade']
0012
0013def make_cascade(loader, global_conf, catch='404', **local_conf):
0014    """
0015    Expects configuration like:
0016
0017    [composit:cascade]
0018    use = egg:Paste#cascade
0019    # all start with 'app' and are sorted alphabetically
0020    app1 = foo
0021    app2 = bar
0022    ...
0023    catch = 404 500 ...
0024    """
0025    catch = map(int, converters.aslist(catch))
0026    apps = []
0027    for name, value in local_conf.items():
0028        if not name.startswith('app'):
0029            raise ValueError(
0030                "Bad configuration key %r (=%r); all configuration keys "
0031                "must start with 'app'"
0032                % (name, value))
0033        app = loader.get_app(value, global_conf=global_conf)
0034        apps.append((name, app))
0035    apps.sort()
0036    apps = [app for name, app in apps]
0037    return Cascade(apps, catch=catch)
0038
0039class Cascade(object):
0040
0041    """
0042    Passed a list of applications, ``Cascade`` will try each of them
0043    in turn.  If one returns a status code listed in ``catch`` (by
0044    default just ``404 Not Found``) then the next application is
0045    tried.
0046
0047    If all applications fail, then the last application's failure
0048    response is used.
0049    """
0050
0051    def __init__(self, applications, catch=(404,)):
0052        self.apps = applications
0053        self.catch_codes = {}
0054        self.catch_exceptions = []
0055        for error in catch:
0056            if isinstance(error, str):
0057                error = int(error.split(None, 1)[0])
0058            if isinstance(error, httpexceptions.HTTPException):
0059                exc = error
0060                code = error.code
0061            else:
0062                exc = httpexceptions.get_exception(error)
0063                code = error
0064            self.catch_codes[code] = exc
0065            self.catch_exceptions.append(exc)
0066        self.catch_exceptions = tuple(self.catch_exceptions)
0067
0068    def __call__(self, environ, start_response):
0069        failed = []
0070        def repl_start_response(status, headers, exc_info=None):
0071            code = int(status.split(None, 1)[0])
0072            if code in self.catch_codes:
0073                failed.append(None)
0074                return _consuming_writer
0075            return start_response(status, headers, exc_info)
0076
0077        for app in self.apps[:-1]:
0078            environ_copy = environ.copy()
0079            failed = []
0080            try:
0081                v = app(environ_copy, repl_start_response)
0082                if not failed:
0083                    return v
0084                else:
0085                    if hasattr(v, 'close'):
0086                        # Exhaust the iterator first:
0087                        list(v)
0088                        # then close:
0089                        v.close()
0090            except self.catch_exceptions, e:
0091                pass
0092        return self.apps[-1](environ, start_response)
0093
0094def _consuming_writer(s):
0095    pass

Top