在cherrypy中使用外部文件配置工具
我试图弄清楚如何使用外部配置文件配置一个工具,以便在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
配置文件内部没有用于实例化工具的工具(
cherrypy.Tool(...)
部分)。您需要在代码中执行此操作。您的“mytools.py”文件应如下所示:...然后您的配置文件用于为给定 URL(及其子项)打开工具:
只需确保在处理配置文件之前“导入 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:...and then your config file is used to turn the Tool on for a given URL (and its children):
Just make sure you 'import mytools' before processing the config file in your startup script.