PigWig

class pigwig.PigWig(routes: RouteDefinition | Callable[[], RouteDefinition], template_dir: str | None = None, template_engine: type = <class 'pigwig.templates_jinja.JinjaTemplateEngine'>, cookie_secret: bytes | None = None, http_exception_handler: HTTPExceptionHandler = <function default_http_exception_handler>, exception_handler: ExceptionHandler = <function default_exception_handler>, response_done_handler: Callable[[Request, Response], Any] | None = None)[source]

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

Parameters:
  • 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: dict, start_response: Callable) Iterable[bytes][source]

main WSGI entrypoint

pigwig.default_http_exception_handler(e: HTTPException, errors: TextIO, request: Request, app: PigWig) Response[source]
pigwig.default_exception_handler(e: Exception, errors: TextIO, request: Request, app: PigWig) Response[source]