comparison genshi_view/template/+package+/factory.py_tmpl @ 0:b65da5423cc9

initial import from https://svn.openplans.org/svn/standalone/templates/genshi_view/trunk/
author k0s <k0scist@gmail.com>
date Tue, 27 Oct 2009 15:11:43 -0400
parents
children b8e5471794b2
comparison
equal deleted inserted replaced
-1:000000000000 0:b65da5423cc9
1 import os
2
3 from ${package} import ${project.title()}View
4 from paste.httpexceptions import HTTPExceptionHandler
5 from paste.urlparser import StaticURLParser
6 from pkg_resources import resource_filename
7
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):
23 """create a webob view and wrap it in middleware"""
24
25 keystr = '${project}.'
26 args = dict([(key.split(keystr, 1)[-1], value)
27 for key, value in app_conf.items()
28 if key.startswith(keystr) ])
29 app = ${project.title()}View(**args)
30 return HTTPExceptionHandler(PassthroughFileserver(app, resource_filename(__name__, 'static')))
31