annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
b65da5423cc9 initial import from https://svn.openplans.org/svn/standalone/templates/genshi_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
1 import os
b65da5423cc9 initial import from https://svn.openplans.org/svn/standalone/templates/genshi_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
2
b65da5423cc9 initial import from https://svn.openplans.org/svn/standalone/templates/genshi_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
3 from ${package} import ${project.title()}View
b65da5423cc9 initial import from https://svn.openplans.org/svn/standalone/templates/genshi_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
4 from paste.httpexceptions import HTTPExceptionHandler
b65da5423cc9 initial import from https://svn.openplans.org/svn/standalone/templates/genshi_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
5 from paste.urlparser import StaticURLParser
b65da5423cc9 initial import from https://svn.openplans.org/svn/standalone/templates/genshi_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
6 from pkg_resources import resource_filename
b65da5423cc9 initial import from https://svn.openplans.org/svn/standalone/templates/genshi_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
7
b65da5423cc9 initial import from https://svn.openplans.org/svn/standalone/templates/genshi_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
8 class PassthroughFileserver(object):
b65da5423cc9 initial import from https://svn.openplans.org/svn/standalone/templates/genshi_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
9 """serve files if they exist"""
b65da5423cc9 initial import from https://svn.openplans.org/svn/standalone/templates/genshi_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
10
b65da5423cc9 initial import from https://svn.openplans.org/svn/standalone/templates/genshi_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
11 def __init__(self, app, directory):
b65da5423cc9 initial import from https://svn.openplans.org/svn/standalone/templates/genshi_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
12 self.app = app
b65da5423cc9 initial import from https://svn.openplans.org/svn/standalone/templates/genshi_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
13 self.directory = directory
b65da5423cc9 initial import from https://svn.openplans.org/svn/standalone/templates/genshi_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
14 self.fileserver = StaticURLParser(self.directory)
b65da5423cc9 initial import from https://svn.openplans.org/svn/standalone/templates/genshi_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
15
b65da5423cc9 initial import from https://svn.openplans.org/svn/standalone/templates/genshi_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
16 def __call__(self, environ, start_response):
b65da5423cc9 initial import from https://svn.openplans.org/svn/standalone/templates/genshi_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
17 path = environ['PATH_INFO'].strip('/')
b65da5423cc9 initial import from https://svn.openplans.org/svn/standalone/templates/genshi_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
18 if path and os.path.exists(os.path.join(self.directory, path)):
b65da5423cc9 initial import from https://svn.openplans.org/svn/standalone/templates/genshi_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
19 return self.fileserver(environ, start_response)
b65da5423cc9 initial import from https://svn.openplans.org/svn/standalone/templates/genshi_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
20 return self.app(environ, start_response)
b65da5423cc9 initial import from https://svn.openplans.org/svn/standalone/templates/genshi_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
21
b65da5423cc9 initial import from https://svn.openplans.org/svn/standalone/templates/genshi_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
22 def factory(global_conf, **app_conf):
b65da5423cc9 initial import from https://svn.openplans.org/svn/standalone/templates/genshi_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
23 """create a webob view and wrap it in middleware"""
b65da5423cc9 initial import from https://svn.openplans.org/svn/standalone/templates/genshi_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
24
b65da5423cc9 initial import from https://svn.openplans.org/svn/standalone/templates/genshi_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
25 keystr = '${project}.'
b65da5423cc9 initial import from https://svn.openplans.org/svn/standalone/templates/genshi_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
26 args = dict([(key.split(keystr, 1)[-1], value)
b65da5423cc9 initial import from https://svn.openplans.org/svn/standalone/templates/genshi_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
27 for key, value in app_conf.items()
b65da5423cc9 initial import from https://svn.openplans.org/svn/standalone/templates/genshi_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
28 if key.startswith(keystr) ])
b65da5423cc9 initial import from https://svn.openplans.org/svn/standalone/templates/genshi_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
29 app = ${project.title()}View(**args)
b65da5423cc9 initial import from https://svn.openplans.org/svn/standalone/templates/genshi_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
30 return HTTPExceptionHandler(PassthroughFileserver(app, resource_filename(__name__, 'static')))
b65da5423cc9 initial import from https://svn.openplans.org/svn/standalone/templates/genshi_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
31