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

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

lots of minor fixes. including removing username from process configs.

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