[mod_python] Output Filters Redeaux

Lee Brown administrator at leebrown.org
Mon Apr 17 12:36:34 EDT 2006

This is the code for a Mod Python output filter that performs an XSLT
transform on XML data that I published a few weeks ago.  I've restructured
it as an example application of the filter template:
from mod_python import apache
from cStringIO import StringIO
import lxml.etree
import time
name = 'LXML'
timestring = 'Time to process XML/XSLT files using %s: %.3f milliseconds (%s
pages per second) [%s pass]\n'
xsltfile = open('c:/webdev/sites/crashtest/templates/template.xslt', 'rU')
styledoc = lxml.etree.parse(StringIO(xsltfile.read()))
transformer = lxml.etree.XSLT(styledoc)
def outputfilter (filter):
        streambuffer = filter.req.streambuffer
    except AttributeError:
        filter.req.streambuffer = StringIO()
        streambuffer = filter.req.streambuffer
        filter.req.start = time.clock()
        filter.req.passes = 0
    streamlet = filter.read()
    while streamlet:
        filter.req.passes += 1
        streambuffer.write(streamlet.replace('\r\n', '\n'))
        streamlet = filter.read()
    if streamlet is None:
        doc = lxml.etree.parse(streambuffer)
        result = str(transformer(doc))
        end = time.clock()
        start = filter.req.start
        passes = filter.req.passes
        ms = (end-start)*1000
        pps = int(1/(end-start))
        timestamp = timestring % (name, ms, pps, passes)
        filter.write(result.replace('benchmark', timestamp))


Best Regards, 
Lee E. Brown 
(leebrown at leebrown.org) 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mm_cfg_has_not_been_edited_to_set_host_domains/pipermail/mod_python/attachments/20060417/e494c7ad/attachment.html

More information about the Mod_python mailing list