~chris-atlee/blobs/main

« back to all changes in this revision

Viewing changes to blobs/wsgiapp.py

  • Committer: Chris AtLee
  • Date: 2008-02-19 16:55:03 UTC
  • Revision ID: chris@atlee.ca-20080219165503-0i5wcwtx04zphrhd
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""The blobs WSGI application"""
 
2
import os
 
3
 
 
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, \
 
11
    StaticJavascripts
 
12
from pylons.wsgiapp import PylonsApp
 
13
from sqlalchemy import engine_from_config
 
14
 
 
15
import blobs.helpers
 
16
from blobs.routing import make_map
 
17
import blobs.model as model
 
18
 
 
19
def load_environment(global_conf, app_conf):
 
20
    """Configure the Pylons environment via the ``pylons.config``
 
21
    object
 
22
    """
 
23
    # Pylons paths
 
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')])
 
29
 
 
30
    # Initialize config with the basic options
 
31
    config.init_app(global_conf, app_conf, package='blobs',
 
32
                    template_engine='mako', paths=paths)
 
33
 
 
34
    config['routes.map'] = make_map()
 
35
    config['pylons.g'] = Globals()
 
36
    config['pylons.h'] = blobs.helpers
 
37
 
 
38
    # Customize templating options via this variable
 
39
    tmpl_options = config['buffet.template_options']
 
40
 
 
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
 
45
 
 
46
 
 
47
def make_app(global_conf, full_stack=True, **app_conf):
 
48
    """Create a Pylons WSGI application and return it
 
49
 
 
50
    ``global_conf``
 
51
        The inherited configuration for this application. Normally from
 
52
        the [DEFAULT] section of the Paste ini file.
 
53
 
 
54
    ``full_stack``
 
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
 
58
        WSGI middleware.
 
59
 
 
60
    ``app_conf``
 
61
        The application's local configuration. Normally specified in the
 
62
        [app:<name>] section of the Paste ini file (where <name>
 
63
        defaults to main).
 
64
    """
 
65
    # Configure the Pylons environment
 
66
    load_environment(global_conf, app_conf)
 
67
 
 
68
    # The Pylons WSGI app
 
69
    app = PylonsApp()
 
70
 
 
71
    # CUSTOM MIDDLEWARE HERE (filtered by error handling middlewares)
 
72
 
 
73
    if asbool(full_stack):
 
74
        # Handle Python exceptions
 
75
        app = ErrorHandler(app, global_conf, error_template=error_template,
 
76
                           **config['pylons.errorware'])
 
77
 
 
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)
 
81
 
 
82
    # Establish the Registry for this application
 
83
    app = RegistryManager(app)
 
84
 
 
85
    # Static files
 
86
    javascripts_app = StaticJavascripts()
 
87
    static_app = StaticURLParser(config['pylons.paths']['static_files'])
 
88
    app = Cascade([static_app, javascripts_app, app])
 
89
    return app
 
90
 
 
91
 
 
92
class Globals(object):
 
93
    """Globals acts as a container for objects available throughout the
 
94
    life of the application
 
95
    """
 
96
 
 
97
    def __init__(self):
 
98
        """One instance of Globals is created during application
 
99
        initialization and is available during requests via the 'g'
 
100
        variable.
 
101
        """
 
102
        pass