PageRenderTime 346ms CodeModel.GetById 140ms app.highlight 5ms RepoModel.GetById 131ms app.codeStats 0ms

/kai/config/middleware.py

https://bitbucket.org/bbangert/kai/
Python | 73 lines | 58 code | 4 blank | 11 comment | 0 complexity | 458d4f31aa81e6c47d9e0985973f743b MD5 | raw file
 1"""Pylons middleware initialization"""
 2import time
 3import _strptime
 4
 5from beaker.middleware import CacheMiddleware, SessionMiddleware
 6from paste.cascade import Cascade
 7from paste.registry import RegistryManager
 8from paste.urlparser import StaticURLParser
 9from paste.deploy.converters import asbool
10from pylons.middleware import ErrorHandler, StatusCodeRedirect
11from pylons.wsgiapp import PylonsApp
12from routes.middleware import RoutesMiddleware
13from tw2.core.middleware import TwMiddleware
14
15from kai.config.environment import load_environment
16
17# Import the docutil extension promptly to ensure the code-block is regged
18import kai.lib.pygmentsupport
19
20def make_app(global_conf, full_stack=True, **app_conf):
21    """Create a Pylons WSGI application and return it
22
23    ``global_conf``
24        The inherited configuration for this application. Normally from
25        the [DEFAULT] section of the Paste ini file.
26
27    ``full_stack``
28        Whether or not this application provides a full WSGI stack (by
29        default, meaning it handles its own exceptions and errors).
30        Disable full_stack when this application is "managed" by
31        another WSGI middleware.
32
33    ``app_conf``
34        The application's local configuration. Normally specified in
35        the [app:<name>] section of the Paste ini file (where <name>
36        defaults to main).
37
38    """
39    # Configure the Pylons environment
40    config = load_environment(global_conf, app_conf)
41
42    # The Pylons WSGI app
43    app = PylonsApp(config=config)
44    
45    # CUSTOM MIDDLEWARE HERE (filtered by error handling middlewares)
46    app = TwMiddleware(app, default_engine='mako')
47    
48    # Routing/Session/Cache Middleware
49    app = RoutesMiddleware(app, config['routes.map'])
50    app = SessionMiddleware(app, config)
51    
52    if asbool(full_stack):
53        # Handle Python exceptions
54        app = ErrorHandler(app, global_conf, **config['pylons.errorware'])
55
56        # Display error documents for 401, 403, 404 status codes (and
57        # 500 when debug is disabled)
58        if asbool(config['debug']):
59            app = StatusCodeRedirect(app)
60        else:
61            app = StatusCodeRedirect(app, [400, 401, 403, 404, 500])
62
63    # Establish the Registry for this application
64    app = RegistryManager(app)
65
66    # Static files (If running in production, and Apache or another web 
67    # server is handling this static content, remove the following 3 lines)
68    if asbool(config['debug']):
69        static_app = StaticURLParser(config['pylons.paths']['static_files'])
70        app = Cascade([static_app, app])
71    
72    app.config = config
73    return app