Skip to content

Instantly share code, notes, and snippets.

@shazow
Created February 28, 2011 17:26
Show Gist options
  • Save shazow/847665 to your computer and use it in GitHub Desktop.
Save shazow/847665 to your computer and use it in GitHub Desktop.
Pylons environment setup outside of paste. Useful for background tasks (like using turnip)
def setup(config_file='development.ini'):
from socialgrapple.config.environment import load_environment
from paste.deploy import loadapp
from routes.util import URLGenerator
from webtest import TestApp
from beaker.session import SessionObject
environ = {'HTTP_HOST': 'socialgrapple.com'}
wsgiapp = loadapp('config:' + config_file, relative_to='.')
config = load_environment(wsgiapp.config['global_conf'], wsgiapp.config['app_conf'])
app = TestApp(wsgiapp, environ)
resp = app.get('/')
# Setup StackedProxyObjects
import pylons
pylons.url._push_object(URLGenerator(config['routes.map'], environ))
pylons.app_globals._push_object(config['pylons.app_globals'])
pylons.tmpl_context._push_object(pylons.util.ContextObj())
pylons.translator._push_object(pylons.i18n.translation._get_translator(pylons.config.get('lang')))
pylons.session._push_object(SessionObject(environ))
pylons.request._push_object(resp.request)
pylons.response._push_object(resp)
pylons.config.update(config)
# Setup logging
import logging.config
logging.config.fileConfig(config_file)
return app
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment