# HG changeset patch # User k0s # Date 1265590089 18000 # Node ID a9ddcfc7c4e861da7bbaa3f6d070f3d09c454de5 # Parent 051d4d39b4b9c0b516ef555c28fcc9dd3fae1ae6 added transformer just of content-type name diff -r 051d4d39b4b9 -r a9ddcfc7c4e8 contenttransformer/app.py --- 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) diff -r 051d4d39b4b9 -r a9ddcfc7c4e8 contenttransformer/transformers.py --- 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): diff -r 051d4d39b4b9 -r a9ddcfc7c4e8 example.ini --- 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 diff -r 051d4d39b4b9 -r a9ddcfc7c4e8 example/foo --- /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