annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
29805d442afc initial commit of contenttransformer; still in the stub stage
k0s <k0scist@gmail.com>
parents:
diff changeset
9 def __init__(self, *types, **kwargs):
29805d442afc initial commit of contenttransformer; still in the stub stage
k0s <k0scist@gmail.com>
parents:
diff changeset
10 """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
11 self.types = types
1
aa491070ccf3 now works, just doesnt do anything, i dont think
k0s <k0scist@gmail.com>
parents: 0
diff changeset
12
aa491070ccf3 now works, just doesnt do anything, i dont think
k0s <k0scist@gmail.com>
parents: 0
diff changeset
13 # intended to be arguments to the xformers
aa491070ccf3 now works, just doesnt do anything, i dont think
k0s <k0scist@gmail.com>
parents: 0
diff changeset
14 # XXX unused
aa491070ccf3 now works, just doesnt do anything, i dont think
k0s <k0scist@gmail.com>
parents: 0
diff changeset
15 self.kwargs = kwargs
4
5258325a496a move transformers getting method to a standalone function
k0s <k0scist@gmail.com>
parents: 1
diff changeset
16
5258325a496a move transformers getting method to a standalone function
k0s <k0scist@gmail.com>
parents: 1
diff changeset
17 self.transformers = transformers()
0
29805d442afc initial commit of contenttransformer; still in the stub stage
k0s <k0scist@gmail.com>
parents:
diff changeset
18 for pattern, transformer_name in self.types:
29805d442afc initial commit of contenttransformer; still in the stub stage
k0s <k0scist@gmail.com>
parents:
diff changeset
19 assert transformer_name in self.transformers
29805d442afc initial commit of contenttransformer; still in the stub stage
k0s <k0scist@gmail.com>
parents:
diff changeset
20
4
5258325a496a move transformers getting method to a standalone function
k0s <k0scist@gmail.com>
parents: 1
diff changeset
21 def __call__(self, path):
0
29805d442afc initial commit of contenttransformer; still in the stub stage
k0s <k0scist@gmail.com>
parents:
diff changeset
22 """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
23 filename = os.path.basename(path)
29805d442afc initial commit of contenttransformer; still in the stub stage
k0s <k0scist@gmail.com>
parents:
diff changeset
24 for pattern, transformer_name in self.types:
29805d442afc initial commit of contenttransformer; still in the stub stage
k0s <k0scist@gmail.com>
parents:
diff changeset
25 if fnmatch(filename, pattern):
29805d442afc initial commit of contenttransformer; still in the stub stage
k0s <k0scist@gmail.com>
parents:
diff changeset
26 content = file(path).read()
29805d442afc initial commit of contenttransformer; still in the stub stage
k0s <k0scist@gmail.com>
parents:
diff changeset
27 return self.transformers[transformer_name](content)
29805d442afc initial commit of contenttransformer; still in the stub stage
k0s <k0scist@gmail.com>
parents:
diff changeset
28 return FileApp(path)
4
5258325a496a move transformers getting method to a standalone function
k0s <k0scist@gmail.com>
parents: 1
diff changeset
29
5258325a496a move transformers getting method to a standalone function
k0s <k0scist@gmail.com>
parents: 1
diff changeset
30
5258325a496a move transformers getting method to a standalone function
k0s <k0scist@gmail.com>
parents: 1
diff changeset
31 def transformers():
5258325a496a move transformers getting method to a standalone function
k0s <k0scist@gmail.com>
parents: 1
diff changeset
32 transformers = {} # XXX could cache
5258325a496a move transformers getting method to a standalone function
k0s <k0scist@gmail.com>
parents: 1
diff changeset
33 for entry_point in iter_entry_points('content_transformers'):
5258325a496a move transformers getting method to a standalone function
k0s <k0scist@gmail.com>
parents: 1
diff changeset
34 try:
5258325a496a move transformers getting method to a standalone function
k0s <k0scist@gmail.com>
parents: 1
diff changeset
35 transformers[entry_point.name] = entry_point.load()
5258325a496a move transformers getting method to a standalone function
k0s <k0scist@gmail.com>
parents: 1
diff changeset
36 except:
5258325a496a move transformers getting method to a standalone function
k0s <k0scist@gmail.com>
parents: 1
diff changeset
37 raise # XXX
5258325a496a move transformers getting method to a standalone function
k0s <k0scist@gmail.com>
parents: 1
diff changeset
38 return transformers
5258325a496a move transformers getting method to a standalone function
k0s <k0scist@gmail.com>
parents: 1
diff changeset
39