comparison simpypi/factory.py @ 16:d3efc504c0b1

more towards the type of fileserver we actually care about
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 28 Feb 2012 15:17:23 -0800
parents 1cdb25cef7ee
children 77357c5c33c2
comparison
equal deleted inserted replaced
15:cfc141c154a8 16:d3efc504c0b1
6 from pkg_resources import resource_filename 6 from pkg_resources import resource_filename
7 7
8 class PassthroughFileserver(object): 8 class PassthroughFileserver(object):
9 """serve files if they exist""" 9 """serve files if they exist"""
10 10
11 def __init__(self, app, *directories): 11 def __init__(self, app, directory):
12 self.app = app 12 self.app = app
13 missing = [i for i in directories 13 self.directory = directory
14 if not os.path.exists(i) or not os.path.isdir(i)] 14 self.fileserver = StaticURLParser(directory)
15 assert not missing, "Missing directories: %s" % missing
16 self.directories = [(directory, StaticURLParser(directory))
17 for directory in directories]
18 15
19 def __call__(self, environ, start_response): 16 def __call__(self, environ, start_response):
20 path = environ['PATH_INFO'].strip('/') 17 path = self.path(environ['PATH_INFO'].strip('/'))
21 for directory, fileserver in self.directories: 18 if path and os.path.exists(os.path.join(self.directory, path)):
22 if path and os.path.exists(os.path.join(directory, path)): 19 return self.fileserver(environ, start_response)
23 return fileserver(environ, start_response) 20 return self.app(environ, start_response)
21
22 class NamespacedFileserver(PassthroughFileserver):
23
24 def __init__(self, app, directory, namespace):
25 PassthroughFileserver.__init__(self, app, directory)
26 self.namespace = namespace
27
28 def __call__(self, environ, start_response):
29 path = environ['PATH_INFO']
30 if path == self.namespace:
31 environ['PATH_INFO'] = '/'
32 return self.fileserver(environ, start_response)
33 elif path.startswith(self.namespace + '/'):
34 environ['PATH_INFO'] = path[len(self.namespace):]
35 return self.fileserver(environ, start_response)
24 return self.app(environ, start_response) 36 return self.app(environ, start_response)
25 37
26 def factory(**app_conf): 38 def factory(**app_conf):
27 """create a webob view and wrap it in middleware""" 39 """create a webob view and wrap it in middleware"""
28 directory = app_conf['directory'] 40 directory = app_conf['directory']