comparison contenttransformer/app.py @ 0:29805d442afc

initial commit of contenttransformer; still in the stub stage
author k0s <k0scist@gmail.com>
date Mon, 11 Jan 2010 11:32:24 -0500
parents
children aa491070ccf3
comparison
equal deleted inserted replaced
-1:000000000000 0:29805d442afc
1 import os
2 import sys
3 from fnmatch import fnmatch
4 from paste.fileapp import FileApp
5 from pkg_resources import iter_entry_points
6
7 class FileTypeTransformer(object):
8 transformers = {}
9
10 def __init__(self, *types, **kwargs):
11 """types is a list of two-tuples: glob pattern (string), transformer name (string, name of entry point)"""
12 self.types = types
13 self.kwargs = kwargs # intended to be arguments to the xformers
14 for blah in iter_entry_points('content_transformers'):
15 try:
16 self.transformers['foo'] = entry_point.load()
17 except:
18 raise
19 for pattern, transformer_name in self.types:
20 assert transformer_name in self.transformers
21
22 def __call__(self, path):
23 """this should return something that is callable with (environ, start_response) to return a response; the transformer thing"""
24 filename = os.path.basename(path)
25 for pattern, transformer_name in self.types:
26 if fnmatch(filename, pattern):
27 content = file(path).read()
28 return self.transformers[transformer_name](content)
29 return FileApp(path)