changeset 4:7caedb575794

serve multiple directories
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 27 Feb 2012 16:20:08 -0800
parents f5360bb59e41
children 39080c0f1b08
files simpypi/factory.py
diffstat 1 files changed, 10 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- 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'))