On Fri, Feb 26, 2010 at 11:17 AM, DD <[email protected]> wrote:
> Hi,
> I am trying to figure out what would be the best way to write batch (command
> line) tools in pylons.
>
> Basically, what I need is a replica of the configuration that my live site
> uses, but instead of controllers getting the requests, its a command line
> tool. Everything else (globals, caching, database, etc) should be the same.
>
> I looked at the command line utilities section in pylons documentation, but
> I can't seem to figure out how to everything loaded properly. loadapp()
> gives me an instance of the app that can be passed on to a wsgi server, but
> that's not what I was looking for.
>
> Another option will be to just run a paster serve and write a controller,
> but that doesn't seem to be the right way to do things :(.
>
> What's the best approach to do this?

There are two general ways to handle utility routines. One is to make
a controller and use "paster request" to run a request from the
command line. I've never done it but it looks like it instantiates the
app in the process without a server.  See "paster help request" for
the options.

The other way is to make a top-level script, which I keep in
myapp/scripts/ and run with "python -m myapp.scripts.myscript".  But
then it's a bear to set up all the configuration and environment.
loadapp() is the easiest way.

I don't quite understand what you're asking beyond that. If you run
loadapp(), the 'config', model, 'app_globals', and 'render' should all
be ready to use the normal way, just import them. You don't need 'app'
itself, it's just a trick to initialize the environment, but you can
call 'app.get()' or 'app.post()' if you want to.

Initializing the environment without 'loadapp' takes a lot more work.
That's because 'loadapp' uses a bunch of custom code to parse the INI
file (it creates a NicerConfigParser and adds some underscored
variables to do the "%(here)s"-style interpolation, which I've never
been able to get to work on my own).  If you *just* need the model,
you can pass a  DBURL on the command line and call model.init_model()
directly.  That's enough for many model utilities, and avoids them
depending on the rest of the app or Pylons.

-- 
Mike Orr <[email protected]>

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en.

Reply via email to