A module of many disparate routines.
An an iterable that iterates over app_iter, then calls close_func.
An an iterable that iterates over app_iter, calls start_func before the first item is returned, then calls close_func at the end.
Chains several app_iters together, also delegating .close() to each of them.
Encodes an app_iterable’s unicode responses as strings
Runs the application, and returns the application iterator (which should be passed upstream). If an error occurs then error_callback will be called with exc_info as its sole argument. If no errors occur and ok_callback is given, then it will be called with no arguments.
Like catch_errors, except error_callback_app should be a callable that will receive three arguments – environ, start_response, and exc_info. It should call start_response (with the exc_info argument!) and return an iterator.
Runs the application in a fake environment.
Runs the application interatively, wrapping raw_interactive but returning the output in a formatted way.
Application which simply dumps the current environment variables out as a plain text response.
Runs application with environ and captures status, headers, and body. None are sent on; you must send them on yourself (unlike capture_output)
Typically this is used like:
def dehtmlifying_middleware(application):
def replacement_app(environ, start_response):
status, headers, body = intercept_output(
environ, application)
start_response(status, headers)
content_type = header_value(headers, 'content-type')
if (not content_type
or not content_type.startswith('text/html')):
return [body]
body = re.sub(r'<.*?>', '', body)
return [body]
return replacement_app
A third optional argument conditional should be a function that takes conditional(status, headers) and returns False if the request should not be intercepted. In that case start_response will be called and (None, None, app_iter) will be returned. You must detect that in your code and return the app_iter, like:
def dehtmlifying_middleware(application):
def replacement_app(environ, start_response):
status, headers, body = intercept_output(
environ, application,
lambda s, h: header_value(headers, 'content-type').startswith('text/html'),
start_response)
if status is None:
return body
start_response(status, headers)
body = re.sub(r'<.*?>', '', body)
return [body]
return replacement_app