# HG changeset patch # User Jeff Hammel # Date 1330985160 28800 # Node ID 0edb831061f5a25db5772a47afc0d4b945e9ad90 # Parent f00fcf6d9f1d3b576cb7e80ca28c600e2e70cd35 test not modified response diff -r f00fcf6d9f1d -r 0edb831061f5 fileserver/web.py --- a/fileserver/web.py Mon Mar 05 13:49:07 2012 -0800 +++ b/fileserver/web.py Mon Mar 05 14:06:00 2012 -0800 @@ -19,24 +19,42 @@ ### classes for iterating over files class FileIterable(object): - def __init__(self, filename): + def __init__(self, filename, start=None, stop=None): self.filename = filename + self.start = start + self.stop = stop def __iter__(self): - return FileIterator(self.filename) + return FileIterator(self.filename, self.start, self.stop) class FileIterator(object): - def __init__(self, filename, chunk_size=4096): + def __init__(self, filename, start, stop, chunk_size=4096): self.filename = filename self.chunk_size = chunk_size self.fileobj = open(self.filename, 'rb') + if start: + self.fileobj.seek(start) + if stop is not None: + self.length = stop - start + else: + self.length = None def __iter__(self): return self def next(self): + if self.length is not None and self.length <= 0: + raise StopIteration chunk = self.fileobj.read(self.chunk_size) if not chunk: raise StopIteration + if self.length is not None: + self.length -= len(chunk) + if self.length < 0: + # Chop off the extra: + chunk = chunk[:self.length] return chunk __next__ = next # py3 compat + +### attributes for serving static files + def get_mimetype(filename): type, encoding = mimetypes.guess_type(filename) # We'll ignore encoding, even though we shouldn't really @@ -65,6 +83,9 @@ res = file_response(self.filename) return res(environ, start_response) + +### class for serving directory indices + class DirectoryServer(object): def __init__(self, directory, sort=True):