# `paste.wsgilib` – Miscellaneous WSGI utility functions

A module of many disparate routines.

## Module Contents

paste.wsgilib.add_close(app_iterable, close_func)

  An an iterable that iterates over app_iter, then calls close_func.

paste.wsgilib.add_start_close(app_iterable, start_func, close_func=None)

  An an iterable that iterates over app_iter, calls start_func before the first item is returned, then calls close_func at the end.

paste.wsgilib.chained_app_iters(*chained)

  Chains several app_iters together, also delegating .close() to each of them.

class paste.wsgilib.encode_unicode_app_iter(app_iterable, encoding='utf-8', errors='strict')

  Encodes an app_iterable’s unicode responses as strings

paste.wsgilib.catch_errors(application, environ, start_response, error_callback, ok_callback=None)

  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.

paste.wsgilib.catch_errors_app(application, environ, start_response, error_callback_app, ok_callback=None, catch=<class 'Exception'>)

  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.

paste.wsgilib.raw_interactive(application, path='', raise_on_wsgi_error=False, **environ)

  Runs the application in a fake environment.

paste.wsgilib.interactive(*args, **kw)

  Runs the application interatively, wrapping raw_interactive but returning the output in a formatted way.

paste.wsgilib.dump_environ(environ, start_response)

  Application which simply dumps the current environment variables out as a plain text response.

paste.wsgilib.intercept_output(environ, application, conditional=None, start_response=None)

  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