views:

29

answers:

1

I'm at a point in my Pylons projects where I end up creating and deleting controllers often (probably more often than I should). I grow tired of adding my own imports and tweaks to the top of every controller. There was a recent question about modifying the new controller template that got me part-way to not having to do that - but I don't understand how the controller.py_tmpl file is used by paster, and how I can tell Paster to, for an existing project, "hey, use this template instead!"

What invocation do I need to tell Paster to use my template instead of the default one?

+1  A: 

Pylons creates new controllers and projects by adding command to paste. The commands are defined in setup.py and you can add new commands.

For example (this is taken from the Paste docs) lets assume you have a project called Foo that is in a package also called foo.

In setup.py add 'foo' to the 'paster_plugins' list Then add a new command to entry_points.

ie entry_points=""" [paste.paster_command] mycommand = foo.commands.test_command:Test """

Create a directory called 'commands' under 'foo', add a __init.py__ file and create a file called test_command.py

In the file add

from paste.script import command

class TestCommand(command.Command):

    max_args = 1
    min_args = 1

    usage = "NAME"
    summary = "Say hello!"
    group_name = "My Package Name"

    parser = command.Command.standard_parser(verbose=True)
    parser.add_option('--goodbye',
                      action='store_true',
                      dest='goodbye',
                      help="Say 'Goodbye' instead")

    def command(self):
        name = self.args[0]
        if self.verbose:
            print "Got name: %r" % name
        if self.options.goodbye:
            print "Goodbye", name
        else:
            print "Hello", name

After you run 'python setup.py develop' you can now run 'paste mycommand bob' and you should get 'Hello bob' output.

To see how Pylons adds to this to create new files etc. look in pylons/commands.py they have commands for creating new Controllers and RestControllers that you can copy.

Ben
That answer got me past the part I was stuck on. Thank you.
Sean M