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
+++ b/contenttransformer/app.py
@@ -1,36 +1,45 @@
 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
+from transformers import ContentTypeChanger
 
 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
 
         # 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): 
         """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()
+
+                # 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)
 
 
 def transformers():
     transformers = {} # XXX could cache
     for entry_point in iter_entry_points('content_transformers'):
         try:
--- a/contenttransformer/transformers.py
+++ b/contenttransformer/transformers.py
@@ -17,17 +17,24 @@ class Transformer(object):
         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)
-        
+
+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):
         """create a Graphviz object"""
         process = subprocess.Popen(['dot', '-Tpng'],
                                    stdin=subprocess.PIPE,
                                    stdout=subprocess.PIPE)
         image, _ = process.communicate(self.content)
--- a/example.ini
+++ b/example.ini
@@ -15,9 +15,9 @@ port = 6666
 use = egg:Paste#urlmap
 / = contenttransformer
 
 set debug = false
 
 [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
new file mode 100644
--- /dev/null
+++ b/example/foo
@@ -0,0 +1,1 @@
+bar
\ No newline at end of file