Mercurial > hg > contenttransformer
annotate contenttransformer/web.py @ 19:afd11b758da0
just require genshi for now
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Wed, 27 Oct 2010 09:09:37 -0700 |
parents | aa491070ccf3 |
children |
rev | line source |
---|---|
0
29805d442afc
initial commit of contenttransformer; still in the stub stage
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
1 """ |
29805d442afc
initial commit of contenttransformer; still in the stub stage
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
2 example app for how to use TypedTransformer TTW on a directory |
29805d442afc
initial commit of contenttransformer; still in the stub stage
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
3 """ |
29805d442afc
initial commit of contenttransformer; still in the stub stage
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
4 |
29805d442afc
initial commit of contenttransformer; still in the stub stage
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
5 import os |
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 from contenttransformer.app import FileTypeTransformer |
29805d442afc
initial commit of contenttransformer; still in the stub stage
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
8 from webob import Request, exc |
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 class Dispatcher(object): |
29805d442afc
initial commit of contenttransformer; still in the stub stage
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
11 |
29805d442afc
initial commit of contenttransformer; still in the stub stage
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
12 ### class level variables |
29805d442afc
initial commit of contenttransformer; still in the stub stage
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
13 defaults = { 'app': None, |
29805d442afc
initial commit of contenttransformer; still in the stub stage
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
14 'directory': None, |
29805d442afc
initial commit of contenttransformer; still in the stub stage
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
15 'transforms': ''} |
29805d442afc
initial commit of contenttransformer; still in the stub stage
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
16 |
29805d442afc
initial commit of contenttransformer; still in the stub stage
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
17 def __init__(self, **kw): |
29805d442afc
initial commit of contenttransformer; still in the stub stage
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
18 for key in self.defaults: |
29805d442afc
initial commit of contenttransformer; still in the stub stage
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
19 setattr(self, key, kw.get(key, self.defaults[key])) |
29805d442afc
initial commit of contenttransformer; still in the stub stage
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
20 assert os.path.exists(self.directory) |
29805d442afc
initial commit of contenttransformer; still in the stub stage
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
21 self.transforms = [ [j.strip() for j in i.split('=', 1) ] for i in self.transforms.split(',') if '=' in i] |
1
aa491070ccf3
now works, just doesnt do anything, i dont think
k0s <k0scist@gmail.com>
parents:
0
diff
changeset
|
22 self.transformer = FileTypeTransformer(*self.transforms) |
0
29805d442afc
initial commit of contenttransformer; still in the stub stage
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
23 if self.app: |
29805d442afc
initial commit of contenttransformer; still in the stub stage
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
24 assert hasattr(self.app, '__call__') |
29805d442afc
initial commit of contenttransformer; still in the stub stage
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
25 |
29805d442afc
initial commit of contenttransformer; still in the stub stage
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
26 ### methods dealing with HTTP |
29805d442afc
initial commit of contenttransformer; still in the stub stage
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
27 def __call__(self, environ, start_response): |
29805d442afc
initial commit of contenttransformer; still in the stub stage
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
28 request = Request(environ) |
1
aa491070ccf3
now works, just doesnt do anything, i dont think
k0s <k0scist@gmail.com>
parents:
0
diff
changeset
|
29 path = os.path.join(self.directory, request.path_info.strip('/')) |
aa491070ccf3
now works, just doesnt do anything, i dont think
k0s <k0scist@gmail.com>
parents:
0
diff
changeset
|
30 if os.path.exists(path) and os.path.isfile(path): |
aa491070ccf3
now works, just doesnt do anything, i dont think
k0s <k0scist@gmail.com>
parents:
0
diff
changeset
|
31 handler = self.transformer(path) |
aa491070ccf3
now works, just doesnt do anything, i dont think
k0s <k0scist@gmail.com>
parents:
0
diff
changeset
|
32 return handler(environ, start_response) |
0
29805d442afc
initial commit of contenttransformer; still in the stub stage
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
33 else: |
1
aa491070ccf3
now works, just doesnt do anything, i dont think
k0s <k0scist@gmail.com>
parents:
0
diff
changeset
|
34 # TODO: if self.app ... |
aa491070ccf3
now works, just doesnt do anything, i dont think
k0s <k0scist@gmail.com>
parents:
0
diff
changeset
|
35 return exc.HTTPNotFound()(environ, start_response) |