writing external commands that use the same data model as a Pylons app

1 view
Skip to first unread message

Jamie Wilkinson

unread,
Apr 22, 2006, 1:08:28 AM4/22/06
to pylons-discuss
I'd like to be able to write some commandline tools that use the same data
model as the Pylons app I'm developing; however one can't just import the
model because it depends on some Pylons infrastructure, namely PackageHub,
and thus also the config code from PasteScript. It seems that the easiest
way to do this then is to write some additional commands for PasteScript,
such that the commandline tool gets called like:

paster somecommand

Can anyone recommend some examples on how to do this? Having read the
PasteScript docs I'm still no closer to understanding how it fits together,
some examples would be a real help.

Alternatively, is there a better way to write a command line tool?

(Some use cases that spring to mind: a mail gateway a la Request Tracker's
inbound mail interface, or a cron job that, say, sends out invoices or
invoice reminders once a day.)

Philip Jenvey

unread,
Apr 22, 2006, 3:30:25 PM4/22/06
to pylons-...@googlegroups.com

The pylons/models/__init__.py example simply sets up sqlobject's
magic module level '__connection__' variable (this can be a string or
a sqlobject ConnectionHub), then imports your models from other
modules. If you followed this example, an external application could
do something similar: setup the '__connection__' var and import the
models.

If you decide to use paster, the pylons custom 'paster controller'
command can be used as an example, which is here:

http://pylonshq.com/project/pylonshq/browser/Pylons/trunk/pylons/
commands.py

Which is the better solution depends on your situation; but I would
lean towards using paster. In the future, you may want/need other
options from your config file for these command line services -- then
your external application has to reinvent the "how do we configure
our application?" wheel.

--
Philip Jenvey

Jamie Wilkinson

unread,
Apr 23, 2006, 8:45:37 PM4/23/06
to pylons-...@googlegroups.com

Yeah, I know that. Do you think then that loading, say, production.ini with
a ConfigParser and looking for a either %(package)s.dburi or sqlobject.dburi
is preferable to letting the pastescript infrastructure take care of that?

Specifically, I don't want to reinvent the wheel -- there is already code
that loads the configuration, and my model is already defined, but one
cannot just import the model modules without knowing that the connection
string is.

>If you decide to use paster, the pylons custom 'paster controller'
>command can be used as an example, which is here:
>
>http://pylonshq.com/project/pylonshq/browser/Pylons/trunk/pylons/
>commands.py
>
>Which is the better solution depends on your situation; but I would
>lean towards using paster. In the future, you may want/need other
>options from your config file for these command line services -- then
>your external application has to reinvent the "how do we configure
>our application?" wheel.

Exactly.

How does paster find the commands, then? Forgive me if this question has an
obvious answer :)

Philip Jenvey

unread,
Apr 24, 2006, 1:05:06 PM4/24/06
to pylons-...@googlegroups.com

Sounds like we're on the same page (a paste script is preferable).

>
>> If you decide to use paster, the pylons custom 'paster controller'
>> command can be used as an example, which is here:
>>
>> http://pylonshq.com/project/pylonshq/browser/Pylons/trunk/pylons/
>> commands.py
>>
>> Which is the better solution depends on your situation; but I would
>> lean towards using paster. In the future, you may want/need other
>> options from your config file for these command line services -- then
>> your external application has to reinvent the "how do we configure
>> our application?" wheel.
>
> Exactly.
>
> How does paster find the commands, then? Forgive me if this
> question has an
> obvious answer :)
>

I forgot to mention: for paste to actually recognize the command,
you'll need to add an egg entry point to your setup.py. That's
covered in the paste docs, and pylons' setup.py has an example.

http://pythonpaste.org/script/developer.html
http://pylonshq.com/project/pylonshq/browser/Pylons/trunk/setup.py#L72

--
Philip Jenvey

Reply all
Reply to author
Forward
0 new messages