source: cows_wps/trunk/cows_wps/wsgiapp.py @ 6949

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/cows_wps/trunk/cows_wps/wsgiapp.py@6949
Revision 6949, 3.5 KB checked in by astephen, 10 years ago (diff)

various fixes to get user picked up and cookies working.

Line 
1"""The cows_wps WSGI application"""
2import os
3
4from beaker.middleware import CacheMiddleware, SessionMiddleware
5from routes.middleware import RoutesMiddleware
6
7from paste.cascade import Cascade
8from paste.registry import RegistryManager
9from paste.urlparser import StaticURLParser
10from paste.deploy.converters import asbool
11
12from pylons import config
13from pylons.error import error_template
14from pylons.middleware import error_mapper, ErrorDocuments, ErrorHandler, \
15    StaticJavascripts
16from pylons.wsgiapp import PylonsApp
17
18import cows_wps.helpers
19from cows_wps.routing import make_map
20
21from cows_wps.utils.setenv import setenv_wps
22
23def load_environment(global_conf, app_conf, set_env = True):
24    """
25    Configure the Pylons environment via the ``pylons.config`` object.
26    """
27    # Pylons paths
28    root = os.path.dirname(os.path.abspath(__file__))
29    paths = dict(root=root,
30                 controllers=os.path.join(root, 'controllers'),
31                 static_files=os.path.join(root, 'public'),
32                 templates=[os.path.join(root, 'templates')])
33
34    # Initialize config with the basic options
35    config.init_app(global_conf, app_conf, package='cows_wps',
36                    template_engine='mako', paths=paths)
37
38    config['routes.map'] = make_map()
39    config['pylons.g'] = Globals()
40    config['pylons.h'] = cows_wps.helpers
41
42    # Customize templating options via this variable
43    tmpl_options = config['buffet.template_options']
44
45    # CONFIGURATION OPTIONS HERE (note: all config options will override
46    # any Pylons config options)
47    if set_env == True:
48        setenv_wps()
49
50
51def make_app(global_conf, full_stack = True, **app_conf):
52    """Create a Pylons WSGI application and return it
53
54    ``global_conf``
55        The inherited configuration for this application. Normally from
56        the [DEFAULT] section of the Paste ini file.
57
58    ``full_stack``
59        Whether or not this application provides a full WSGI stack (by
60        default, meaning it handles its own exceptions and errors).
61        Disable full_stack when this application is "managed" by another
62        WSGI middleware.
63
64    ``app_conf``
65        The application's local configuration. Normally specified in the
66        [app:<name>] section of the Paste ini file (where <name>
67        defaults to main).
68    """
69    # Configure the Pylons environment
70    load_environment(global_conf, app_conf)
71
72    # The Pylons WSGI app
73    app = PylonsApp()
74
75    # CUSTOM MIDDLEWARE HERE (filtered by error handling middlewares)
76    app = RoutesMiddleware(app, config['routes.map'])
77    app = SessionMiddleware(app, config)
78    app = CacheMiddleware(app, config)
79
80
81    # Import managers here to trigger instantiation in main thread.
82    import cows_wps.model.managers
83
84    if asbool(full_stack):
85        # Display error documents for 401, 403, 404 status codes (and
86        # 500 when debug is disabled)
87        app = ErrorDocuments(app, global_conf, mapper=error_mapper, **app_conf)
88
89    # Establish the Registry for this application
90    app = RegistryManager(app)
91
92    # Static files
93    javascripts_app = StaticJavascripts()
94    static_app = StaticURLParser(config['pylons.paths']['static_files'])
95    app = Cascade([static_app, javascripts_app, app])
96    return app
97
98
99
100
101
102class Globals(object):
103    """Globals acts as a container for objects available throughout the
104    life of the application
105    """
106
107    def __init__(self):
108        """One instance of Globals is created during application
109        initialization and is available during requests via the 'g'
110        variable.
111        """
112        pass
Note: See TracBrowser for help on using the repository browser.