environment.py
77 lines
| 3.0 KiB
| text/x-python
|
PythonLexer
|
r0 | """Pylons environment configuration""" | ||
from mako.lookup import TemplateLookup | ||||
r43 | from pylons.configuration import PylonsConfig | |||
|
r0 | from pylons.error import handle_mako_error | ||
r239 | from pylons_app.config.routing import make_map | |||
r309 | from pylons_app.lib.auth import set_available_permissions, set_base_path | |||
r341 | from pylons_app.lib.utils import repo2db_mapper, make_ui, set_hg_app_config | |||
r239 | from pylons_app.model import init_model | |||
r265 | from pylons_app.model.hg_model import _get_repos_cached_initial | |||
r43 | from sqlalchemy import engine_from_config | |||
r239 | import logging | |||
import os | ||||
|
r0 | import pylons_app.lib.app_globals as app_globals | ||
import pylons_app.lib.helpers | ||||
r239 | ||||
|
r0 | log = logging.getLogger(__name__) | ||
r365 | def load_environment(global_conf, app_conf, initial=False): | |||
|
r0 | """Configure the Pylons environment via the ``pylons.config`` | ||
object | ||||
""" | ||||
r43 | config = PylonsConfig() | |||
|
r0 | # Pylons paths | ||
root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | ||||
|
r32 | paths = dict(root=root, | ||
controllers=os.path.join(root, 'controllers'), | ||||
static_files=os.path.join(root, 'public'), | ||||
templates=[os.path.join(root, 'templates')]) | ||||
|
r0 | |||
# Initialize config with the basic options | ||||
r43 | config.init_app(global_conf, app_conf, package='pylons_app', paths=paths) | |||
|
r0 | |||
r43 | config['routes.map'] = make_map(config) | |||
config['pylons.app_globals'] = app_globals.Globals(config) | ||||
|
r0 | config['pylons.h'] = pylons_app.lib.helpers | ||
r43 | ||||
# Setup cache object as early as possible | ||||
import pylons | ||||
pylons.cache._push_object(config['pylons.app_globals'].cache) | ||||
|
r0 | # Create the Mako TemplateLookup, with the default auto-escaping | ||
|
r41 | config['pylons.app_globals'].mako_lookup = TemplateLookup( | ||
|
r32 | directories=paths['templates'], | ||
error_handler=handle_mako_error, | ||||
module_directory=os.path.join(app_conf['cache_dir'], 'templates'), | ||||
|
r41 | input_encoding='utf-8', default_filters=['escape'], | ||
imports=['from webhelpers.html import escape']) | ||||
|
r0 | |||
r252 | #sets the c attribute access when don't existing attribute are accessed | |||
r107 | config['pylons.strict_tmpl_context'] = True | |||
r473 | test = os.path.split(config['__file__'])[-1] == 'test.ini' | |||
if test: | ||||
from pylons_app.lib.utils import make_test_env | ||||
make_test_env() | ||||
r43 | #MULTIPLE DB configs | |||
# Setup the SQLAlchemy database engine | ||||
r469 | if config['debug'] and not test: | |||
|
r49 | #use query time debugging. | ||
from pylons_app.lib.timerproxy import TimerProxy | ||||
sa_engine_db1 = engine_from_config(config, 'sqlalchemy.db1.', | ||||
proxy=TimerProxy()) | ||||
else: | ||||
sa_engine_db1 = engine_from_config(config, 'sqlalchemy.db1.') | ||||
r43 | ||||
|
r49 | init_model(sa_engine_db1) | ||
r388 | #init baseui | |||
r341 | config['pylons.app_globals'].baseui = make_ui('db') | |||
r365 | repo2db_mapper(_get_repos_cached_initial(config['pylons.app_globals'], initial)) | |||
r239 | set_available_permissions(config) | |||
r309 | set_base_path(config) | |||
r341 | set_hg_app_config(config) | |||
|
r0 | # CONFIGURATION OPTIONS HERE (note: all config options will override | ||
# any Pylons config options) | ||||
r43 | ||||
return config | ||||