Mercurial > hg > contenttransformer
changeset 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 | 1a267297f779 |
children | 349a80ce9def |
files | contenttransformer/app.py setup.py |
diffstat | 2 files changed, 15 insertions(+), 9 deletions(-) [+] |
line wrap: on
line diff
--- a/contenttransformer/app.py Thu Jan 14 00:16:34 2010 -0500 +++ b/contenttransformer/app.py Sun Jan 17 19:47:32 2010 -0500 @@ -5,7 +5,6 @@ from pkg_resources import iter_entry_points class FileTypeTransformer(object): - transformers = {} def __init__(self, *types, **kwargs): """types is a list of two-tuples: glob pattern (string), transformer name (string, name of entry point)""" @@ -14,16 +13,12 @@ # intended to be arguments to the xformers # XXX unused self.kwargs = kwargs - - for entry_point in iter_entry_points('content_transformers'): - try: - self.transformers[entry_point.name] = entry_point.load() - except: - raise # XXX + + self.transformers = transformers() for pattern, transformer_name in self.types: assert transformer_name in self.transformers - def __call__(self, path): + 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: @@ -31,3 +26,14 @@ content = file(path).read() return self.transformers[transformer_name](content) return FileApp(path) + + +def transformers(): + transformers = {} # XXX could cache + for entry_point in iter_entry_points('content_transformers'): + try: + transformers[entry_point.name] = entry_point.load() + except: + raise # XXX + return transformers +