source: cows/trunk/cows/test/testapp/wsgiapp.py @ 4613

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/cows/trunk/cows/test/testapp/wsgiapp.py@4613
Revision 4613, 3.3 KB checked in by spascoe, 12 years ago (diff)

Improved testing for COWS.

To effectively test COWS you need a Pylons application. I've created a
mini pylons app in cows/test/testapp that is automatically wired into
the Pylons testing framework when you run "nosetests cows". See
cows/test/README.

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