API docs: fresco.core

class fresco.core.FrescoApp(*args, **kwargs)[source]

Fresco application class.

add_middleware(middleware, *args, **kwargs)[source]

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)[source]

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

get_methods(request, path)[source]

Return the HTTP methods valid in routes to the given path

handle_http_error_response(request, response)[source]

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

insert_middleware(position, middleware, *args, **kwargs)[source]

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

make_wsgi_app(wsgi_app=None, use_middleware=True) Callable[[Dict[str, Any], Callable], Iterable[bytes]][source]

Return a WSGI (PEP-3333) compliant application that drives this FrescoApp object.

Parameters:
  • wsgi_app – if given, will be called in place of :meth:~`fresco.core.FrescoApp.view`(request) with :meth:~`fresco.core.FrescoApp.requestcontext`)

  • use_middleware – if True, the app’s middleware stack will be applied to the resulting WSGI app.

options

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'>)[source]

Register a process_exception hook function

process_exception_handlers: List[Tuple[Exception, Callable]]

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)[source]

Register a process_http_error_response hook function

process_http_error_response_handlers: List[Tuple[int, Callable]]

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)[source]

Register a process_request hook function

process_request_handlers: List[Callable]

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_request_once(func: Callable[[Request], Response | None]) Callable[[Request], Response | None][source]

Register a process_request hook function that is called only once

When running fresco with multiple worker threads/processes the hook function will be called at most once per worker.

process_response(func)[source]

Register a process_response hook function

process_response_handlers: List[Callable]

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)[source]

Register a process_teardown hook function

process_teardown_handlers: List[Callable]

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)[source]

Register a process_view hook function

process_view_handlers: List[Callable]

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)[source]

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)[source]

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)[source]

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)[source]

Return a 405 Method Not Allowed response.

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

fresco.core.urlfor(*args, **kwargs)[source]

Convenience wrapper around urlfor().