# HG changeset patch # User Jeff Hammel # Date 1330563064 28800 # Node ID fb03f34a982f61f8b3e5dc941b8dd34af3dcda09 # Parent f8ff95180d04918141d1df270480ab28e8d7bdd9 stub using new fileserver diff -r f8ff95180d04 -r fb03f34a982f simpypi/factory.py --- a/simpypi/factory.py Wed Feb 29 16:43:46 2012 -0800 +++ b/simpypi/factory.py Wed Feb 29 16:51:04 2012 -0800 @@ -10,52 +10,10 @@ import sys import tempfile -from paste.httpexceptions import HTTPExceptionHandler -from paste.urlparser import StaticURLParser -from pkg_resources import resource_filename +from fileserver import DirectoryServer from wsgi import SimPyPI from wsgiref import simple_server -class DirectoryServer(StaticURLParser): - 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): - - # 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"""