findig.content — Formatters, parsers and error handlers

These are helper implementations of content-handling ‘functions’ for parsing, formatting and error-handling. The module exposes Parser, Formatter and ErrorHandler respectively, each of which acts like a function but introduces some additional semantics.

Although this is the default behavior, Findig applications are not required to use to tools provided by this module and may use any callable in their place.

Note

Instances of Formatter and Parser require an active request context to work when called.

class findig.content.ErrorHandler[source]

A generic implementation of a error handler ‘function’.

A ErrorHandler collects handler functions for specific exception types, so that when it is called, it looks up the appropriate handler for the exception that it is called with. The handler used is the closest superclass of the exception’s type. If no handler was registered for the exception, then it is raised again.

register(err_type, handler)[source]

Register a handler function for a particular exception type and its subclasses.

Parameters:err_type – A type of Exception
Type:BaseException or subclass.
Handler:A function that will handle errors of the given type.

This method is also usable as a decorator factory:

handler = ErrorHandler()
@handler.register(ValueError)
def handle_value_err(e):
    # Handle a value error
    pass
class findig.content.Formatter[source]

A generic implementation of a formatter ‘function’.

A Formatter collects handler functions for specific mime-types, so that when it is called, it looks up the the appropriate function to call in turn, according to the mime-type specified by the request’s Accept header.

register(mime_type, handler, default=False)

Register a handler function for a particular content-type.

Parameters:
  • mime_type – A content type.
  • handler – A handler function for the given content type.
  • default – Whether the handler should be used for requests which don’t specify a preferred content-type. Only one default content type may be given, so if default=True is set multiple times, only the last one takes effect.

Tip

This method can also be used as a generator factory.

class findig.content.Parser[source]

A generic implementation of a parser ‘function’.

A Parser collects handler functions for specific mime-types, so that when it is called, it looks up the the appropriate function to call in turn, according to the mime-type specified by the request’s Content-Type header.

register(mime_type, handler, default=False)

Register a handler function for a particular content-type.

Parameters:
  • mime_type – A content type.
  • handler – A handler function for the given content type.
  • default – Whether the handler should be used for requests which don’t specify a preferred content-type. Only one default content type may be given, so if default=True is set multiple times, only the last one takes effect.

Tip

This method can also be used as a generator factory.