source: cowsserver/branches/migrate-py26-pylons10/lib/cowsserver/config/middleware.py @ 7345

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/cowsserver/branches/migrate-py26-pylons10/lib/cowsserver/config/middleware.py@7345
Revision 7345, 3.1 KB checked in by spascoe, 9 years ago (diff)

Fixed a bunch of errors exposed by the tests (what few there are).

Several tests are still failing because they are out of date with the codebase.

Line 
1"""Pylons middleware initialization"""
2from paste.cascade import Cascade
3from paste.registry import RegistryManager
4from paste.urlparser import StaticURLParser
5from paste.deploy.converters import asbool
6
7from pylons import config
8from pylons.middleware import ErrorHandler, StatusCodeRedirect
9from pylons.wsgiapp import PylonsApp
10
11from pylons import __version__ as pylons_version
12
13def is_096():
14    parts = pylons_version.split('.')
15    return parts[:3] == ['0', '9', '6']
16       
17
18if not is_096():
19    from beaker.middleware import SessionMiddleware
20    from routes.middleware import RoutesMiddleware
21
22
23from cowsserver.config.environment import load_environment
24
25
26def make_app(global_conf, full_stack=True, **app_conf):
27    """Create a Pylons WSGI application and return it
28
29    ``global_conf``
30        The inherited configuration for this application. Normally from
31        the [DEFAULT] section of the Paste ini file.
32
33    ``full_stack``
34        Whether or not this application provides a full WSGI stack (by
35        default, meaning it handles its own exceptions and errors).
36        Disable full_stack when this application is "managed" by
37        another WSGI middleware.
38
39    ``app_conf``
40        The application's local configuration. Normally specified in the
41        [app:<name>] section of the Paste ini file (where <name>
42        defaults to main).
43    """
44
45    if is_096():
46        return make_app_096(global_conf, fullstack, **app_conf)
47
48    # Configure the Pylons environment
49    config = load_environment(global_conf, app_conf)
50
51    # The Pylons WSGI app
52    app = PylonsApp(config=config)
53
54    # CUSTOM MIDDLEWARE HERE (filtered by error handling middlewares)
55    app = RoutesMiddleware(app, config['routes.map'])
56    app = SessionMiddleware(app, config)
57
58    if asbool(full_stack):
59        # Handle Python exceptions
60        app = ErrorHandler(app, global_conf, **config['pylons.errorware'])
61
62        if asbool(config['debug']):
63            app = StatusCodeRedirect(app)
64        else:
65            app = StatusCodeRedirect(app, [401, 403, 404, 500])
66
67    # Establish the Registry for this application
68    app = RegistryManager(app)
69
70    # Static files
71    static_app = StaticURLParser(config['pylons.paths']['static_files'])
72    app = Cascade([static_app, app])
73    app.config = config
74
75    return app
76
77
78def make_app_096(global_conf, full_stack=True, **app_conf):
79    """Create a Pylons WSGI application and return it
80
81    Legacy app config for Pylons 0.9.6
82    """
83
84    from pylons.middleware import StaticJavascripts
85
86    # Configure the Pylons environment
87    load_environment(global_conf, app_conf)
88
89    # The Pylons WSGI app
90    app = PylonsApp()
91
92    # CUSTOM MIDDLEWARE HERE (filtered by error handling middlewares)
93   
94    if asbool(full_stack):
95        # Handle Python exceptions
96        app = ErrorHandler(app, global_conf, **config['pylons.errorware'])
97
98    # Establish the Registry for this application
99    app = RegistryManager(app)
100
101    # Static files
102    javascripts_app = StaticJavascripts()
103    static_app = StaticURLParser(config['pylons.paths']['static_files'])
104    app = Cascade([static_app, javascripts_app, app])
105    return app
Note: See TracBrowser for help on using the repository browser.