Mercurial > hg > contenttransformer
comparison contenttransformer/app.py @ 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 | 5258325a496a |
children | a9ddcfc7c4e8 |
comparison
equal
deleted
inserted
replaced
8:805cadc2b825 | 9:051d4d39b4b9 |
---|---|
1 import os | 1 import os |
2 import sys | 2 import sys |
3 from fnmatch import fnmatch | 3 from fnmatch import fnmatch |
4 from mimetypes import guess_type | |
4 from paste.fileapp import FileApp | 5 from paste.fileapp import FileApp |
5 from pkg_resources import iter_entry_points | 6 from pkg_resources import iter_entry_points |
6 | 7 |
7 class FileTypeTransformer(object): | 8 class FileTypeTransformer(object): |
8 | 9 |
21 def __call__(self, path): | 22 def __call__(self, path): |
22 """this should return something that is callable with (environ, start_response) to return a response; the transformer thing""" | 23 """this should return something that is callable with (environ, start_response) to return a response; the transformer thing""" |
23 filename = os.path.basename(path) | 24 filename = os.path.basename(path) |
24 for pattern, transformer_name in self.types: | 25 for pattern, transformer_name in self.types: |
25 if fnmatch(filename, pattern): | 26 if fnmatch(filename, pattern): |
27 content_type, _ = guess_type(filename) | |
26 content = file(path).read() | 28 content = file(path).read() |
27 return self.transformers[transformer_name](content) | 29 return self.transformers[transformer_name](content, content_type) |
28 return FileApp(path) | 30 return FileApp(path) |
29 | 31 |
30 | 32 |
31 def transformers(): | 33 def transformers(): |
32 transformers = {} # XXX could cache | 34 transformers = {} # XXX could cache |