在cherrypy中使用外部文件配置工具

发布于 2024-11-13 06:51:20 字数 662 浏览 2 评论 0原文

我试图弄清楚如何使用外部配置文件配置一个工具,以便在cherrypy中收到请求时运行。我已经阅读了文档中的示例,但这些示例都将配置嵌入到源文件中,而不是单独的配置文件中。我读过可以在外部配置工具,但我没有找到任何示例。

wiki 上的示例为例,我希望能够按照逻辑执行以下操作:

tools.print_path = cherrypy.Tool('on_start_resource', {what goes here?})

假设我的 PYTHONPATH 中有一个名为“mytools.py”的文件,我可以使用“import mytools”导入该文件,并且在该文件中我有一个简单的“def print_path(multiplier=1)”方法。我应该在“{这里放什么?}”位置放什么?我尝试过 mytools.print_path 的变体,我得到的最好的结果是:

CherryPy Checker:
The config entry 'tools.print_path' may be invalid, because the 'print_path' tool was not found.
section: [/]

如果有人能指出我正确的方向,我将不胜感激。

I'm trying to figure out how to configure a tool to run whenever a request is received in cherrypy, using an external configuration file. I've read through the examples in the documentation, but these all embed the configuration into the source file, rather than a separate configuration file. I've read that tools can be configured externally, but I haven't found any examples.

Taking the example on the wiki, I'd like to be able to do something logically like this:

tools.print_path = cherrypy.Tool('on_start_resource', {what goes here?})

Suppose I have a file named 'mytools.py' in my PYTHONPATH, which I can import using 'import mytools', and in this file I have a simple "def print_path(multiplier=1)" method. What do I put in the "{what goes here?}" spot? I've tried variations on mytools.print_path and the best I get is this:

CherryPy Checker:
The config entry 'tools.print_path' may be invalid, because the 'print_path' tool was not found.
section: [/]

If anyone could point me in the right direction, I'd be most appreciative.

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

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

发布评论

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

评论(1

混吃等死 2024-11-20 06:51:20

配置文件内部没有用于实例化工具的工具(cherrypy.Tool(...) 部分)。您需要在代码中执行此操作。您的“mytools.py”文件应如下所示:

def print_path(multiplier=1):
    ...
cherrypy.tools.print_path = cherrypy.Tool('on_start_resource', print_path)

...然后您的配置文件用于为给定 URL(及其子项)打开工具:

[/]
tools.print_path.on: True
tools.print_path.multiplier: 23

只需确保在处理配置文件之前“导入 mytools”在你的启动脚本中。

There is no facility inside of a config file to instantiate a Tool (the cherrypy.Tool(...) part). You need to do that in code. Your 'mytools.py' file should look like this:

def print_path(multiplier=1):
    ...
cherrypy.tools.print_path = cherrypy.Tool('on_start_resource', print_path)

...and then your config file is used to turn the Tool on for a given URL (and its children):

[/]
tools.print_path.on: True
tools.print_path.multiplier: 23

Just make sure you 'import mytools' before processing the config file in your startup script.

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