annotate lxmlmiddleware/middleware.py @ 0:d1067d921e97

initial import of lxml middleware
author k0s <k0scist@gmail.com>
date Sun, 24 Jan 2010 12:00:39 -0500
parents
children 2bf25337048f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
d1067d921e97 initial import of lxml middleware
k0s <k0scist@gmail.com>
parents:
diff changeset
1 """
d1067d921e97 initial import of lxml middleware
k0s <k0scist@gmail.com>
parents:
diff changeset
2 LXML middleware
d1067d921e97 initial import of lxml middleware
k0s <k0scist@gmail.com>
parents:
diff changeset
3 """
d1067d921e97 initial import of lxml middleware
k0s <k0scist@gmail.com>
parents:
diff changeset
4
d1067d921e97 initial import of lxml middleware
k0s <k0scist@gmail.com>
parents:
diff changeset
5 from lxml import etree
d1067d921e97 initial import of lxml middleware
k0s <k0scist@gmail.com>
parents:
diff changeset
6
d1067d921e97 initial import of lxml middleware
k0s <k0scist@gmail.com>
parents:
diff changeset
7 class LXMLMiddleware(object):
d1067d921e97 initial import of lxml middleware
k0s <k0scist@gmail.com>
parents:
diff changeset
8 """
d1067d921e97 initial import of lxml middleware
k0s <k0scist@gmail.com>
parents:
diff changeset
9 abstract base class; inherit from and implement the manipulate method
d1067d921e97 initial import of lxml middleware
k0s <k0scist@gmail.com>
parents:
diff changeset
10 """
d1067d921e97 initial import of lxml middleware
k0s <k0scist@gmail.com>
parents:
diff changeset
11
d1067d921e97 initial import of lxml middleware
k0s <k0scist@gmail.com>
parents:
diff changeset
12 def __init__(self, app):
d1067d921e97 initial import of lxml middleware
k0s <k0scist@gmail.com>
parents:
diff changeset
13 self.app = app
d1067d921e97 initial import of lxml middleware
k0s <k0scist@gmail.com>
parents:
diff changeset
14
d1067d921e97 initial import of lxml middleware
k0s <k0scist@gmail.com>
parents:
diff changeset
15 ### methods dealing with HTTP
d1067d921e97 initial import of lxml middleware
k0s <k0scist@gmail.com>
parents:
diff changeset
16 def __call__(self, environ, start_response):
d1067d921e97 initial import of lxml middleware
k0s <k0scist@gmail.com>
parents:
diff changeset
17
d1067d921e97 initial import of lxml middleware
k0s <k0scist@gmail.com>
parents:
diff changeset
18 # set up to recompose on the way out
d1067d921e97 initial import of lxml middleware
k0s <k0scist@gmail.com>
parents:
diff changeset
19 if not 'lxml.recomposer' in environ:
d1067d921e97 initial import of lxml middleware
k0s <k0scist@gmail.com>
parents:
diff changeset
20 environ['lxml.recomposer'] = self
d1067d921e97 initial import of lxml middleware
k0s <k0scist@gmail.com>
parents:
diff changeset
21
d1067d921e97 initial import of lxml middleware
k0s <k0scist@gmail.com>
parents:
diff changeset
22 # get the response
d1067d921e97 initial import of lxml middleware
k0s <k0scist@gmail.com>
parents:
diff changeset
23 response = self.app(environ, start_response)
d1067d921e97 initial import of lxml middleware
k0s <k0scist@gmail.com>
parents:
diff changeset
24
d1067d921e97 initial import of lxml middleware
k0s <k0scist@gmail.com>
parents:
diff changeset
25 # get the DOM, if not already made
d1067d921e97 initial import of lxml middleware
k0s <k0scist@gmail.com>
parents:
diff changeset
26 if not isinstance(response, etree._Element):
d1067d921e97 initial import of lxml middleware
k0s <k0scist@gmail.com>
parents:
diff changeset
27 response = etree.fromstring(''.join(response))
d1067d921e97 initial import of lxml middleware
k0s <k0scist@gmail.com>
parents:
diff changeset
28
d1067d921e97 initial import of lxml middleware
k0s <k0scist@gmail.com>
parents:
diff changeset
29 # manipulate the DOM
d1067d921e97 initial import of lxml middleware
k0s <k0scist@gmail.com>
parents:
diff changeset
30 response = self.manipulate(environ, response)
d1067d921e97 initial import of lxml middleware
k0s <k0scist@gmail.com>
parents:
diff changeset
31
d1067d921e97 initial import of lxml middleware
k0s <k0scist@gmail.com>
parents:
diff changeset
32 # recompose the DOM if the last in the chain
d1067d921e97 initial import of lxml middleware
k0s <k0scist@gmail.com>
parents:
diff changeset
33 if environ['lxml.recomposer'] is self:
d1067d921e97 initial import of lxml middleware
k0s <k0scist@gmail.com>
parents:
diff changeset
34 response = [ etree.tostring(response) ]
d1067d921e97 initial import of lxml middleware
k0s <k0scist@gmail.com>
parents:
diff changeset
35
d1067d921e97 initial import of lxml middleware
k0s <k0scist@gmail.com>
parents:
diff changeset
36 # return the response
d1067d921e97 initial import of lxml middleware
k0s <k0scist@gmail.com>
parents:
diff changeset
37 return response
d1067d921e97 initial import of lxml middleware
k0s <k0scist@gmail.com>
parents:
diff changeset
38
d1067d921e97 initial import of lxml middleware
k0s <k0scist@gmail.com>
parents:
diff changeset
39 def manipulate(self, environ, tree):
d1067d921e97 initial import of lxml middleware
k0s <k0scist@gmail.com>
parents:
diff changeset
40 """manipulate the DOM; should return an etree._Element"""
d1067d921e97 initial import of lxml middleware
k0s <k0scist@gmail.com>
parents:
diff changeset
41 return tree
d1067d921e97 initial import of lxml middleware
k0s <k0scist@gmail.com>
parents:
diff changeset
42