Previously Mike Orr wrote:
> On Fri, Jan 23, 2009 at 12:31 AM, cropr <ruben.dec...@gmail.com> wrote:
> > I want to add some administrative console tools next to my 0.9.7
> > Pylons application (e.g. a backup/restore).  I want to make use of
> > some parameter settings in the development.ini or production.ini,
> > but I don't need any wsgi stuff.  What is the best and easiest way to
> > start up the console application
> 
> The easiest way is to make a package for your scripts and run them
> with "python -m"; e.g.,
> 
>     python -m myapp.scripts.maintain   development.ini
> 
> .  A more difficult way is to create paster plugins, but these will
> only be available if the current directory is the application.

I'm not sure it is more difficult. Here is an example :). Lets start
by creating a base class which does the basic Pylons setup work so
we can use Pylons tools in our commandline script:


from paste.deploy import loadapp, appconfig
from paste.script.command import Command

class PylonsCommand(Command):
    group_name = "2style4you"
    min_args=1
    max_args=1
    
    def _SetupPylons(self):
        """Perform some magic setup work."""
        config_name = "config:%s" % self.args[0]
        self.logging_file_config(self.args[0])
        cwd=os.getcwd()
        conf=appconfig(config_name, relative_to=cwd)
        conf.update(dict(app_conf=conf.local_conf, 
global_conf=conf.global_conf))
        self.wsgiapp=loadapp(config_name, relative_to=cwd)


We can now write the code for a simple command:

class MyExampleCommand(PylonsCommand):
    summary="Summary shown in paster command listing"
    usage="\nExtra help text shown by paster help <command>"

    parser=PylonsCommand.standard_parser()

    def command(self):
        self._SetupPylons()
        session=meta.Session()
        # Do your work here
        session.commit()


If you need to handle commandline arguments you register those with the parser
class variable:


class MyExampleCommand(PylonsCommand):
    summary="Summary shown in paster command listing"
    usage="\nExtra help text shown by paster help <command>"

    parser=PylonsCommand.standard_parser()
    parser.add_option("-u", "--url", dest="url", default=None,
            help="Use a different URL for our data")


The result is available via self.options.

To make your command available create an entry point so paster picks
it up. In your setup.py do something like this:

    entry_points="""
    [paste.paster_command]
    controller = pylons.commands:ControllerCommand
    restcontroller = pylons.commands:RestControllerCommand

    mycommand = myapp.commands:MyExampleCommand
    """


You can now call it like this:

   paster mycommand development.ini


Wichert.

-- 
Wichert Akkerman <wich...@wiggy.net>    It is simple to make things.
http://www.wiggy.net/                   It is hard to make things simple.

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

Reply via email to