view 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
line wrap: on
line source

import os
import sys
from fnmatch import fnmatch
from paste.fileapp import FileApp
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)"""
        self.types = types
        self.kwargs = kwargs # intended to be arguments to the xformers
        for blah in iter_entry_points('content_transformers'):
            try:
                self.transformers['foo'] = entry_point.load()
            except:
                raise
        for pattern, transformer_name in self.types:
            assert transformer_name in self.transformers

    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:
            if fnmatch(filename, pattern):
                content = file(path).read()
                return self.transformers[transformer_name](content)
        return FileApp(path)