Mercurial > hg > contenttransformer
comparison contenttransformer/app.py @ 4:5258325a496a
move transformers getting method to a standalone function
author | k0s <k0scist@gmail.com> |
---|---|
date | Sun, 17 Jan 2010 19:47:32 -0500 |
parents | aa491070ccf3 |
children | 051d4d39b4b9 |
comparison
equal
deleted
inserted
replaced
3:1a267297f779 | 4:5258325a496a |
---|---|
3 from fnmatch import fnmatch | 3 from fnmatch import fnmatch |
4 from paste.fileapp import FileApp | 4 from paste.fileapp import FileApp |
5 from pkg_resources import iter_entry_points | 5 from pkg_resources import iter_entry_points |
6 | 6 |
7 class FileTypeTransformer(object): | 7 class FileTypeTransformer(object): |
8 transformers = {} | |
9 | 8 |
10 def __init__(self, *types, **kwargs): | 9 def __init__(self, *types, **kwargs): |
11 """types is a list of two-tuples: glob pattern (string), transformer name (string, name of entry point)""" | 10 """types is a list of two-tuples: glob pattern (string), transformer name (string, name of entry point)""" |
12 self.types = types | 11 self.types = types |
13 | 12 |
14 # intended to be arguments to the xformers | 13 # intended to be arguments to the xformers |
15 # XXX unused | 14 # XXX unused |
16 self.kwargs = kwargs | 15 self.kwargs = kwargs |
17 | 16 |
18 for entry_point in iter_entry_points('content_transformers'): | 17 self.transformers = transformers() |
19 try: | |
20 self.transformers[entry_point.name] = entry_point.load() | |
21 except: | |
22 raise # XXX | |
23 for pattern, transformer_name in self.types: | 18 for pattern, transformer_name in self.types: |
24 assert transformer_name in self.transformers | 19 assert transformer_name in self.transformers |
25 | 20 |
26 def __call__(self, path): | 21 def __call__(self, path): |
27 """this should return something that is callable with (environ, start_response) to return a response; the transformer thing""" | 22 """this should return something that is callable with (environ, start_response) to return a response; the transformer thing""" |
28 filename = os.path.basename(path) | 23 filename = os.path.basename(path) |
29 for pattern, transformer_name in self.types: | 24 for pattern, transformer_name in self.types: |
30 if fnmatch(filename, pattern): | 25 if fnmatch(filename, pattern): |
31 content = file(path).read() | 26 content = file(path).read() |
32 return self.transformers[transformer_name](content) | 27 return self.transformers[transformer_name](content) |
33 return FileApp(path) | 28 return FileApp(path) |
29 | |
30 | |
31 def transformers(): | |
32 transformers = {} # XXX could cache | |
33 for entry_point in iter_entry_points('content_transformers'): | |
34 try: | |
35 transformers[entry_point.name] = entry_point.load() | |
36 except: | |
37 raise # XXX | |
38 return transformers | |
39 |