changeset 9:051d4d39b4b9

* separate out get_response to its own function * keep track of raw content_type for later usage (muahaha)
author k0s <k0scist@gmail.com>
date Sun, 07 Feb 2010 19:03:15 -0500
parents 805cadc2b825
children a9ddcfc7c4e8
files contenttransformer/app.py contenttransformer/transformers.py setup.py
diffstat 3 files changed, 14 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/contenttransformer/app.py
+++ b/contenttransformer/app.py
@@ -1,11 +1,12 @@
 import os
 import sys
 from fnmatch import fnmatch
+from mimetypes import guess_type
 from paste.fileapp import FileApp
 from pkg_resources import iter_entry_points
 
 class FileTypeTransformer(object):
 
     def __init__(self, *types, **kwargs):
         """types is a list of two-tuples: glob pattern (string), transformer name (string, name of entry point)"""
         self.types = types
@@ -18,18 +19,19 @@ class FileTypeTransformer(object):
         for pattern, transformer_name in self.types:
             assert transformer_name in self.transformers
 
     def __call__(self, path): 
         """this should return something that is callable with (environ, start_response) to return a response; the transformer thing"""
         filename = os.path.basename(path)
         for pattern, transformer_name in self.types:
             if fnmatch(filename, pattern):
+                content_type, _ = guess_type(filename)
                 content = file(path).read()
-                return self.transformers[transformer_name](content)
+                return self.transformers[transformer_name](content, content_type)
         return FileApp(path)
 
 
 def transformers():
     transformers = {} # XXX could cache
     for entry_point in iter_entry_points('content_transformers'):
         try:
             transformers[entry_point.name] = entry_point.load()
--- a/contenttransformer/transformers.py
+++ b/contenttransformer/transformers.py
@@ -1,25 +1,33 @@
 import docutils.core
 import subprocess
 from webob import Request, Response
 
 class Transformer(object):
     """abstract base class for transformer objects"""
-    def __init__(self, content):
+    def __init__(self, content, content_type):
         self.content = content
+        self.content_type = content_type
 
     def transform(self, request):
         """returns a tuple of (body, content-type)"""
         raise NotImplementedError
 
     def __call__(self, environ, start_response):
         request = Request(environ)
+        response = self.get_response(request)
+        return response(environ, start_response)
+
+    def get_response(self, request):
+        if request.GET.get('format') == 'raw':
+            return Response(content_type=self.content_type, body=self.content)
         content_type, body = self.transform(request)
-        return Response(content_type=content_type, body=body)(environ, start_response)
+        return Response(content_type=content_type, body=body)
+        
 
 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)
--- a/setup.py
+++ b/setup.py
@@ -1,12 +1,12 @@
 from setuptools import setup, find_packages
 import sys, os
 
-version = "0.2"
+version = "0.3"
 
 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',