修改新 Pylons 控制器的模板

发布于 2024-09-25 11:40:43 字数 353 浏览 0 评论 0原文

在我的 Pylons 项目中,我最终经常创建和删除控制器(可能比我应该的更频繁)。我厌倦了在每个控制器的顶部添加自己的导入和调整。 最近有一个关于修改新控制器模板的问题< /a> 这让我部分地不必这样做 - 但我不明白粘贴器如何使用controller.py_tmpl文件,以及我如何告诉粘贴器,对于现有项目,“嘿,使用这个模板来代替!”

我需要什么调用来告诉 Paster 使用我的模板而不是默认模板?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哀由 2024-10-02 11:40:43

Pylons 通过添加粘贴命令来创建新的控制器和项目。这些命令在 setup.py 中定义,您可以添加新命令。

例如(这取自 Paste docs),假设您有一个名为 Foo 的项目,它是在一个也称为 foo 的包中。

在 setup.py 中将 'foo' 添加到 'paster_plugins' 列表中
然后将新命令添加到entry_points。

IE
入口点=“”“
[粘贴.paster_命令]
mycommand = foo.commands.test_command:测试
"""

在 'foo' 下创建一个名为 'commands' 的目录,添加一个 __init.py__ 文件并创建一个名为 test_command.py 的文件

在该文件中添加

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

运行 'python setup.pydevelop' 后,您现在可以运行 'paste mycommand bob' 并且您应该得到 'Hello bob' 输出。

要了解 Pylons 如何添加到此以创建新文件等,请查看 pylons/commands.py,它们具有用于创建您可以复制的新控制器和 RestController 的命令。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文