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