Mercurial > hg > lxmlmiddleware
view lxmlmiddleware/middleware.py @ 3:ca1f58f5bad4
pass a webob request and response to the manipulate method, API change, bumping version
author | k0s <k0scist@gmail.com> |
---|---|
date | Tue, 26 Jan 2010 22:57:00 -0500 |
parents | bb7d50d54cb6 |
children |
line wrap: on
line source
""" LXML middleware """ import lxml.html from lxml import etree from webob import Request, Response class LXMLMiddleware(object): """ abstract base class; inherit from and implement the manipulate method """ def __init__(self, app): self.app = app ### methods dealing with HTTP def __call__(self, environ, start_response): # set up to recompose on the way out if not 'lxml.recomposer' in environ: environ['lxml.recomposer'] = self request = Request(environ) # get the response response = request.get_response(self.app) response.decode_content() # get the DOM, if not already made # TODO: check response.content_type if not 'lxml.etree' in environ: try: environ['lxml.etree'] = etree.fromstring(response.body) except etree.XMLSyntaxError: # not XML environ.pop('lxml.recomposer') return response(environ, start_response) # manipulate the DOM environ['lxml.etree'] = self.manipulate(request, response, environ['lxml.etree']) # recompose the DOM if the last in the chain if environ['lxml.recomposer'] is self: response.body = lxml.html.tostring(environ['lxml.etree']) # return the response return response(environ, start_response) def manipulate(self, environ, response, tree): """manipulate the DOM; should return an etree._Element""" return tree