changeset 0:d1067d921e97

initial import of lxml middleware
author k0s <k0scist@gmail.com>
date Sun, 24 Jan 2010 12:00:39 -0500
parents
children 2bf25337048f
files example.ini lxmlmiddleware/__init__.py lxmlmiddleware/example.py lxmlmiddleware/middleware.py setup.py
diffstat 5 files changed, 119 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /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
--- /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
--- /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('<html><body>Hello, world!</body></html>')(environ, start_response)
+
+class ExampleMiddleware(LXMLMiddleware):
+    def manipulate(self, environ, tree):
+        tree.append(etree.XML('<div><i>How are you doing?</i></div>'))        
+        return tree
+    
+class ExampleMiddleware2(LXMLMiddleware):
+    def manipulate(self, environ, tree):
+        tree.append(etree.XML("<div><b>I'm doing find, thank you!</b></div>"))
+        return tree
+
+def factory(global_conf, **app_conf):
+    """create a webob view and wrap it in middleware"""
+    return HTTPExceptionHandler(ExampleMiddleware2(ExampleMiddleware(example_app)))
+    
--- /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
+
--- /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
+      """,
+      )
+