PyXR

c:\python24\lib \ bsddb \ dbrecio.py



0001 
0002 """
0003 File-like objects that read from or write to a bsddb record.
0004 
0005 This implements (nearly) all stdio methods.
0006 
0007 f = DBRecIO(db, key, txn=None)
0008 f.close()           # explicitly release resources held
0009 flag = f.isatty()   # always false
0010 pos = f.tell()      # get current position
0011 f.seek(pos)         # set current position
0012 f.seek(pos, mode)   # mode 0: absolute; 1: relative; 2: relative to EOF
0013 buf = f.read()      # read until EOF
0014 buf = f.read(n)     # read up to n bytes
0015 f.truncate([size])  # truncate file at to at most size (default: current pos)
0016 f.write(buf)        # write at current position
0017 f.writelines(list)  # for line in list: f.write(line)
0018 
0019 Notes:
0020 - fileno() is left unimplemented so that code which uses it triggers
0021   an exception early.
0022 - There's a simple test set (see end of this file) - not yet updated
0023   for DBRecIO.
0024 - readline() is not implemented yet.
0025 
0026 
0027 From:
0028     Itamar Shtull-Trauring <itamar@maxnm.com>
0029 """
0030 
0031 import errno
0032 import string
0033 
0034 class DBRecIO:
0035     def __init__(self, db, key, txn=None):
0036         self.db = db
0037         self.key = key
0038         self.txn = txn
0039         self.len = None
0040         self.pos = 0
0041         self.closed = 0
0042         self.softspace = 0
0043 
0044     def close(self):
0045         if not self.closed:
0046             self.closed = 1
0047             del self.db, self.txn
0048 
0049     def isatty(self):
0050         if self.closed:
0051             raise ValueError, "I/O operation on closed file"
0052         return 0
0053 
0054     def seek(self, pos, mode = 0):
0055         if self.closed:
0056             raise ValueError, "I/O operation on closed file"
0057         if mode == 1:
0058             pos = pos + self.pos
0059         elif mode == 2:
0060             pos = pos + self.len
0061         self.pos = max(0, pos)
0062 
0063     def tell(self):
0064         if self.closed:
0065             raise ValueError, "I/O operation on closed file"
0066         return self.pos
0067 
0068     def read(self, n = -1):
0069         if self.closed:
0070             raise ValueError, "I/O operation on closed file"
0071         if n < 0:
0072             newpos = self.len
0073         else:
0074             newpos = min(self.pos+n, self.len)
0075 
0076         dlen = newpos - self.pos
0077 
0078         r = self.db.get(key, txn=self.txn, dlen=dlen, doff=self.pos)
0079         self.pos = newpos
0080         return r
0081 
0082     __fixme = """
0083     def readline(self, length=None):
0084         if self.closed:
0085             raise ValueError, "I/O operation on closed file"
0086         if self.buflist:
0087             self.buf = self.buf + string.joinfields(self.buflist, '')
0088             self.buflist = []
0089         i = string.find(self.buf, '\n', self.pos)
0090         if i < 0:
0091             newpos = self.len
0092         else:
0093             newpos = i+1
0094         if length is not None:
0095             if self.pos + length < newpos:
0096                 newpos = self.pos + length
0097         r = self.buf[self.pos:newpos]
0098         self.pos = newpos
0099         return r
0100 
0101     def readlines(self, sizehint = 0):
0102         total = 0
0103         lines = []
0104         line = self.readline()
0105         while line:
0106             lines.append(line)
0107             total += len(line)
0108             if 0 < sizehint <= total:
0109                 break
0110             line = self.readline()
0111         return lines
0112     """
0113 
0114     def truncate(self, size=None):
0115         if self.closed:
0116             raise ValueError, "I/O operation on closed file"
0117         if size is None:
0118             size = self.pos
0119         elif size < 0:
0120             raise IOError(errno.EINVAL,
0121                                       "Negative size not allowed")
0122         elif size < self.pos:
0123             self.pos = size
0124         self.db.put(key, "", txn=self.txn, dlen=self.len-size, doff=size)
0125 
0126     def write(self, s):
0127         if self.closed:
0128             raise ValueError, "I/O operation on closed file"
0129         if not s: return
0130         if self.pos > self.len:
0131             self.buflist.append('\0'*(self.pos - self.len))
0132             self.len = self.pos
0133         newpos = self.pos + len(s)
0134         self.db.put(key, s, txn=self.txn, dlen=len(s), doff=self.pos)
0135         self.pos = newpos
0136 
0137     def writelines(self, list):
0138         self.write(string.joinfields(list, ''))
0139 
0140     def flush(self):
0141         if self.closed:
0142             raise ValueError, "I/O operation on closed file"
0143 
0144 
0145 """
0146 # A little test suite
0147 
0148 def _test():
0149     import sys
0150     if sys.argv[1:]:
0151         file = sys.argv[1]
0152     else:
0153         file = '/etc/passwd'
0154     lines = open(file, 'r').readlines()
0155     text = open(file, 'r').read()
0156     f = StringIO()
0157     for line in lines[:-2]:
0158         f.write(line)
0159     f.writelines(lines[-2:])
0160     if f.getvalue() != text:
0161         raise RuntimeError, 'write failed'
0162     length = f.tell()
0163     print 'File length =', length
0164     f.seek(len(lines[0]))
0165     f.write(lines[1])
0166     f.seek(0)
0167     print 'First line =', repr(f.readline())
0168     here = f.tell()
0169     line = f.readline()
0170     print 'Second line =', repr(line)
0171     f.seek(-len(line), 1)
0172     line2 = f.read(len(line))
0173     if line != line2:
0174         raise RuntimeError, 'bad result after seek back'
0175     f.seek(len(line2), 1)
0176     list = f.readlines()
0177     line = list[-1]
0178     f.seek(f.tell() - len(line))
0179     line2 = f.read()
0180     if line != line2:
0181         raise RuntimeError, 'bad result after seek back from EOF'
0182     print 'Read', len(list), 'more lines'
0183     print 'File length =', f.tell()
0184     if f.tell() != length:
0185         raise RuntimeError, 'bad length'
0186     f.close()
0187 
0188 if __name__ == '__main__':
0189     _test()
0190 """
0191 

Generated by PyXR 0.9.4
SourceForge.net Logo