PotPy Documentation

PotPy lets you build applications in a very flexible way. You design objects based on your domain requirements, then compose them together using PotPy’s flexible routing system. PotPy enables you to:

  • Write simple, decoupled objects that work together in various ways.
  • Easily test your objects, by not imposing a rigid object construction paradigm.
  • Write WSGI components using TDD, without having to deal with WSGI conventions except at the edges of the system.

PotPy was inspired by the Raptor project from the Ruby world.

Installation

$ pip install potpy

For details, see the README file.

Hello World Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from potpy.wsgi import PathRouter, MethodRouter, App

# Our domain objects
# ------------------

class Greeter(object):
    """A WSGI app that displays a greeting."""
    def __init__(self, greeting):
        self.greeting = greeting

    def __call__(self, environ, start_response):
        start_response('200 OK', [
            ('Content-type', 'text/plain'),
            ('Content-length', str(len(self.greeting))),
        ])
        return [self.greeting]


def get_greeting(name):
    """Generate a greeting for the given name."""
    return 'Hello, %s' % (name,)



# PotPy plumbing
# --------------

hello = MethodRouter(
    (('GET', 'HEAD'), [                 # when the request is a GET or HEAD

        (get_greeting, 'greeting'),     # generate a greeting and save it
                                        # to the context under the
                                        # 'greeting' key

        Greeter                         # then show the greeting
    ]),
)


urls = PathRouter(
    ('hello', '/hello/{name}', hello),  # expose the greeter at /hello/{name}
)


if __name__ == '__main__':
    from wsgiref.simple_server import make_server
    make_server('', 8000, App(urls)).serve_forever()

More Examples

Overview

PotPy is a generic routing apparatus. It allows you to develop applications by composing your domain objects together into one or more “pipelines” for data to flow along. Composition is accomplished with the idea of a Context – return values from handler functions along these routes can be added to the context, enabling later handler functions to access them.

The magic happens when a context is injected into a callable. PotPy inspects the callable’s signature, and pulls values out of the context by argument name. For example:

>>> from potpy.context import Context
>>> def my_callable(foo, bar, baz='default'):
...     # do something cool
...     return 42
...
>>> ctx = Context(foo='some value', bar='another value')
>>> ctx.inject(my_callable)
42

Building on the context, PotPy provides routes and routers. A Route is a list of callables that you define which get called in order. These callables are actually called by injecting a context, as above. The same context is used to call subsequent callables in the route, and these callables can either interact with the context directly, or their return value may be added to the context by the Route object. This allows later callables in a route to access information produced by earlier ones, which facilitates a very expressive style of application design.

A Router is an object that (typically) selects between various Routes given some condition. The Router class itself is an abstract base class, although the potpy.wsgi module provides two concrete subclasses that route based on specific WSGI environ variables (PEP 333). By subclassing the Router class and providing a match() method that selects based on something specific to your problem domain, you can build powerful control flows between the objects in your system with minimal effort.

The potpy.template, potpy.wsgi, and potpy.configparser modules turn PotPy into a flexible HTTP request routing system. Template objects allow string matching with parameter extraction, and the reverse – filling parameters into a string from a mapping. The potpy.wsgi.PathRouter class utilizes these templates to make URL-based routing convenient and easy. The configparser module enables you to specify a web application’s URL layout in a simple declarative syntax, while being flexible enough to let you specify which HTTP methods (eg. GET, POST, etc.) your domain objects should handle, and exception handlers.

Indices and tables

Project Versions

Table Of Contents

Next topic

potpy.context – Context module

This Page