PageRenderTime 102ms CodeModel.GetById 60ms app.highlight 7ms RepoModel.GetById 34ms app.codeStats 0ms

/kai/config/environment.py

https://bitbucket.org/bbangert/kai/
Python | 57 lines | 38 code | 11 blank | 8 comment | 0 complexity | c24af2a0891cbd0737e2524373a25ddd MD5 | raw file
 1"""Pylons environment configuration"""
 2import os
 3from datetime import timedelta
 4
 5from couchdb import Server, Database
 6from mako.lookup import TemplateLookup
 7from paste.deploy.converters import asbool
 8from pylons.configuration import PylonsConfig
 9from pylons.error import handle_mako_error
10import webhelpers.pylonslib.secure_form as secure_form
11
12import kai.lib.app_globals as app_globals
13import kai.lib.helpers
14from kai.config.routing import make_map
15
16secure_form.token_key = 'authentication_token'
17
18def load_environment(global_conf, app_conf):
19    """Configure the Pylons environment via the ``pylons.config``
20    object
21    """
22    config = PylonsConfig()
23    
24    # Pylons paths
25    root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
26    paths = dict(root=root,
27                 controllers=os.path.join(root, 'controllers'),
28                 static_files=os.path.join(root, 'public'),
29                 templates=[os.path.join(root, 'templates')])
30
31    # Initialize config with the basic options
32    config.init_app(global_conf, app_conf, package='kai', paths=paths)
33
34    config['pylons.app_globals'] = app_globals.Globals(config)
35    config['routes.map'] = make_map(config)
36    config['pylons.h'] = kai.lib.helpers
37    config['pylons.strict_tmpl_context'] = False
38    config['beaker.session.cookie_expires'] = timedelta(seconds=604800)
39
40    # Create the Mako TemplateLookup, with the default auto-escaping
41    config['pylons.app_globals'].mako_lookup = TemplateLookup(
42        directories=paths['templates'],
43        error_handler=handle_mako_error,
44        module_directory=os.path.join(app_conf['cache_dir'], 'templates'),
45        input_encoding='utf-8', output_encoding='utf-8',
46        imports=['from webhelpers.html import escape'],
47        filesystem_checks=asbool(config['debug']),
48        default_filters=['escape'])
49    
50    import pylons
51    pylons.cache._push_object(config['pylons.app_globals'].cache)
52    
53    # Setup the database connection
54    config['kai.server'] = Server(config['couchdb_server'])
55    config['kai.db'] = Database(config['couchdb_uri'])
56    
57    return config