Mercurial > hg > contenttransformer
changeset 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 | 85f84b928768 |
children | 805cadc2b825 |
files | contenttransformer/transformers.py setup.py |
diffstat | 2 files changed, 26 insertions(+), 18 deletions(-) [+] |
line wrap: on
line diff
--- a/contenttransformer/transformers.py Sun Jan 17 19:53:57 2010 -0500 +++ b/contenttransformer/transformers.py Wed Feb 03 17:56:24 2010 -0500 @@ -1,28 +1,36 @@ import docutils.core import subprocess -from webob import Response +from webob import Request, Response + +class Transformer(object): + """abstract base class for transformer objects""" + def __init__(self, content): + self.content = content -class Graphviz(object): - def __init__(self, content): + def transform(self, request): + """returns a tuple of (body, content-type)""" + raise NotImplementedError + + def __call__(self, environ, start_response): + request = Request(environ) + content_type, body = self.transform(request) + return Response(content_type=content_type, body=body)(environ, start_response) + +class Graphviz(Transformer): + def transform(self, request): """create a Graphviz object""" process = subprocess.Popen(['dot', '-Tpng'], stdin=subprocess.PIPE, stdout=subprocess.PIPE) - self.image, _ = process.communicate(content) + image, _ = process.communicate(self.content) + return ('image/png', image) - def __call__(self, environ, start_response): - """return a WSGI response""" - return Response(content_type='image/png', body=self.image)(environ, start_response) - -class RestructuredText(object): +class RestructuredText(Transformer): settings = { 'report_level': 5 } - def __init__(self, content): + def transform(self, request): """template: genshi(?) template to use (???)""" - self.html = docutils.core.publish_string(content, - writer_name='html', - settings_overrides=self.settings) - - def __call__(self, environ, start_response): - """return a WSGI response""" - return Response(content_type='text/html', body=self.html)(environ, start_response) + html = docutils.core.publish_string(self.content, + writer_name='html', + settings_overrides=self.settings) + return ('text/html', html)