findig.dispatcher – Low-level dispatchers for Findig applications

This low-level module defines the Dispatcher class, from which findig.App derives.

class findig.dispatcher.Dispatcher(formatter=None, parser=None, error_handler=None, pre_processor=None, post_processor=None)[source]

A Dispatcher creates resources and routes requests to them.

Parameters:
  • formatter – A function that converts resource data to a string string suitable for output. It returns a 2-tuple: (mime_type, output). If not given, a generic findig.content.Formatter is used.
  • parser – A function that parses request input and returns a 2-tuple: (mime_type, data). If not given, a generic findig.content.Parser.
  • error_handler – A function that converts an exception to a Response. If not given, a generic findig.content.ErrorHandler is used.
  • pre_processor – A function that is called on request data just after is is parsed.
  • post_processor – A function that is called on resource data just before it is formatted.

This class is fairly low-level and shouldn’t be instantiated directly in application code. It does however serve as a base for findig.App.

formatter

If a formatter function was given to the constructor, then that is used. Otherwise, a generic findig.content.Formatter is used.

parser

The value that was passed for parser to the constructor. If no argument for parser was given to the constructor, then a generic findig.content.Parser is used.

error_handler

The value that was passed for error_handler to the constructor, or if None was given, then a generic findig.content.ErrorHandler.

response_class

A class that is used to construct responses after they’re returned from formatters.

alias of Response

build_rules()[source]

Return a generator for all of the url rules collected by the Dispatcher.

Return type:Iterable of werkzeug.routing.Rule

Note

This method will ‘freeze’ resource names; do not change resource names after this function is invoked.

dispatch()[source]

Dispatch the current request to the appropriate resource, based on which resource the rule applies to.

This function requires an active request context in order to work.

resource(wrapped, **args)[source]

Create a findig.resource.Resource instance.

Parameters:wrapped – A wrapped function for the resource. In most cases, this should be a function that takes named route arguments for the resource and returns a dictionary with the resource’s data.

The keyword arguments are passed on directly to the constructor for Resource, with the exception that name will default to {module}.{name} of the wrapped function if not given.

This method may also be used as a decorator factory:

@dispatcher.resource(name='my-very-special-resource')
def my_resource(route, param):
    return {'id': 10, ... }

In this case the decorated function will be replaced by a Resource instance that wraps it. Any keyword arguments passed to the decorator factory will be handed over to the Resource constructor. If no keyword arguments are required, then @resource may be used instead of @resource().

Note

If this function is used as a decorator factory, then a keyword parameter for wrapped must not be used.

route(resource, rulestr, **ruleargs)[source]

Add a route to a resource.

Adding a URL route to a resource allows Findig to dispatch incoming requests to it.

Parameters:
  • resource (Resource or function) – The resource that the route will be created for.
  • rulestr (str) – A URL rule, according to werkzeug’s specification.

See werkzeug.routing.Rule for valid rule parameters.

This method can also be used as a decorator factory to assign route to resources using declarative syntax:

@route("/index")
@resource(name='index')
def index_generator():
    return ( ... )
unrouted_resources

A list of resources created by the dispatcher which have no routes to them.