view 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
line wrap: on
line source

import docutils.core
import subprocess
from webob import Request, Response

class Transformer(object):
    """abstract base class for transformer objects"""
    def __init__(self, content):
        self.content = 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)
        image, _ = process.communicate(self.content)
        return ('image/png', image)

class RestructuredText(Transformer):
    settings = { 'report_level': 5 }

    def transform(self, request):
        """template: genshi(?) template to use (???)"""
        html = docutils.core.publish_string(self.content,
                                            writer_name='html',
                                            settings_overrides=self.settings)
        return ('text/html', html)