pigwig
pigwig
PigWig(routes: RouteDefinition | Callable[[], RouteDefinition], template_dir: str | None = None, template_engine: type = JinjaTemplateEngine, cookie_secret: bytes | None = None, http_exception_handler: HTTPExceptionHandler = default_http_exception_handler, exception_handler: ExceptionHandler = default_exception_handler, response_done_handler: Callable[[Request, Response], Any] | None = None)
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 | function) –a list of 3-tuples:
(method, path, handler)or a function that returns such a list *methodis the HTTP method/verb (GET,POST, etc.) *pathcan 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>. *handleris 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, default:None) –if specified, a
template_engineis 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(type, default:JinjaTemplateEngine) –a class that takes a
template_dirin the constructor and has a.streammethod that takestemplate_name, contextas arguments (passed from user code - for jinja2, context is a dictionary) -
cookie_secret(str, default:None) –app-wide secret used for signing secure cookies. see Request.get_secure_cookie
-
http_exception_handler(HTTPExceptionHandler, default:default_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 byexception_handler. -
exception_handler(ExceptionHandler, default:default_exception_handler) –a function that will be called when any other exception is raised. it will be passed the same arguments as
http_exception_handlerand must also return a Response. be careful: raising an exception here is bad. -
response_done_handler(Callable[[Request, Response], Any] | None, default:None) –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_enginecookie_secrethttp_exception_handlerexception_handler
Source code in pigwig/pigwig.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | |
content_handlers: dict[str, Callable[[io.BufferedIOBase, int | None, dict[str, str]], Any]]
instance-attribute
map of content types to body parsers
__call__(environ: dict, start_response: Callable) -> Iterable[bytes]
main WSGI entrypoint
Source code in pigwig/pigwig.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | |
build_request(environ: dict) -> tuple[Request, Exception | None]
builds Response objects. for internal use.
Source code in pigwig/pigwig.py
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | |
main(host: str = '0.0.0.0', port: int | None = None) -> None
sets up the autoreloader and runs a wsgiref.simple_server. useful for development.
Source code in pigwig/pigwig.py
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | |
default_http_exception_handler(e: exceptions.HTTPException, errors: TextIO, request: Request, app: 'PigWig') -> Response
Source code in pigwig/pigwig.py
25 26 27 28 | |
default_exception_handler(e: Exception, errors: TextIO, request: Request, app: 'PigWig') -> Response
Source code in pigwig/pigwig.py
30 31 32 33 | |