annotate simplewiki/factory.py @ 5:b2fbb4f982da default tip

[mq]: edit
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 07 Sep 2010 22:58:11 -0700
parents d5102c881cb5
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 import os
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 from dispatcher import Dispatcher
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4 from paste.httpexceptions import HTTPExceptionHandler
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5 from paste.urlparser import StaticURLParser
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6 from pkg_resources import resource_filename
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
7
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
8 class PassthroughFileserver(object):
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
9 """serve files if they exist"""
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
10
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11 def __init__(self, app, directory):
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12 self.app = app
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13 self.directory = directory
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14 self.fileserver = StaticURLParser(self.directory)
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16 def __call__(self, environ, start_response):
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17 path = environ['PATH_INFO'].strip('/')
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18 if path and os.path.exists(os.path.join(self.directory, path)):
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19 return self.fileserver(environ, start_response)
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20 return self.app(environ, start_response)
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22 def factory(global_conf, **app_conf):
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23 """create a webob view and wrap it in middleware"""
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
25 keystr = 'SimpleWiki.'
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
26 args = dict([(key.split(keystr, 1)[-1], value)
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
27 for key, value in app_conf.items()
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
28 if key.startswith(keystr) ])
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
29 app = Dispatcher(**args)
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30 return HTTPExceptionHandler(PassthroughFileserver(app, resource_filename(__name__, 'static')))
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
31