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.
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 a path template and handler.
Parameters: |
|
---|
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'
Check for a path match.
Parameters: |
|
---|---|
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'}
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')
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.
Check for a method match.
Parameters: |
|
---|---|
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')
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: |
|
---|
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!']