comparison simpypi/factory.py @ 23:e72d9655d753

start stubbing a directory server
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 28 Feb 2012 16:59:20 -0800
parents de28a619880e
children 13ed82d10144
comparison
equal deleted inserted replaced
22:53328d3d9936 23:e72d9655d753
14 from paste.urlparser import StaticURLParser 14 from paste.urlparser import StaticURLParser
15 from pkg_resources import resource_filename 15 from pkg_resources import resource_filename
16 from wsgi import SimPyPI 16 from wsgi import SimPyPI
17 from wsgiref import simple_server 17 from wsgiref import simple_server
18 18
19 class DirectoryServer(StaticURLParser):
20 def __init__(self, directory):
21 StaticUrlParser.__init__(self, directory)
22
23 def __call__(self, environ, start_response):
24 import pdb; pdb.set_trace()
25
19 class PassthroughFileserver(object): 26 class PassthroughFileserver(object):
20 """serve files if they exist""" 27 """serve files if they exist"""
21 28
22 def __init__(self, app, directory): 29 def __init__(self, app, directory):
23 self.app = app 30 self.app = app
28 path = self.path(environ['PATH_INFO'].strip('/')) 35 path = self.path(environ['PATH_INFO'].strip('/'))
29 if path and os.path.exists(os.path.join(self.directory, path)): 36 if path and os.path.exists(os.path.join(self.directory, path)):
30 return self.fileserver(environ, start_response) 37 return self.fileserver(environ, start_response)
31 return self.app(environ, start_response) 38 return self.app(environ, start_response)
32 39
33 class NamespacedFileserver(PassthroughFileserver): 40 class NamespacedFileserver(DirectoryServer):
34 41
35 def __init__(self, app, directory, namespace): 42 def __init__(self, app, directory, namespace):
36 PassthroughFileserver.__init__(self, app, directory) 43 DirectoryServer.__init__(self, directory)
44 self.app = app
37 self.namespace = namespace 45 self.namespace = namespace
38 46
39 def __call__(self, environ, start_response): 47 def __call__(self, environ, start_response):
40 path = environ['PATH_INFO'] 48 path = environ['PATH_INFO']
41 if path == self.namespace: 49 if path == self.namespace:
43 return self.fileserver(environ, start_response) 51 return self.fileserver(environ, start_response)
44 elif path.startswith(self.namespace + '/'): 52 elif path.startswith(self.namespace + '/'):
45 environ['PATH_INFO'] = path[len(self.namespace):] 53 environ['PATH_INFO'] = path[len(self.namespace):]
46 return self.fileserver(environ, start_response) 54 return self.fileserver(environ, start_response)
47 return self.app(environ, start_response) 55 return self.app(environ, start_response)
56
48 57
49 def factory(**app_conf): 58 def factory(**app_conf):
50 """create a webob view and wrap it in middleware""" 59 """create a webob view and wrap it in middleware"""
51 directory = app_conf['directory'] 60 directory = app_conf['directory']
52 app = SimPyPI(**app_conf) 61 app = SimPyPI(**app_conf)