Mercurial > hg > contenttransformer
annotate contenttransformer/app.py @ 25:2a2063d571a6
py3
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Mon, 28 Dec 2020 20:42:56 +0000 |
parents | ee3eadc97a41 |
children |
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 |
9
051d4d39b4b9
* separate out get_response to its own function
k0s <k0scist@gmail.com>
parents:
4
diff
changeset
|
4 from mimetypes import guess_type |
0
29805d442afc
initial commit of contenttransformer; still in the stub stage
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
5 from paste.fileapp import FileApp |
29805d442afc
initial commit of contenttransformer; still in the stub stage
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
6 from pkg_resources import iter_entry_points |
22 | 7 from .transformers import ContentTypeChanger |
0
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 class FileTypeTransformer(object): |
29805d442afc
initial commit of contenttransformer; still in the stub stage
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
10 |
29805d442afc
initial commit of contenttransformer; still in the stub stage
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
11 def __init__(self, *types, **kwargs): |
29805d442afc
initial commit of contenttransformer; still in the stub stage
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
12 """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
|
13 self.types = types |
1
aa491070ccf3
now works, just doesnt do anything, i dont think
k0s <k0scist@gmail.com>
parents:
0
diff
changeset
|
14 |
13
9d51954e2e68
allow transformers to take kwargs
Jeff Hammel <jhammel@mozilla.com>
parents:
10
diff
changeset
|
15 # arguments to the xformers |
1
aa491070ccf3
now works, just doesnt do anything, i dont think
k0s <k0scist@gmail.com>
parents:
0
diff
changeset
|
16 self.kwargs = kwargs |
22 | 17 |
4
5258325a496a
move transformers getting method to a standalone function
k0s <k0scist@gmail.com>
parents:
1
diff
changeset
|
18 self.transformers = transformers() |
0
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: |
10
a9ddcfc7c4e8
added transformer just of content-type name
k0s <k0scist@gmail.com>
parents:
9
diff
changeset
|
20 if '/' in transformer_name: |
a9ddcfc7c4e8
added transformer just of content-type name
k0s <k0scist@gmail.com>
parents:
9
diff
changeset
|
21 continue |
14 | 22 assert transformer_name in self.transformers, '%s not in transformers' % transformer_name |
0
29805d442afc
initial commit of contenttransformer; still in the stub stage
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
23 |
22 | 24 def __call__(self, path): |
0
29805d442afc
initial commit of contenttransformer; still in the stub stage
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
25 """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
|
26 filename = os.path.basename(path) |
29805d442afc
initial commit of contenttransformer; still in the stub stage
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
27 for pattern, transformer_name in self.types: |
29805d442afc
initial commit of contenttransformer; still in the stub stage
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
28 if fnmatch(filename, pattern): |
9
051d4d39b4b9
* separate out get_response to its own function
k0s <k0scist@gmail.com>
parents:
4
diff
changeset
|
29 content_type, _ = guess_type(filename) |
25 | 30 content = open(path).read() |
10
a9ddcfc7c4e8
added transformer just of content-type name
k0s <k0scist@gmail.com>
parents:
9
diff
changeset
|
31 |
a9ddcfc7c4e8
added transformer just of content-type name
k0s <k0scist@gmail.com>
parents:
9
diff
changeset
|
32 # transform content type |
a9ddcfc7c4e8
added transformer just of content-type name
k0s <k0scist@gmail.com>
parents:
9
diff
changeset
|
33 # XXX hack: -> refactor |
a9ddcfc7c4e8
added transformer just of content-type name
k0s <k0scist@gmail.com>
parents:
9
diff
changeset
|
34 if '/' in transformer_name: |
a9ddcfc7c4e8
added transformer just of content-type name
k0s <k0scist@gmail.com>
parents:
9
diff
changeset
|
35 return ContentTypeChanger(content, content_type, transformer_name) |
22 | 36 return self.transformers[transformer_name](content, content_type, **self.kwargs.get(transformer_name, {})) |
0
29805d442afc
initial commit of contenttransformer; still in the stub stage
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
37 return FileApp(path) |
4
5258325a496a
move transformers getting method to a standalone function
k0s <k0scist@gmail.com>
parents:
1
diff
changeset
|
38 |
5258325a496a
move transformers getting method to a standalone function
k0s <k0scist@gmail.com>
parents:
1
diff
changeset
|
39 |
5258325a496a
move transformers getting method to a standalone function
k0s <k0scist@gmail.com>
parents:
1
diff
changeset
|
40 def transformers(): |
5258325a496a
move transformers getting method to a standalone function
k0s <k0scist@gmail.com>
parents:
1
diff
changeset
|
41 transformers = {} # XXX could cache |
5258325a496a
move transformers getting method to a standalone function
k0s <k0scist@gmail.com>
parents:
1
diff
changeset
|
42 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
|
43 try: |
5258325a496a
move transformers getting method to a standalone function
k0s <k0scist@gmail.com>
parents:
1
diff
changeset
|
44 transformers[entry_point.name] = entry_point.load() |
5258325a496a
move transformers getting method to a standalone function
k0s <k0scist@gmail.com>
parents:
1
diff
changeset
|
45 except: |
5258325a496a
move transformers getting method to a standalone function
k0s <k0scist@gmail.com>
parents:
1
diff
changeset
|
46 raise # XXX |
5258325a496a
move transformers getting method to a standalone function
k0s <k0scist@gmail.com>
parents:
1
diff
changeset
|
47 return transformers |
22 | 48 |