1
"""The blobs WSGI application"""
4
from paste.cascade import Cascade
5
from paste.registry import RegistryManager
6
from paste.urlparser import StaticURLParser
7
from paste.deploy.converters import asbool
8
from pylons import config
9
from pylons.error import error_template
10
from pylons.middleware import error_mapper, ErrorDocuments, ErrorHandler, \
12
from pylons.wsgiapp import PylonsApp
13
from sqlalchemy import engine_from_config
16
from blobs.routing import make_map
17
import blobs.model as model
19
def load_environment(global_conf, app_conf):
20
"""Configure the Pylons environment via the ``pylons.config``
24
root = os.path.dirname(os.path.abspath(__file__))
25
paths = dict(root=root,
26
controllers=os.path.join(root, 'controllers'),
27
static_files=os.path.join(root, 'public'),
28
templates=[os.path.join(root, 'templates')])
30
# Initialize config with the basic options
31
config.init_app(global_conf, app_conf, package='blobs',
32
template_engine='mako', paths=paths)
34
config['routes.map'] = make_map()
35
config['pylons.g'] = Globals()
36
config['pylons.h'] = blobs.helpers
38
# Customize templating options via this variable
39
tmpl_options = config['buffet.template_options']
41
# CONFIGURATION OPTIONS HERE (note: all config options will override
42
# any Pylons config options)
43
config['pylons.g'].sa_engine = engine_from_config(config, 'sqlalchemy.')
44
model.meta.bind = config['pylons.g'].sa_engine
47
def make_app(global_conf, full_stack=True, **app_conf):
48
"""Create a Pylons WSGI application and return it
51
The inherited configuration for this application. Normally from
52
the [DEFAULT] section of the Paste ini file.
55
Whether or not this application provides a full WSGI stack (by
56
default, meaning it handles its own exceptions and errors).
57
Disable full_stack when this application is "managed" by another
61
The application's local configuration. Normally specified in the
62
[app:<name>] section of the Paste ini file (where <name>
65
# Configure the Pylons environment
66
load_environment(global_conf, app_conf)
71
# CUSTOM MIDDLEWARE HERE (filtered by error handling middlewares)
73
if asbool(full_stack):
74
# Handle Python exceptions
75
app = ErrorHandler(app, global_conf, error_template=error_template,
76
**config['pylons.errorware'])
78
# Display error documents for 401, 403, 404 status codes (and
79
# 500 when debug is disabled)
80
app = ErrorDocuments(app, global_conf, mapper=error_mapper, **app_conf)
82
# Establish the Registry for this application
83
app = RegistryManager(app)
86
javascripts_app = StaticJavascripts()
87
static_app = StaticURLParser(config['pylons.paths']['static_files'])
88
app = Cascade([static_app, javascripts_app, app])
92
class Globals(object):
93
"""Globals acts as a container for objects available throughout the
94
life of the application
98
"""One instance of Globals is created during application
99
initialization and is available during requests via the 'g'