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 listmethod
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 withpath:
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 aRequest
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 anexceptions.RouteConflict
template_dir (str) – if specified, a
template_engine
is created with this as the argument. forpigwig.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 takestemplate_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, theRequest
, and a reference to thisPigWig
instance. it must return aResponse
and should almost certainly have the code of the original exception. exceptions raised here can be handled byexception_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 aResponse
. 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 constructortemplate_engine
cookie_secret
http_exception_handler
exception_handler