Skip to main content

Tools to allow developers to cleanup web serialization objects (HTML, JSON, XHTML)

Project description

Provides middleware for detecting and correcting errors in web pages that are served via the standard WSGI protocol used by most Python web frameworks. By default, validation errors are logged to the “strainer.middleware” channel using the standard Python logging module.

You should read the documentation for your web framework to find out how to get the “WSGI application” that is responsible for serving your web site. In Pylons, for example, the following code could be added in the config/middleware.py file.

To add HTML/XHTML/XML well-formedness validation to your WSGI app:

>>> from strainer.middleware import WellformednessCheckerMiddleware
>>> app = WellformednessCheckerMiddleware(app)

This uses the expat parser to detect most syntax errors and mismatched tags, but it won’t perform stricter checks that the document structure matches the XHTML DTD, such as detecting disallowed child tags or attributes. For that you should install a recent version of lxml (e.g. “easy_install lxml”) and use XHTMLValidatorMiddleware instead, with code such as:

>>> from strainer.middleware import XHTMLValidatorMiddleware
>>> app = XHTMLValidatorMiddleware(app)

To add JSON validation to your WSGI app:

>>> from strainer.middleware import JSONValidatorMiddleware
>>> app = JSONValidatorMiddleware(app)

If your web framework doesn’t provide an alternative handler for the error messages that are logged to the “strainer.middleware” channel, you can have them printed to sys.stderr with:

>>> import logging
>>> logging.basicConfig()

To add automatic correction of common HTML and XHTML errors to your WSGI app:

>>> from strainer.middleware import XHTMLifyMiddleware
>>> app = XHTMLifyMiddleware(app)

This is somewhat experimental, but it will improve faster if people use it and email us bug reports…

As with all (or at least most) WSGI middleware, you can also combine them:

>>> app = XHTMLifyMiddleware(app)
>>> app = XHTMLValidatorMiddleware(app)
>>> app = JSONValidatorMiddleware(app)

The middleware in this package buffer the output internally (this violates the PEP 333 specification, but it seems unavoidable), so it is best to use them near the top of the middleware stack.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

strainer-0.1.1.tar.gz (21.1 kB view details)

Uploaded Source

Built Distribution

strainer-0.1.1-py2.6.egg (82.1 kB view details)

Uploaded Source

File details

Details for the file strainer-0.1.1.tar.gz.

File metadata

  • Download URL: strainer-0.1.1.tar.gz
  • Upload date:
  • Size: 21.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for strainer-0.1.1.tar.gz
Algorithm Hash digest
SHA256 6de9dd4a9fb95e60b63d7de612db899a3214690a6f71ee450a0abbcf266f1af0
MD5 bfc51de160b481f4b6fa0668940c7a39
BLAKE2b-256 9223ba9052a33e948ee39e3f58dd94820474e374c9333930c7f5ce3ac94d876b

See more details on using hashes here.

File details

Details for the file strainer-0.1.1-py2.6.egg.

File metadata

  • Download URL: strainer-0.1.1-py2.6.egg
  • Upload date:
  • Size: 82.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for strainer-0.1.1-py2.6.egg
Algorithm Hash digest
SHA256 fcc28498f3bef5bc39b8755efa6067ae54e47e10e244e42197af1b318dca6a86
MD5 7180e0b50978959e8bc826d65a292f09
BLAKE2b-256 f03768185c4c50ed9b5d268cda00bd8bc4528b285339ff2aec7475a025d34a70

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page