class pigwig.PigWig(routes, template_dir=None, template_engine=<class 'pigwig.templates_jinja.JinjaTemplateEngine'>, cookie_secret=None, http_exception_handler=<function default_http_exception_handler>, exception_handler=<function default_exception_handler>, response_done_handler=None)[source]

Bases: object

main WSGI entrypoint. this is a class but defines a __call__() so instances of it can be passed directly to WSGI servers.

  • routes (list or function) –

    a list of 3-tuples: (method, path, handler) or a function that returns such a list

    • method is the HTTP method/verb (GET, POST, etc.)
    • path can either be a static path (/foo/bar) or have params (/post/<id>). params can be prefixed with path: to eat up the rest of the path (/tree/<path:subdir> matches /tree/a/b/c). params are passed to the handler as keyword arguments. params cannot be optional, but you can map two routes to a handler that takes an optional argument. params must make up the entire path segment - you cannot have /post_<id>.
    • handler is a function taking a Request positional argument and any number of param keyword arguments

    having two identical static routes or two overlapping param segments (/foo/<bar> and /foo/<baz>) with the same method raises an exceptions.RouteConflict

  • template_dir (str) – if specified, a template_engine is created with this as the argument. for pigwig.templates_jinja.JinjaTemplateEngine, this should be an absolute path or it will be relative to the current working directory.
  • template_engine – a class that takes a template_dir in the constructor and has a .stream method that takes template_name, context as arguments (passed from user code - for jinja2, context is a dictionary)
  • cookie_secret (str) – app-wide secret used for signing secure cookies. see Request.get_secure_cookie()
  • http_exception_handler – a function that will be called when an exceptions.HTTPException is raised. it will be passed the original exception, wsgi.errors, the Request, and a reference to this PigWig instance. it must return a Response and should almost certainly have the code of the original exception. exceptions raised here can be handled by exception_handler.
  • exception_handler – a function that will be called when any other exception is raised. it will be passed the same arguments as http_exception_handler and must also return a Response. be careful: raising an exception here is bad.
  • response_done_handler – a function that will be called when control has been returned back to the WSGI server. it will be passed a request and response. be careful: raising an exception here is very bad.

has the following instance attrs:

  • routes - an internal representation of the route tree - not the list passed to the constructor
  • template_engine
  • cookie_secret
  • http_exception_handler
  • exception_handler
__call__(environ, start_response)[source]

main WSGI entrypoint


builds Response objects. for internal use.

main(host='', port=None)[source]

sets up the autoreloader and runs a wsgiref.simple_server. useful for development.

static handle_urlencoded(body, length, params)[source]
static handle_json(body, length, params)[source]
static handle_multipart(body, length, params)[source]
content_handlers = {'application/json': <function PigWig.handle_json at 0x7fd117d8dd08>, 'application/x-www-form-urlencoded': <function PigWig.handle_urlencoded at 0x7fd117d8dc80>, 'multipart/form-data': <function PigWig.handle_multipart at 0x7fd117d8dd90>}
pigwig.default_http_exception_handler(e, errors, request, app)[source]
pigwig.default_exception_handler(e, errors, request, app)[source]