Mercurial > hg > TagInTheMiddle
comparison taginthemiddle/example.py @ 3:c2bb8f873aee
make a useful(?) example factory
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Wed, 05 May 2010 22:05:16 -0700 |
parents | 1c5cbbde4299 |
children | bf4c763b0313 |
comparison
equal
deleted
inserted
replaced
2:1182315b18ac | 3:c2bb8f873aee |
---|---|
1 import os | 1 from dispatcher import Tagger |
2 | |
3 from dispatcher import Dispatcher | |
4 from paste.httpexceptions import HTTPExceptionHandler | 2 from paste.httpexceptions import HTTPExceptionHandler |
5 from paste.urlparser import StaticURLParser | 3 from paste.urlparser import StaticURLParser |
6 from pkg_resources import resource_filename | 4 from pkg_resources import resource_filename |
7 | 5 |
8 class PassthroughFileserver(object): | |
9 """serve files if they exist""" | |
10 | |
11 def __init__(self, app, directory): | |
12 self.app = app | |
13 self.directory = directory | |
14 self.fileserver = StaticURLParser(self.directory) | |
15 | |
16 def __call__(self, environ, start_response): | |
17 path = environ['PATH_INFO'].strip('/') | |
18 if path and os.path.exists(os.path.join(self.directory, path)): | |
19 return self.fileserver(environ, start_response) | |
20 return self.app(environ, start_response) | |
21 | |
22 def factory(global_conf, **app_conf): | 6 def factory(global_conf, **app_conf): |
23 """create a webob view and wrap it in middleware""" | 7 """create an example view and wrap it in tagging middleware""" |
24 | 8 |
25 keystr = 'TagInTheMiddle.' | 9 keystr = 'TagInTheMiddle.' |
26 args = dict([(key.split(keystr, 1)[-1], value) | 10 args = dict([(key.split(keystr, 1)[-1], value) |
27 for key, value in app_conf.items() | 11 for key, value in app_conf.items() |
28 if key.startswith(keystr) ]) | 12 if key.startswith(keystr) ]) |
29 app = Dispatcher(**args) | 13 app = StaticURLParser(app_conf['directory']) |
30 return HTTPExceptionHandler(PassthroughFileserver(app, resource_filename(__name__, 'static'))) | 14 tagger = Tagger(app, **args) |
15 return HTTPExceptionHandler(tagger) | |
31 | 16 |