Pylons

Aug 11, 2009 6:31:59 AM

Running Commands with the App environment

On many occasions, one might want to run scripts such as crontabs that have access to models, and/or other parts of the webapp code-base. It's desirable on those occasions to have the app loaded.

Here's how to load the app from the shell, this example includes using optparser to let the INI file to use (for the app settings) be specified on the command line:

import optparse

import pylons
from paste.deploy import appconfig

from YOURAPP.config.environment import load_environment


if __name__ == '__main__':
    option_parser = optparse.OptionParser()
    option_parser.add_option('--ini',
        help='INI file to use for pylons settings',
        type='str',
        default='development.ini')
    options, args = option_parser.parse_args()

    # Initialize the Pylons app
    conf = appconfig('config:' + options.ini, relative_to='.')
    load_environment(conf.global_conf, conf.local_conf)

    # Now code can be run, the SQLalchemy Session can be used, etc.
    ....

Comments (78)

estin
Oct 19, 2009 1:20:49 PM

If use pipeline or composite applications this snippet does`n work for me.

I use loadapp function instead appconfig and load_enviroment (they will be called in by app_factory entry point -> YOURAPP.config.middleware.make_app) but you get all middleware are initialized…

@# Initialize the Pylons app
loadapp(‘config:’ + options.ini, relative_to=’.’)@

P.S. Sorry for my poor english

estin
Oct 20, 2009 2:10:18 PM

for pipeline or composite project you must call:
conf= appconfig(‘config:’ + options.ini, name=’app name in ini file’, relative_to=’.’)

Andrew Miller
5 hours and 42 minutes ago

I wanted to say that it’s nice Applied Arts degree to know that someone else also mentioned Business management school this as I had trouble finding the same online computer degree info elsewhere criminal justice school. This was the first place that told me the answer Education degree.

You must login before you can comment.

Powered by Pylons - Contact Administrators