Pyramid project paster command line tools script template

Wednesday, October 26, 2011

1, new a commands script file commands.py

from paste.script.command import Command
import os.path
from pyramid.threadlocal import manager as threadlocal_manager
from paste.deploy import loadapp


class PyramidCommand(Command):

    group_name = "lxneng"
    min_args = 1
    max_args = 1
    parser = Command.standard_parser()

    def setupPyramid(self):
        app = loadapp('config:%s' % self.args[0], relative_to=os.path.abspath('.'))
        registry = app.registry
        try:
            threadlocal_manager.push({'registry':registry})
        finally:
            threadlocal_manager.pop()


class DemoCommand(PyramidCommand):
    summary = "demo script"
    usage = "paster --plugin=lxneng demo_script development.ini"
    parser = PyramidCommand.standard_parser()

    def command(self):
        from lxneng.models.post import Tag
        tag = Tag.find_by_name('Python')
        print tag.name

2, add command to setup.py

entry_points="""\
  [paste.app_factory]
  main = lxneng:main

  [paste.paster_command]
  demo_script = lxneng.commands:DemoCommand
  """,

3, build

python setup.py develop

4, run

paster --plugin=lxneng demo_script development.ini

note:
lxneng is my project name

This entry was tagged paster, Pyramid and Python

comments powered by Disqus

© 2009-2013 lxneng.com. All rights reserved. Powered by Pyramid

go to Top