changeset 10:a9ddcfc7c4e8

added transformer just of content-type name
author k0s <k0scist@gmail.com>
date Sun, 07 Feb 2010 19:48:09 -0500
parents 051d4d39b4b9
children ba52425d1250
files contenttransformer/app.py contenttransformer/transformers.py example.ini example/foo
diffstat 4 files changed, 20 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/contenttransformer/app.py	Sun Feb 07 19:03:15 2010 -0500
+++ b/contenttransformer/app.py	Sun Feb 07 19:48:09 2010 -0500
@@ -4,6 +4,7 @@
 from mimetypes import guess_type
 from paste.fileapp import FileApp
 from pkg_resources import iter_entry_points
+from transformers import ContentTypeChanger
 
 class FileTypeTransformer(object):
 
@@ -14,9 +15,11 @@
         # intended to be arguments to the xformers
         # XXX unused
         self.kwargs = kwargs
-
+        
         self.transformers = transformers()
         for pattern, transformer_name in self.types:
+            if '/' in transformer_name:
+                continue
             assert transformer_name in self.transformers
 
     def __call__(self, path): 
@@ -26,6 +29,12 @@
             if fnmatch(filename, pattern):
                 content_type, _ = guess_type(filename)
                 content = file(path).read()
+
+                # transform content type
+                # XXX hack: -> refactor
+                if '/' in transformer_name:
+                    return ContentTypeChanger(content, content_type, transformer_name)
+                    
                 return self.transformers[transformer_name](content, content_type)
         return FileApp(path)
 
--- a/contenttransformer/transformers.py	Sun Feb 07 19:03:15 2010 -0500
+++ b/contenttransformer/transformers.py	Sun Feb 07 19:48:09 2010 -0500
@@ -22,7 +22,14 @@
             return Response(content_type=self.content_type, body=self.content)
         content_type, body = self.transform(request)
         return Response(content_type=content_type, body=body)
-        
+
+class ContentTypeChanger(Transformer):
+    def __init__(self, content, from_type, to_type):
+        self.to_type = to_type
+        Transformer.__init__(self, content, from_type)
+
+    def transform(self, request):
+        return (self.to_type, self.content)
 
 class Graphviz(Transformer):
     def transform(self, request):
--- a/example.ini	Sun Feb 07 19:03:15 2010 -0500
+++ b/example.ini	Sun Feb 07 19:48:09 2010 -0500
@@ -20,4 +20,4 @@
 [app:contenttransformer]
 paste.app_factory = contenttransformer.factory:factory
 transformer.directory = %(here)s/example
-transformer.transforms = *.gv.txt=Graphviz,*.txt=ReST
\ No newline at end of file
+transformer.transforms = *.gv.txt=Graphviz,*.txt=ReST,foo=text/plain
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/example/foo	Sun Feb 07 19:48:09 2010 -0500
@@ -0,0 +1,1 @@
+bar
\ No newline at end of file