cherrypy._cpconfig

Configuration system for CherryPy.

Configuration in CherryPy is implemented via dictionaries. Keys are strings which name the mapped value, which may be of any type.

Architecture

CherryPy Requests are part of an Application, which runs in a global context, and configuration data may apply to any of those three scopes:

Global
Configuration entries which apply everywhere are stored in cherrypy.config.
Application
Entries which apply to each mounted application are stored on the Application object itself, as ‘app.config’. This is a two-level dict where each key is a path, or “relative URL” (for example, “/” or “/path/to/my/page”), and each value is a config dict. Usually, this data is provided in the call to tree.mount(root(), config=conf), although you may also use app.merge(conf).
Request
Each Request object possesses a single ‘Request.config’ dict. Early in the request process, this dict is populated by merging global config entries, Application entries (whose path equals or is a parent of Request.path_info), and any config acquired while looking up the page handler (see next).

Declaration

Configuration data may be supplied as a Python dictionary, as a filename, or as an open file object. When you supply a filename or file, CherryPy uses Python’s builtin ConfigParser; you declare Application config by writing each path as a section header:

[/path/to/my/page]
request.stream = True

To declare global configuration entries, place them in a [global] section.

You may also declare config entries directly on the classes and methods (page handlers) that make up your CherryPy application via the _cp_config attribute. For example:

class Demo:
    _cp_config = {'tools.gzip.on': True}

    def index(self):
        return "Hello world"
    index.exposed = True
    index._cp_config = {'request.show_tracebacks': False}

Note

This behavior is only guaranteed for the default dispatcher. Other dispatchers may have different restrictions on where you can attach _cp_config attributes.

Namespaces

Configuration keys are separated into namespaces by the first ”.” in the key. Current namespaces:

engine
Controls the ‘application engine’, including autoreload. These can only be declared in the global config.
tree
Grafts cherrypy.Application objects onto cherrypy.tree. These can only be declared in the global config.
hooks
Declares additional request-processing functions.
log
Configures the logging for each application. These can only be declared in the global or / config.
request
Adds attributes to each Request.
response
Adds attributes to each Response.
server
Controls the default HTTP server via cherrypy.server. These can only be declared in the global config.
tools
Runs and configures additional request-processing packages.
wsgi
Adds WSGI middleware to an Application’s “pipeline”. These can only be declared in the app’s root config (“/”).
checker
Controls the ‘checker’, which looks for common errors in app state (including config) when the engine starts. Global config only.

The only key that does not exist in a namespace is the “environment” entry. This special entry ‘imports’ other config entries from a template stored in cherrypy._cpconfig.environments[environment]. It only applies to the global config, and only when you use cherrypy.config.update.

You can define your own namespaces to be called at the Global, Application, or Request level, by adding a named handler to cherrypy.config.namespaces, app.namespaces, or app.request_class.namespaces. The name can be any string, and the handler must be either a callable or a (Python 2.5 style) context manager.

Classes

class cherrypy._cpconfig.Config(file=None, **kwargs)

The ‘global’ configuration data for the entire CherryPy process.

clear() → None. Remove all items from D.
copy() → a shallow copy of D
static fromkeys(S[, v]) → New dict with keys from S and values equal to v.

v defaults to None.

get(k[, d]) → D[k] if k in D, else d. d defaults to None.
has_key(k) → True if D has a key k, else False
items() → list of D's (key, value) pairs, as 2-tuples
iteritems() → an iterator over the (key, value) items of D
iterkeys() → an iterator over the keys of D
itervalues() → an iterator over the values of D
keys() → list of D's keys
pop(k[, d]) → v, remove specified key and return the corresponding value.

If key is not found, d is returned if given, otherwise KeyError is raised

popitem() → (k, v), remove and return some (key, value) pair as a

2-tuple; but raise KeyError if D is empty.

reset()

Reset self to default values.

setdefault(k[, d]) → D.get(k,d), also set D[k]=d if k not in D
update(config)

Update self from a dict, file or filename.

values() → list of D's values
viewitems() → a set-like object providing a view on D's items
viewkeys() → a set-like object providing a view on D's keys
viewvalues() → an object providing a view on D's values

Functions

cherrypy._cpconfig.merge(base, other)

Merge one app config (from a dict, file, or filename) into another.

If the given config is a filename, it will be appended to the list of files to monitor for “autoreload” changes.