potpy.configparser – Config Parser module

Construct a WSGI router from a configuration file.

A configuration file consists of lines specifying URLs, request methods, and handlers, allowing construction of PathRouter and MethodRouter instances using a hierarchical syntax.

At the top level, you specify URLs with optional names and parameter type converters. See the Template class documentation for the converter and URL specification format.

foo /foo/{foo_id:\d+} (foo_id: int):
    ...

Following each URL is a list of handlers, one on each line. Handlers may also specify a name (in parentheses), in which case the result of the handler is added to the routing context under that name.

read_foo (foo)
save_foo

It is also possible to specify request method handlers using * METHOD: blocks. Adjacent method blocks are combined into a single MethodRouter instance.

* GET, HEAD:
    show_foo
* POST:
    edit_foo

Exception handlers can be specified for a given handler by ending the handler line with a colon (:) and listing exception types and handlers on the following lines.

read_foo (foo):
    ValidationError, BadFooError: show_foo_errors
    IOError: show_system_errors

Complete Example:

index /:
    * GET, HEAD:
        views.index
article /{article_id:\d+} (article_id: int):
    * GET, HEAD:
        views.show_article
    * POST:
        auth.require_user (user)
        views.edit_article:
            views.InvalidArticleError: views.show_article_errors
admin /admin/:
    auth.require_admin (user)  # run this regardless of request_method
    * GET, HEAD:
        views.admin_console

Module Contents

potpy.configparser.parse_config(lines, module=None)[source]

Parse a config file.

Names referenced within the config file are found within the calling scope. For example:

>>> from potpy.configparser import parse_config
>>> class foo:
...     @staticmethod
...     def bar():
...         pass
...
>>> config = '''
... /foo:
...     foo.bar
... '''
>>> router = parse_config(config.splitlines())

would find the bar method of the foo class, because foo is in the same scope as the call to parse_config.

Parameters:
  • lines – An iterable of configuration lines (an open file object will do).
  • module – Optional. If provided and not None, look for referenced names within this object instead of the calling module.
potpy.configparser.load_config(name='urls.conf')[source]

Load a config from a resource file.

The resource is found using pkg_resources.resource_stream(), relative to the calling module.

See parse_config() for config file details.

Parameters:name – The name of the resource, relative to the calling module.

Project Versions

Table Of Contents

Previous topic

potpy.wsgi – WSGI module

This Page