Mercurial > hg > lxmlmiddleware
annotate 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 |
rev | line source |
---|---|
0 | 1 """ |
2 LXML middleware | |
3 """ | |
4 | |
1 | 5 import lxml.html |
0 | 6 from lxml import etree |
2
bb7d50d54cb6
correct huge mistake in how i try to get the response
k0s <k0scist@gmail.com>
parents:
1
diff
changeset
|
7 from webob import Request, Response |
0 | 8 |
9 class LXMLMiddleware(object): | |
10 """ | |
11 abstract base class; inherit from and implement the manipulate method | |
12 """ | |
13 | |
14 def __init__(self, app): | |
15 self.app = app | |
16 | |
17 ### methods dealing with HTTP | |
18 def __call__(self, environ, start_response): | |
19 | |
20 # set up to recompose on the way out | |
21 if not 'lxml.recomposer' in environ: | |
22 environ['lxml.recomposer'] = self | |
23 | |
2
bb7d50d54cb6
correct huge mistake in how i try to get the response
k0s <k0scist@gmail.com>
parents:
1
diff
changeset
|
24 request = Request(environ) |
0 | 25 # get the response |
2
bb7d50d54cb6
correct huge mistake in how i try to get the response
k0s <k0scist@gmail.com>
parents:
1
diff
changeset
|
26 response = request.get_response(self.app) |
bb7d50d54cb6
correct huge mistake in how i try to get the response
k0s <k0scist@gmail.com>
parents:
1
diff
changeset
|
27 response.decode_content() |
0 | 28 |
29 # get the DOM, if not already made | |
2
bb7d50d54cb6
correct huge mistake in how i try to get the response
k0s <k0scist@gmail.com>
parents:
1
diff
changeset
|
30 # TODO: check response.content_type |
bb7d50d54cb6
correct huge mistake in how i try to get the response
k0s <k0scist@gmail.com>
parents:
1
diff
changeset
|
31 if not 'lxml.etree' in environ: |
1 | 32 try: |
2
bb7d50d54cb6
correct huge mistake in how i try to get the response
k0s <k0scist@gmail.com>
parents:
1
diff
changeset
|
33 environ['lxml.etree'] = etree.fromstring(response.body) |
1 | 34 except etree.XMLSyntaxError: # not XML |
35 environ.pop('lxml.recomposer') | |
2
bb7d50d54cb6
correct huge mistake in how i try to get the response
k0s <k0scist@gmail.com>
parents:
1
diff
changeset
|
36 return response(environ, start_response) |
0 | 37 |
38 # manipulate the DOM | |
3
ca1f58f5bad4
pass a webob request and response to the manipulate method, API change, bumping version
k0s <k0scist@gmail.com>
parents:
2
diff
changeset
|
39 environ['lxml.etree'] = self.manipulate(request, response, environ['lxml.etree']) |
0 | 40 |
41 # recompose the DOM if the last in the chain | |
42 if environ['lxml.recomposer'] is self: | |
2
bb7d50d54cb6
correct huge mistake in how i try to get the response
k0s <k0scist@gmail.com>
parents:
1
diff
changeset
|
43 response.body = lxml.html.tostring(environ['lxml.etree']) |
0 | 44 |
45 # return the response | |
2
bb7d50d54cb6
correct huge mistake in how i try to get the response
k0s <k0scist@gmail.com>
parents:
1
diff
changeset
|
46 return response(environ, start_response) |
bb7d50d54cb6
correct huge mistake in how i try to get the response
k0s <k0scist@gmail.com>
parents:
1
diff
changeset
|
47 |
0 | 48 |
3
ca1f58f5bad4
pass a webob request and response to the manipulate method, API change, bumping version
k0s <k0scist@gmail.com>
parents:
2
diff
changeset
|
49 def manipulate(self, environ, response, tree): |
0 | 50 """manipulate the DOM; should return an etree._Element""" |
51 return tree | |
52 |