开发者

Configuring tools with an external file in cherrypy

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 sou开发者_JAVA百科rce 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜