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

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

added more work on getting async jobs running

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
11from pylons import config
12from pylons.error import error_template
13from pylons.middleware import error_mapper, ErrorDocuments, ErrorHandler, \
14    StaticJavascripts
15from pylons.wsgiapp import PylonsApp
16
17import cows_wps.helpers
18from cows_wps.routing import make_map
19
20from cows_wps.utils.setenv import setenv_wps
21
22def load_environment(global_conf, app_conf, set_env = True):
23    """
24    Configure the Pylons environment via the ``pylons.config`` object.
25    """
26    # Pylons paths
27    root = os.path.dirname(os.path.abspath(__file__))
28    paths = dict(root=root,
29                 controllers=os.path.join(root, 'controllers'),
30                 static_files=os.path.join(root, 'public'),
31                 templates=[os.path.join(root, 'templates')])
32
33    # Initialize config with the basic options
34    config.init_app(global_conf, app_conf, package='cows_wps',
35                    template_engine='mako', paths=paths)
36
37    config['routes.map'] = make_map()
38    config['pylons.g'] = Globals()
39    config['pylons.h'] = cows_wps.helpers
40
41    # Customize templating options via this variable
42    tmpl_options = config['buffet.template_options']
43
44    # CONFIGURATION OPTIONS HERE (note: all config options will override
45    # any Pylons config options)
46    if set_env == True:
47        setenv_wps()
48
49
50def make_app(global_conf, full_stack=True, **app_conf):
51    """Create a Pylons WSGI application and return it
52
53    ``global_conf``
54        The inherited configuration for this application. Normally from
55        the [DEFAULT] section of the Paste ini file.
56
57    ``full_stack``
58        Whether or not this application provides a full WSGI stack (by
59        default, meaning it handles its own exceptions and errors).
60        Disable full_stack when this application is "managed" by another
61        WSGI middleware.
62
63    ``app_conf``
64        The application's local configuration. Normally specified in the
65        [app:<name>] section of the Paste ini file (where <name>
66        defaults to main).
67    """
68    # Configure the Pylons environment
69    load_environment(global_conf, app_conf)
70
71    # The Pylons WSGI app
72    app = PylonsApp()
73
74    # CUSTOM MIDDLEWARE HERE (filtered by error handling middlewares)
75    app = RoutesMiddleware(app, config['routes.map'])
76    app = SessionMiddleware(app, config)
77    app = CacheMiddleware(app, config)
78
79
80    # Import managers here to triger instantiation in main thread.
81    #!TODO:  Should probably be instantiated here too and add to globals.
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.