# HG changeset patch # User Jeff Hammel # Date 1330471043 28800 # Node ID d3efc504c0b190b0869c6fb640c02d40799dfe6e # Parent cfc141c154a87edd2261ebae18b22547fc020abd more towards the type of fileserver we actually care about diff -r cfc141c154a8 -r d3efc504c0b1 simpypi/factory.py --- a/simpypi/factory.py Tue Feb 28 14:51:37 2012 -0800 +++ b/simpypi/factory.py Tue Feb 28 15:17:23 2012 -0800 @@ -8,19 +8,31 @@ class PassthroughFileserver(object): """serve files if they exist""" - def __init__(self, app, *directories): + def __init__(self, app, directory): self.app = app - 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] + self.directory = directory + self.fileserver = StaticURLParser(directory) def __call__(self, environ, start_response): - path = environ['PATH_INFO'].strip('/') - for directory, fileserver in self.directories: - if path and os.path.exists(os.path.join(directory, path)): - return fileserver(environ, start_response) + path = self.path(environ['PATH_INFO'].strip('/')) + if path and os.path.exists(os.path.join(self.directory, path)): + return self.fileserver(environ, start_response) + return self.app(environ, start_response) + +class NamespacedFileserver(PassthroughFileserver): + + def __init__(self, app, directory, namespace): + PassthroughFileserver.__init__(self, app, directory) + self.namespace = namespace + + def __call__(self, environ, start_response): + path = environ['PATH_INFO'] + if path == self.namespace: + environ['PATH_INFO'] = '/' + return self.fileserver(environ, start_response) + elif path.startswith(self.namespace + '/'): + environ['PATH_INFO'] = path[len(self.namespace):] + return self.fileserver(environ, start_response) return self.app(environ, start_response) def factory(**app_conf):