Mercurial > hg > contenttransformer
comparison contenttransformer/transformers.py @ 23:9cf78a1c7373
python3
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Tue, 03 Nov 2020 10:55:18 -0800 |
parents | afd11b758da0 |
children |
comparison
equal
deleted
inserted
replaced
22:ee3eadc97a41 | 23:9cf78a1c7373 |
---|---|
1 import docutils.core | 1 import docutils.core |
2 import subprocess | 2 import subprocess |
3 from utils import import_path | 3 from .utils import import_path |
4 from webob import Request, Response | 4 from webob import Request, Response |
5 | 5 |
6 import genshi | 6 import genshi |
7 from genshi.template import MarkupTemplate | 7 from genshi.template import MarkupTemplate |
8 | 8 |
33 Transformer.__init__(self, content, from_type) | 33 Transformer.__init__(self, content, from_type) |
34 | 34 |
35 def transform(self, request): | 35 def transform(self, request): |
36 return (self.to_type, self.content) | 36 return (self.to_type, self.content) |
37 | 37 |
38 | |
38 class Graphviz(Transformer): | 39 class Graphviz(Transformer): |
39 content_types = { 'png': 'image/png', | 40 content_types = { 'png': 'image/png', |
40 'svg': 'image/svg+xml' } | 41 'svg': 'image/svg+xml' } |
41 | 42 |
42 def __init__(self, content, content_type, format='png'): | 43 def __init__(self, content, content_type, format='png'): |
43 self.format=format | 44 self.format=format |
44 Transformer.__init__(self, content, content_type) | 45 Transformer.__init__(self, content, content_type) |
45 | 46 |
46 def transform(self, request): | 47 def transform(self, request): |
47 """create a Graphviz object""" | 48 """create a Graphviz object""" |
48 _format = request.GET.get('format', self.format) | 49 _format = request.GET.get('format', self.format) |
49 assert _format in self.content_types, 'Unknown format: ' + _format | 50 assert _format in self.content_types, 'Unknown format: ' + _format |
50 process = subprocess.Popen(['dot', '-T' + _format], | 51 process = subprocess.Popen(['dot', '-T' + _format], |
51 stdin=subprocess.PIPE, | 52 stdin=subprocess.PIPE, |
52 stdout=subprocess.PIPE) | 53 stdout=subprocess.PIPE) |
53 image, _ = process.communicate(self.content) | 54 image, _ = process.communicate(self.content) |
54 return (self.content_types[_format], image) | 55 return (self.content_types[_format], image) |
56 | |
55 | 57 |
56 class RestructuredText(Transformer): | 58 class RestructuredText(Transformer): |
57 settings = { 'report_level': 5 } | 59 settings = { 'report_level': 5 } |
58 | 60 |
59 def transform(self, request): | 61 def transform(self, request): |
74 for path in modules: | 76 for path in modules: |
75 module = import_path(path) | 77 module = import_path(path) |
76 name = path.rsplit('.')[-1] | 78 name = path.rsplit('.')[-1] |
77 self.variables[name] = module | 79 self.variables[name] = module |
78 Transformer.__init__(self, content, content_type) | 80 Transformer.__init__(self, content, content_type) |
79 | 81 |
80 def transform(self, request): | 82 def transform(self, request): |
81 variables = dict(request=request) | 83 variables = dict(request=request) |
82 template = MarkupTemplate(self.content) | 84 template = MarkupTemplate(self.content) |
83 stream = template.generate(**variables) | 85 stream = template.generate(**variables) |
84 return ('text/html', stream.render('html', doctype='html')) | 86 return ('text/html', stream.render('html', doctype='html')) |
85 |