# HG changeset patch # User Jeff Hammel # Date 1330540151 28800 # Node ID 511210365ce32f21dbf46e29623e6fe8d5e32d0f # Parent 13ed82d101443569b5188fccdf50630bde5591f3 WIP getting paste StaticURLParser to serve directories diff -r 13ed82d10144 -r 511210365ce3 simpypi/factory.py --- a/simpypi/factory.py Wed Feb 29 09:16:48 2012 -0800 +++ b/simpypi/factory.py Wed Feb 29 10:29:11 2012 -0800 @@ -20,8 +20,42 @@ def __init__(self, directory): StaticURLParser.__init__(self, directory) + def index(self, directory): + """ + generate a directory listing for a given directory + """ + parts = ['Simple Index'] + listings = os.listdir(directory) + listings = [(os.path.isdir(os.path.join(directory, entry)) and entry + '/' or entry, entry) + for entry in listings] + for link, entry in listings: + parts.append('%s
' % (link, entry)) + parts.append('') + return '\n'.join(parts) + def __call__(self, environ, start_response): - import pdb; pdb.set_trace() + + # normalize path + # from paste.urlparser.StaticURLParser + path_info = environ.get('PATH_INFO', '') + if not path_info: + return self.add_slash(environ, start_response) + full = self.normpath(os.path.join(self.directory, path_info.strip('/'))) + if not full.startswith(self.root_directory): + # Out of bounds + return self.not_found(environ, start_response) + + # return index listing + if os.path.isdir(full): + if not path_info.endswith('/'): + return self.add_slash(environ, start_response) + index = self.index(full) + response_headers = [('Content-Type', 'text/html'), + ('Content-Length', str(len(index)))] + start_response('200 OK', response_headers) + return [index] + + return StaticURLParser.__call__(self, environ, start_response) class PassthroughFileserver(object): """serve files if they exist""" @@ -47,6 +81,7 @@ def __call__(self, environ, start_response): path = environ['PATH_INFO'] if path == self.namespace: + return self.add_slash(environ, start_response) environ['PATH_INFO'] = '/' return DirectoryServer.__call__(self, environ, start_response) elif path.startswith(self.namespace + '/'):