potpy.wsgi – WSGI module

This module provides classes for creating WSGI (PEP 333) applications.

For a simple example, see examples/wsgi.py. For a more complete example, see examples/todo.

Module Contents

class potpy.wsgi.PathRouter(*routes)[source]

Bases: potpy.router.Router

Route by URL/path.

Utilizes the Template class to capture path parameters, adding them to the Context. For example, you might define a route with a path template: /posts/{slug} – which would match the path /posts/my-post, adding {'slug': 'my-post'} to the context:

>>> from potpy.context import Context
>>> from pprint import pprint
>>> handler = lambda: None  # just a bogus handler
>>> router = PathRouter(('/posts/{slug}', handler))
>>> ctx = Context(path_info='/posts/my-post')
>>> ctx.inject(router)
>>> pprint(dict(ctx))
{'path_info': '/posts/my-post', 'slug': 'my-post'}

Routes can also be named, allowing reverse path lookup and filling of path parameters. See reverse() for details.

add([name], template, handler)[source]

Add a path template and handler.

Parameters:
  • name – Optional. If specified, allows reverse path lookup with reverse().
  • template – A string or Template instance used to match paths against. Strings will be wrapped in a Template instance.
  • handler – A callable or Route instance which will handle calls for the given path. See potpy.router.Router.add() for details.
reverse(name, **kwargs)[source]

Look up a path by name and fill in the provided parameters.

Example:

>>> handler = lambda: None  # just a bogus handler
>>> router = PathRouter(('post', '/posts/{slug}', handler))
>>> router.reverse('post', slug='my-post')
'/posts/my-post'
match(template, path_info)[source]

Check for a path match.

Parameters:
  • template – A Template object to match against.
  • path_info – The path to check for a match.
Returns:

The template parameters extracted from the path, or None if the path does not match the template.

Example:

>>> from potpy.template import Template
>>> template = Template('/posts/{slug}')
>>> PathRouter().match(template, '/posts/my-post')
{'slug': 'my-post'}
class potpy.wsgi.MethodRouter(*routes)[source]

Bases: potpy.router.Router

Route by request method.

>>> from potpy.context import Context
>>> handler1 = lambda request_method: (1, request_method.lower())
>>> handler2 = lambda request_method: (2, request_method.lower())
>>> router = MethodRouter(
...     ('POST', handler1),          # can specify a single method
...     (('GET', 'HEAD'), handler2)  # or a tuple of methods
... )
>>> Context(request_method='GET').inject(router)
(2, 'get')
>>> Context(request_method='POST').inject(router)
(1, 'post')
exception MethodNotAllowed(allowed_methods, request_method)[source]

Bases: potpy.router.NoRoute

Raised instead of potpy.router.Router.NoRoute when no handler matches the given method.

Has an allowed_methods attribute which is a list of the methods handled by this router.

MethodRouter.match(methods, request_method)[source]

Check for a method match.

Parameters:
  • methods – A method or tuple of methods to match against.
  • request_method – The method to check for a match.
Returns:

An empty dict in the case of a match, or None if there is no matching handler for the given method.

Example:

>>> MethodRouter().match(('GET', 'HEAD'), 'HEAD')
{}
>>> MethodRouter().match('POST', 'DELETE')
class potpy.wsgi.App(router, default_context=None)[source]

Wrap a potpy router in a WSGI application.

Use this with PathRouter and MethodRouter to implement a full-featured HTTP request routing system. Return a WSGI app from the last handler in the route, and it will be called with environ and start_response.

If no route matches, a 404 Not Found response will be generated. If using a MethodRouter, and the request method doesn’t match, a 405 Method Not Allowed response will be generated. Also responds to HTTP OPTIONS requests.

Calls the provided router with a context containing environ, path_info, and request_method fields, and any fields from the optional default_context argument.

Parameters:
  • router – The router to call in response to WSGI requests.
  • default_context – Optional. A dict-like mapping of extra fields to add to the context for each request.

Example:

>>> def my_app(environ, start_response):
...     start_response('200 OK', [('Content-type', 'text/plain')])
...     return ['Hello, world!']
...
>>> def handler(request):
...     # do something with the request
...     return my_app
...
>>> class Request(object):
...     def __init__(self, environ):
...         pass    # wrap environ in a custom request object
...
>>> app = App(
...     PathRouter(('/hello', lambda: my_app)),
...     {'request': Request}    # add a Request object to context
... )
>>> app({
...     'PATH_INFO': '/hello',
...     'REQUEST_METHOD': 'GET',
... }, lambda status, headers: None)    # bogus start_response
['Hello, world!']
__call__(environ, start_response)[source]

Call the router as a WSGI app.

Constructs a Context object with environ, path_info, and request_method (extracted from the environ), and any fields supplied in self.default_context.

Calls the result of the router call as a WSGI app.

Project Versions

Table Of Contents

Previous topic

potpy.template – Template module

Next topic

potpy.configparser – Config Parser module

This Page