# HG changeset patch # User k0s # Date 1264352439 18000 # Node ID d1067d921e977f3c290675fc18d39715bc582740 initial import of lxml middleware diff -r 000000000000 -r d1067d921e97 example.ini --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example.ini Sun Jan 24 12:00:39 2010 -0500 @@ -0,0 +1,21 @@ +#!/usr/bin/env paster + +[DEFAULT] +debug = true +email_to = k0scist@gmail.com +smtp_server = localhost +error_email_from = paste@localhost + +[server:main] +use = egg:Paste#http +host = 0.0.0.0 +port = 5397 + +[composite:main] +use = egg:Paste#urlmap +/ = lxmlmiddleware + +set debug = false + +[app:lxmlmiddleware] +paste.app_factory = lxmlmiddleware.example:factory diff -r 000000000000 -r d1067d921e97 lxmlmiddleware/__init__.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lxmlmiddleware/__init__.py Sun Jan 24 12:00:39 2010 -0500 @@ -0,0 +1,2 @@ +# +from middleware import LXMLMiddleware diff -r 000000000000 -r d1067d921e97 lxmlmiddleware/example.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lxmlmiddleware/example.py Sun Jan 24 12:00:39 2010 -0500 @@ -0,0 +1,22 @@ +from webob import Response +from lxml import etree +from lxmlmiddleware.middleware import LXMLMiddleware +from paste.httpexceptions import HTTPExceptionHandler + +def example_app(environ, start_response): + return Response('Hello, world!')(environ, start_response) + +class ExampleMiddleware(LXMLMiddleware): + def manipulate(self, environ, tree): + tree.append(etree.XML('
How are you doing?
')) + return tree + +class ExampleMiddleware2(LXMLMiddleware): + def manipulate(self, environ, tree): + tree.append(etree.XML("
I'm doing find, thank you!
")) + return tree + +def factory(global_conf, **app_conf): + """create a webob view and wrap it in middleware""" + return HTTPExceptionHandler(ExampleMiddleware2(ExampleMiddleware(example_app))) + diff -r 000000000000 -r d1067d921e97 lxmlmiddleware/middleware.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lxmlmiddleware/middleware.py Sun Jan 24 12:00:39 2010 -0500 @@ -0,0 +1,42 @@ +""" +LXML middleware +""" + +from lxml import etree + +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 + + # get the response + response = self.app(environ, start_response) + + # get the DOM, if not already made + if not isinstance(response, etree._Element): + response = etree.fromstring(''.join(response)) + + # manipulate the DOM + response = self.manipulate(environ, response) + + # recompose the DOM if the last in the chain + if environ['lxml.recomposer'] is self: + response = [ etree.tostring(response) ] + + # return the response + return response + + def manipulate(self, environ, tree): + """manipulate the DOM; should return an etree._Element""" + return tree + diff -r 000000000000 -r d1067d921e97 setup.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/setup.py Sun Jan 24 12:00:39 2010 -0500 @@ -0,0 +1,32 @@ +from setuptools import setup, find_packages +import sys, os + +version = "0.0" + +setup(name='lxmlmiddleware', + version=version, + description="stack of middleware to deal with a response as a LXML etree", + long_description=""" +""", + classifiers=[], # Get strings from http://www.python.org/pypi?%3Aaction=list_classifiers + author='Jeff Hammel', + author_email='k0scist@gmail.com', + url='http://k0s.org', + license="", + packages=find_packages(exclude=['ez_setup', 'examples', 'tests']), + include_package_data=True, + zip_safe=False, + install_requires=[ + # -*- Extra requirements: -*- + 'WebOb', + 'Paste', + 'PasteScript', + 'lxml', + ], + entry_points=""" + # -*- Entry points: -*- + [paste.app_factory] + example = lxmlmiddleware.example:factory + """, + ) +