1 | """Pylons environment configuration""" |
---|
2 | import os |
---|
3 | |
---|
4 | from mako.lookup import TemplateLookup |
---|
5 | from pylons.configuration import PylonsConfig |
---|
6 | from pylons.error import handle_mako_error |
---|
7 | |
---|
8 | import pylonsapp.lib.app_globals as app_globals |
---|
9 | import pylonsapp.lib.helpers |
---|
10 | from pylonsapp.config.routing import make_map |
---|
11 | |
---|
12 | def load_environment(global_conf, app_conf): |
---|
13 | """Configure the Pylons environment via the ``pylons.config`` |
---|
14 | object |
---|
15 | """ |
---|
16 | config = PylonsConfig() |
---|
17 | |
---|
18 | # Pylons paths |
---|
19 | root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
---|
20 | paths = dict(root=root, |
---|
21 | controllers=os.path.join(root, 'controllers'), |
---|
22 | static_files=os.path.join(root, 'public'), |
---|
23 | templates=[os.path.join(root, 'templates')]) |
---|
24 | |
---|
25 | # Initialize config with the basic options |
---|
26 | config.init_app(global_conf, app_conf, package='pylonsapp', paths=paths) |
---|
27 | |
---|
28 | config['routes.map'] = make_map(config) |
---|
29 | config['pylons.app_globals'] = app_globals.Globals(config) |
---|
30 | config['pylons.h'] = pylonsapp.lib.helpers |
---|
31 | |
---|
32 | # Setup cache object as early as possible |
---|
33 | import pylons |
---|
34 | pylons.cache._push_object(config['pylons.app_globals'].cache) |
---|
35 | |
---|
36 | |
---|
37 | # Create the Mako TemplateLookup, with the default auto-escaping |
---|
38 | config['pylons.app_globals'].mako_lookup = TemplateLookup( |
---|
39 | directories=paths['templates'], |
---|
40 | error_handler=handle_mako_error, |
---|
41 | module_directory=os.path.join(app_conf['cache_dir'], 'templates'), |
---|
42 | input_encoding='utf-8', default_filters=['escape'], |
---|
43 | imports=['from webhelpers.html import escape']) |
---|
44 | |
---|
45 | # CONFIGURATION OPTIONS HERE (note: all config options will override |
---|
46 | # any Pylons config options) |
---|
47 | |
---|
48 | return config |
---|