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	Sun Feb 07 18:52:40 2010 -0500
+++ b/contenttransformer/app.py	Sun Feb 07 19:03:15 2010 -0500
@@ -1,6 +1,7 @@
 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
 
@@ -23,8 +24,9 @@
         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)
 
 
--- a/contenttransformer/transformers.py	Sun Feb 07 18:52:40 2010 -0500
+++ b/contenttransformer/transformers.py	Sun Feb 07 19:03:15 2010 -0500
@@ -4,8 +4,9 @@
 
 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)"""
@@ -13,8 +14,15 @@
 
     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):
--- a/setup.py	Sun Feb 07 18:52:40 2010 -0500
+++ b/setup.py	Sun Feb 07 19:03:15 2010 -0500
@@ -1,7 +1,7 @@
 from setuptools import setup, find_packages
 import sys, os
 
-version = "0.2"
+version = "0.3"
 
 setup(name='contenttransformer',
       version=version,