App — classes for creating WSGI callables¶
The core Findig namespace defines the Findig :class:App class, which is essential to building Findig applications. Every :class:App is capable of registering resources as well as URL routes that point to them, and is a WSGI callable that can be passed to any WSGI complaint server.
-
class
findig.
App
(autolist=False)[source]¶ Bases:
findig.dispatcher.Dispatcher
-
request_class
¶ The class used to wrap WSGI environments by this App instance.
alias of
Request
-
build_context
(environ)[source]¶ Start a request context.
Parameters: environ – A WSGI environment. Returns: A context manager for the request. When the context manager exits, the request context variables are destroyed and all cleanup hooks are run. Note
This method is intended for internal use; Findig will call this method internally on its own. It is not re-entrant with a single request.
-
cleanup_hook
(func)[source]¶ Register a function that should run after each request in the application.
-
context
(func)[source]¶ Register a request context manager for the application.
A request context manager is a function that yields once, that is used to wrap request contexts. It is called at the beginning of a request context, during which it yields control to Findig, and regains control sometime after findig processes the request. If the function yields a value, it is made available as an attribute on
findig.context.ctx
with the same name as the function.Example:
>>> from findig.context import ctx >>> from findig import App >>> >>> app = App() >>> items = [] >>> @app.context ... def meaning(): ... items.extend(["Life", "Universe", "Everything"]) ... yield 42 ... items.clear() ... >>> with app.test_context(create_route=True): ... print("The meaning of", end=" ") ... print(*items, sep=", ", end=": ") ... print(ctx.meaning) ... The meaning of Life, Universe, Everything: 42 >>> items []
-
startup_hook
(func)[source]¶ Register a function to be run before the very first request in the application.
-
test_context
(create_route=False, **args)[source]¶ Make a mock request context for testing.
A mock request context is generated using the arguments here. In other words, context variables are set up and callbacks are registered. The returned object is intended to be used as a context manager:
app = App() with app.test_context(): # This will set up request context variables # that are needed by some findig code. do_some_stuff_in_the_request_context() # After the with statement exits, the request context # variables are cleared.
This method is really just a shortcut for creating a fake WSGI environ with
werkzeug.test.EnvironBuilder
and passing that tobuild_context()
. It takes the very same keyword parameters asEnvironBuilder
; the arguments given here are passed directly in.Parameters: create_route – Create a URL rule routing to a mock resource, which will match the path of the mock request. This must be set to True if the mock request being generated doesn’t already have a route registered for the request path, otherwise this method will raise a werkzeug.exceptions.NotFound
error.Returns: A context manager for a mock request.
-
-
class
findig.json.
App
(indent=None, encoder_cls=None, autolist=False)[source]¶ A
findig.App
that works with application/json data.This app is pre-configured to parse incoming
application/json
data, outputapplication/json
data by default and convert errors toapplication/json
responses.Parameters: - indent – The number of spaces to indent by when outputting JSON. By default, no indentation is used.
- encoder_cls – A
json.JSONEncoder
subclass that should be used to serialize data into JSON. By default, an encoder that converts all mappings to JSON objects and all other iterables to JSON lists in addition to the normally supported simplejson types (int, float, str) is used. - autolist – Same as the autolist parameter in
findig.App
.