Rich Salz
rsalz at zolera.com
Fri May 25 09:41:44 EST 2001
The attached class provides a more complete implementation of Python's file object. We'd be quite pleased if it were accepted into the core. :) /r$ -------------- next part -------------- #! /usr/bin/env python ## ContentReader -- wrapper class for mod_python's request object that ## provides a more complete set of file methods. ## Copyright 2001, Zolera Systems, Inc. All Rights Reserved. ## This software is distributed under the terms of the mod_python license. try: import xreadlines except: pass class ContentReader: '''A simple object that provides a minimal set of file methods from a mod_python request object. Methods provided: read, readline, readlines, close, flush, and tell (and, if available, xreadlines). Attributes provided: closed, mode, and name. ''' def __init__(self, req): '''req is the mod_python request handler.''' self.size = int(req.headers_in['content-length']) self.name = '<%s at 0x%x>' % (str(self.__class__), id(self)) self.req, self.left, self.closed, self.mode = \ req, self.size, 0, 'r' def read(self, size = 0): if self.closed: raise ValueError if self.left <= 0: return '' if size <= 0: size = self.left v = self.req.read(size) self.left = self.left - len(v) return v def readline(self, size = 0): v = self.req.readline(size) if len(v) > 1 and v[-2:] == '\r\n': v = v[:-2] + '\n' return v def readlines(self, sizehint = None): result = [] while 1: l = x.readline() if l == '': break result.append(l) if sizehint != None: sizehint = sizehint - len(l) if sizehint <= 0: break return result def xreadlines(self): return xreadlines.xreadlines(self) def tell(self): return self.size - self.left def close(self): self.closed = 1 def flush(self): pass
|