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