changeset 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 cfc141c154a8
children 77357c5c33c2
files simpypi/factory.py
diffstat 1 files changed, 22 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- 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):