cherrypy._cptools module

CherryPy tools. A “tool” is any helper, adapted to CP.

Tools are usually designed to be used in a variety of ways (although some may only offer one if they choose):

Library calls

All tools are callables that can be used wherever needed. The arguments are straightforward and should be detailed within the docstring.

Function decorators

All tools, when called, may be used as decorators which configure individual CherryPy page handlers (methods on the CherryPy tree). That is, “@tools.anytool()” should “turn on” the tool via the decorated function’s _cp_config attribute.

CherryPy config

If a tool exposes a “_setup” callable, it will be called once per Request (if the feature is “turned on” via config).

Tools may be implemented as any object with a namespace. The builtins are generally either modules or instances of the tools.Tool class.

class cherrypy._cptools.CachingTool(point, callable, name=None, priority=50)[source]

Bases: Tool

Caching Tool for CherryPy.

_setup()[source]

Hook caching into cherrypy.request.

_wrapper(**kwargs)[source]
class cherrypy._cptools.ErrorTool(callable, name=None)[source]

Bases: Tool

Tool which is used to replace the default request.error_response.

_setup()[source]

Hook this tool into cherrypy.request.

The standard CherryPy request object will automatically call this method when the tool is “turned on” in config.

_wrapper()[source]
class cherrypy._cptools.HandlerTool(callable, name=None)[source]

Bases: Tool

Tool which is called ‘before main’, that may skip normal handlers.

If the tool successfully handles the request (by setting response.body), if should return True. This will cause CherryPy to skip any ‘normal’ page handler. If the tool did not handle the request, it should return False to tell CherryPy to continue on and call the normal page handler. If the tool is declared AS a page handler (see the ‘handler’ method), returning False will raise NotFound.

_setup()[source]

Hook this tool into cherrypy.request.

The standard CherryPy request object will automatically call this method when the tool is “turned on” in config.

_wrapper(**kwargs)[source]
handler(*args, **kwargs)[source]

Use this tool as a CherryPy page handler.

For example:

class Root:
    nav = tools.staticdir.handler(section="/nav", dir="nav",
                                  root=absDir)
class cherrypy._cptools.HandlerWrapperTool(newhandler, point='before_handler', name=None, priority=50)[source]

Bases: Tool

Tool which wraps request.handler in a provided wrapper function.

The ‘newhandler’ arg must be a handler wrapper function that takes a ‘next_handler’ argument, plus *args and **kwargs. Like all page handler functions, it must return an iterable for use as cherrypy.response.body.

For example, to allow your ‘inner’ page handlers to return dicts which then get interpolated into a template:

def interpolator(next_handler, *args, **kwargs):
    filename = cherrypy.request.config.get('template')
    cherrypy.response.template = env.get_template(filename)
    response_dict = next_handler(*args, **kwargs)
    return cherrypy.response.template.render(**response_dict)
cherrypy.tools.jinja = HandlerWrapperTool(interpolator)
callable(*args, **kwargs)[source]
class cherrypy._cptools.SessionAuthTool(callable, name=None)[source]

Bases: HandlerTool

class cherrypy._cptools.SessionTool[source]

Bases: Tool

Session Tool for CherryPy.

sessions.locking

When ‘implicit’ (the default), the session will be locked for you, just before running the page handler.

When ‘early’, the session will be locked before reading the request body. This is off by default for safety reasons; for example, a large upload would block the session, denying an AJAX progress meter (issue).

When ‘explicit’ (or any other value), you need to call cherrypy.session.acquire_lock() yourself before using session data.

_lock_session()[source]
_setup()[source]

Hook this tool into cherrypy.request.

The standard CherryPy request object will automatically call this method when the tool is “turned on” in config.

regenerate()[source]

Drop the current session and make a new one (with a new id).

class cherrypy._cptools.Tool(point, callable, name=None, priority=50)[source]

Bases: object

A registered function for use with CherryPy request-processing hooks.

help(tool.callable) should give you more information about this Tool.

_merged_args(d=None)[source]

Return a dict of configuration entries for this Tool.

_setargs()[source]

Copy func parameter names to obj attributes.

_setup()[source]

Hook this tool into cherrypy.request.

The standard CherryPy request object will automatically call this method when the tool is “turned on” in config.

namespace = 'tools'
property on
class cherrypy._cptools.Toolbox(namespace)[source]

Bases: object

A collection of Tools.

This object also functions as a config namespace handler for itself. Custom toolboxes should be added to each Application’s toolboxes dict.

register(point, **kwargs)[source]

Return a decorator which registers the function at the given hook point.

class cherrypy._cptools.XMLRPCController[source]

Bases: object

A Controller (page handler collection) for XML-RPC.

To use it, have your controllers subclass this base class (it will turn on the tool for you).

You can also supply the following optional config entries:

tools.xmlrpc.encoding: 'utf-8'
tools.xmlrpc.allow_none: 0

XML-RPC is a rather discontinuous layer over HTTP; dispatching to the appropriate handler must first be performed according to the URL, and then a second dispatch step must take place according to the RPC method specified in the request body. It also allows a superfluous “/RPC2” prefix in the URL, supplies its own handler args in the body, and requires a 200 OK “Fault” response instead of 404 when the desired method is not found.

Therefore, XML-RPC cannot be implemented for CherryPy via a Tool alone. This Controller acts as the dispatch target for the first half (based on the URL); it then reads the RPC method from the request body and does its own second dispatch step based on that method. It also reads body params, and returns a Fault on error.

The XMLRPCDispatcher strips any /RPC2 prefix; if you aren’t using /RPC2 in your URL’s, you can safely skip turning on the XMLRPCDispatcher. Otherwise, you need to use declare it in config:

request.dispatch: cherrypy.dispatch.XMLRPCDispatcher()
_cp_config = {'tools.xmlrpc.on': True}
default(*vpath, **params)[source]
cherrypy._cptools._getargs(func)[source]

Return the names of all static arguments to the given function.