annotate contenttransformer/transformers.py @ 7:68643e72c749

use abstract base class for transformers so that behaviour can be streamlined
author k0s <k0scist@gmail.com>
date Wed, 03 Feb 2010 17:56:24 -0500
parents 1a267297f779
children 051d4d39b4b9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
29805d442afc initial commit of contenttransformer; still in the stub stage
k0s <k0scist@gmail.com>
parents:
diff changeset
1 import docutils.core
2
1e2c475015d8 restructured text now works
k0s <k0scist@gmail.com>
parents: 0
diff changeset
2 import subprocess
7
68643e72c749 use abstract base class for transformers so that behaviour can be streamlined
k0s <k0scist@gmail.com>
parents: 3
diff changeset
3 from webob import Request, Response
68643e72c749 use abstract base class for transformers so that behaviour can be streamlined
k0s <k0scist@gmail.com>
parents: 3
diff changeset
4
68643e72c749 use abstract base class for transformers so that behaviour can be streamlined
k0s <k0scist@gmail.com>
parents: 3
diff changeset
5 class Transformer(object):
68643e72c749 use abstract base class for transformers so that behaviour can be streamlined
k0s <k0scist@gmail.com>
parents: 3
diff changeset
6 """abstract base class for transformer objects"""
68643e72c749 use abstract base class for transformers so that behaviour can be streamlined
k0s <k0scist@gmail.com>
parents: 3
diff changeset
7 def __init__(self, content):
68643e72c749 use abstract base class for transformers so that behaviour can be streamlined
k0s <k0scist@gmail.com>
parents: 3
diff changeset
8 self.content = content
0
29805d442afc initial commit of contenttransformer; still in the stub stage
k0s <k0scist@gmail.com>
parents:
diff changeset
9
7
68643e72c749 use abstract base class for transformers so that behaviour can be streamlined
k0s <k0scist@gmail.com>
parents: 3
diff changeset
10 def transform(self, request):
68643e72c749 use abstract base class for transformers so that behaviour can be streamlined
k0s <k0scist@gmail.com>
parents: 3
diff changeset
11 """returns a tuple of (body, content-type)"""
68643e72c749 use abstract base class for transformers so that behaviour can be streamlined
k0s <k0scist@gmail.com>
parents: 3
diff changeset
12 raise NotImplementedError
68643e72c749 use abstract base class for transformers so that behaviour can be streamlined
k0s <k0scist@gmail.com>
parents: 3
diff changeset
13
68643e72c749 use abstract base class for transformers so that behaviour can be streamlined
k0s <k0scist@gmail.com>
parents: 3
diff changeset
14 def __call__(self, environ, start_response):
68643e72c749 use abstract base class for transformers so that behaviour can be streamlined
k0s <k0scist@gmail.com>
parents: 3
diff changeset
15 request = Request(environ)
68643e72c749 use abstract base class for transformers so that behaviour can be streamlined
k0s <k0scist@gmail.com>
parents: 3
diff changeset
16 content_type, body = self.transform(request)
68643e72c749 use abstract base class for transformers so that behaviour can be streamlined
k0s <k0scist@gmail.com>
parents: 3
diff changeset
17 return Response(content_type=content_type, body=body)(environ, start_response)
68643e72c749 use abstract base class for transformers so that behaviour can be streamlined
k0s <k0scist@gmail.com>
parents: 3
diff changeset
18
68643e72c749 use abstract base class for transformers so that behaviour can be streamlined
k0s <k0scist@gmail.com>
parents: 3
diff changeset
19 class Graphviz(Transformer):
68643e72c749 use abstract base class for transformers so that behaviour can be streamlined
k0s <k0scist@gmail.com>
parents: 3
diff changeset
20 def transform(self, request):
0
29805d442afc initial commit of contenttransformer; still in the stub stage
k0s <k0scist@gmail.com>
parents:
diff changeset
21 """create a Graphviz object"""
3
1a267297f779 graphviz now works, giving real version
k0s <k0scist@gmail.com>
parents: 2
diff changeset
22 process = subprocess.Popen(['dot', '-Tpng'],
1a267297f779 graphviz now works, giving real version
k0s <k0scist@gmail.com>
parents: 2
diff changeset
23 stdin=subprocess.PIPE,
1a267297f779 graphviz now works, giving real version
k0s <k0scist@gmail.com>
parents: 2
diff changeset
24 stdout=subprocess.PIPE)
7
68643e72c749 use abstract base class for transformers so that behaviour can be streamlined
k0s <k0scist@gmail.com>
parents: 3
diff changeset
25 image, _ = process.communicate(self.content)
68643e72c749 use abstract base class for transformers so that behaviour can be streamlined
k0s <k0scist@gmail.com>
parents: 3
diff changeset
26 return ('image/png', image)
0
29805d442afc initial commit of contenttransformer; still in the stub stage
k0s <k0scist@gmail.com>
parents:
diff changeset
27
7
68643e72c749 use abstract base class for transformers so that behaviour can be streamlined
k0s <k0scist@gmail.com>
parents: 3
diff changeset
28 class RestructuredText(Transformer):
0
29805d442afc initial commit of contenttransformer; still in the stub stage
k0s <k0scist@gmail.com>
parents:
diff changeset
29 settings = { 'report_level': 5 }
29805d442afc initial commit of contenttransformer; still in the stub stage
k0s <k0scist@gmail.com>
parents:
diff changeset
30
7
68643e72c749 use abstract base class for transformers so that behaviour can be streamlined
k0s <k0scist@gmail.com>
parents: 3
diff changeset
31 def transform(self, request):
0
29805d442afc initial commit of contenttransformer; still in the stub stage
k0s <k0scist@gmail.com>
parents:
diff changeset
32 """template: genshi(?) template to use (???)"""
7
68643e72c749 use abstract base class for transformers so that behaviour can be streamlined
k0s <k0scist@gmail.com>
parents: 3
diff changeset
33 html = docutils.core.publish_string(self.content,
68643e72c749 use abstract base class for transformers so that behaviour can be streamlined
k0s <k0scist@gmail.com>
parents: 3
diff changeset
34 writer_name='html',
68643e72c749 use abstract base class for transformers so that behaviour can be streamlined
k0s <k0scist@gmail.com>
parents: 3
diff changeset
35 settings_overrides=self.settings)
68643e72c749 use abstract base class for transformers so that behaviour can be streamlined
k0s <k0scist@gmail.com>
parents: 3
diff changeset
36 return ('text/html', html)