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, 25 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/contenttransformer/transformers.py
+++ b/contenttransformer/transformers.py
@@ -1,28 +1,36 @@
 import docutils.core
 import subprocess
-from webob import Response
+from webob import Request, Response
 
-class Graphviz(object):
+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)
-        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)
--- a/setup.py
+++ b/setup.py
@@ -1,12 +1,12 @@
 from setuptools import setup, find_packages
 import sys, os
 
-version = "0.1.1"
+version = "0.2"
 
 setup(name='contenttransformer',
       version=version,
       description="transform e.g. file data based on type to be served TTW",
       long_description="""
 """,
       classifiers=[], # Get strings from http://www.python.org/pypi?%3Aaction=list_classifiers
       author='Jeff Hammel',