# HG changeset patch # User Jeff Hammel # Date 1330388408 28800 # Node ID 7caedb57579469f4e945d9cc17bda62363234491 # Parent f5360bb59e418698ba4fc67d13661ed94030279e serve multiple directories diff -r f5360bb59e41 -r 7caedb575794 simpypi/factory.py --- a/simpypi/factory.py Mon Feb 27 16:12:43 2012 -0800 +++ b/simpypi/factory.py Mon Feb 27 16:20:08 2012 -0800 @@ -8,19 +8,24 @@ class PassthroughFileserver(object): """serve files if they exist""" - def __init__(self, app, directory): + def __init__(self, app, *directories): self.app = app - self.directory = directory - self.fileserver = StaticURLParser(self.directory) + missing = [i for i in directories + if not os.path.exists(i) or not os.path.isdir(i)] + assert not missing, "Missing directories: %s" % missing + self.directories = [(directory, StaticURLParser(directory)) + for directory in directories] def __call__(self, environ, start_response): path = environ['PATH_INFO'].strip('/') - if path and os.path.exists(os.path.join(self.directory, path)): - return self.fileserver(environ, start_response) + for directory, fileserver in self.directories: + if path and os.path.exists(os.path.join(directory, path)): + return fileserver(environ, start_response) return self.app(environ, start_response) def factory(**app_conf): """create a webob view and wrap it in middleware""" + directory = app_conf['directory'] app = Dispatcher(**app_conf) return PassthroughFileserver(app, resource_filename(__name__, 'static'))