API docs: fresco.core

class fresco.core.FrescoApp(*args, **kwargs)

Fresco application class.

add_middleware(middleware, *args, **kwargs)

Add a WSGI middleware layer

Note that middleware is applied from the outside in. The first middleware added will occupy the innermost layer and be called last in each request cycle.

call_process_teardown_handlers(request)

Called once the request has been completed and the response content output.

core_middleware = None

Middleware layers applied after any middleware added through add_middleware().

get_methods(routecollection, request, path)

Return the HTTP methods valid in routes to the given path

handle_http_error_response(request, response)

Call any process_http_error_response handlers and return the (potentially modified) response object.

insert_middleware(position, middleware, *args, **kwargs)

Insert a middleware layer at the given position

Parameters:
  • position – The index of the element before which to insert. Note that middleware is applied from the outside in, so middleware inserted at position zero will be called last.
  • middleware – A middleware callable.
  • args – extra positional args to be passed to the middleware for initialization
  • kwargs – extra keyword args to be passed to the middleware for initialization
logger = None

A stdlib logger object, or None

options = None

An options dictionary, for arbitrary application variables or configuration

pattern_class

The default class to use for URL pattern matching

alias of ExtensiblePattern

process_exception(func, exc_type=<class 'Exception'>)

Register a process_exception hook function

process_exception_handlers = None

Functions to be called if an exception is raised during a view Each function will be passed request, exc_info. If a function returns a value (other than None), this value will be returned as the response and the error will not be propagated. If all exception handlers return None then the error will be raised

process_http_error_response(func, status=None)

Register a process_http_error_response hook function

process_http_error_response_handlers = None

Functions to be called if the response has an HTTP error status code (400 <= x <= 599) Each function will be passed request, response. If a function returns a value (other than None), this value will be returned as the response instead of calling the scheduled view.

process_request(func)

Register a process_request hook function

process_request_handlers = None

Functions to be called before the routing has been traversed. Each function will be passed the request object. If a function returns a value (other than None) this will be returned and the normal routing system bypassed.

process_response(func)

Register a process_response hook function

process_response_handlers = None

Functions to be called after a response object has been generated Each function will be passed request, response. If a function returns a value (other than None), this value will be returned as the response instead of calling the scheduled view.

process_teardown(func)

Register a process_teardown hook function

process_teardown_handlers = None

Functions to be called at the end of request processing, after all content has been output. Each function will be passed request and should not return any value.

process_view(func)

Register a process_view hook function

process_view_handlers = None

Functions to be called after routing, but before any view is invoked Each function will be passed request, view, view_args, view_kwargs. If a function returns a Response instance this will be returned as the response instead of calling the scheduled view. If a function returns any other (non-None) value this will be used to replace the scheduled view function.

remove_middleware(middleware)

Remove the given WSGI middleware layer.

Parameters:middleware – A middleware object. This must be the an object that was previouslly passed to add_middleware().
request_class

Class to use to instantiate request objects

alias of Request

requestcontext(url='/', environ=None, wsgi_input=b'', middleware=True, **kwargs)

Return the global fresco.requestcontext.RequestContext instance, populated with a new request object modelling default WSGI environ values.

Synopsis:

>>> app = FrescoApp()
>>> with app.requestcontext('http://www.example.com/view') as c:
...     print c.request.url
...
http://www.example.com/view

Note that url must be properly URL encoded.

Parameters:
  • url – The URL for the request, eg /index.html or /search?q=foo.
  • environ – values to pass into the WSGI environ dict
  • wsgi_input – The input stream to use in the wsgi.input key of the environ dict
  • middleware – If False the middleware stack will not be invoked. Disabling the middleware can speed up the execution considerably, but it will no longer give an accurate simulation of a real HTTP request.
  • kwargs – additional keyword arguments will be passed into the WSGI request environment
urlfor(viewspec, *args, **kwargs)

Return the url for the given view name or function spec

Parameters:
  • viewspec – a view name, a reference in the form 'package.module.viewfunction', or the view callable itself.
  • _scheme – the URL scheme to use (eg ‘https’ or ‘http’).
  • _netloc – the network location to use (eg ‘localhost:8000’).
  • _script_name – the SCRIPT_NAME path component
  • _query – any query parameters, as a dict or list of (key, value) tuples.
  • _fragment – a URL fragment to append.

All other arguments or keyword args are fed to the pathfor method of the pattern.

view_method_not_found(valid_methods)

Return a 405 Method Not Allowed response.

Called when a view matched the pattern but no HTTP methods matched.

fresco.core.urlfor(*args, **kwargs)

Convenience wrapper around urlfor().