annotate makeitso/factory.py @ 36:0cba953a03ca

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