Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ExeBy/smexe_ro.../lib64/python2..../bsddb
File: dbrecio.py
[0] Fix | Delete
"""
[1] Fix | Delete
File-like objects that read from or write to a bsddb record.
[2] Fix | Delete
[3] Fix | Delete
This implements (nearly) all stdio methods.
[4] Fix | Delete
[5] Fix | Delete
f = DBRecIO(db, key, txn=None)
[6] Fix | Delete
f.close() # explicitly release resources held
[7] Fix | Delete
flag = f.isatty() # always false
[8] Fix | Delete
pos = f.tell() # get current position
[9] Fix | Delete
f.seek(pos) # set current position
[10] Fix | Delete
f.seek(pos, mode) # mode 0: absolute; 1: relative; 2: relative to EOF
[11] Fix | Delete
buf = f.read() # read until EOF
[12] Fix | Delete
buf = f.read(n) # read up to n bytes
[13] Fix | Delete
f.truncate([size]) # truncate file at to at most size (default: current pos)
[14] Fix | Delete
f.write(buf) # write at current position
[15] Fix | Delete
f.writelines(list) # for line in list: f.write(line)
[16] Fix | Delete
[17] Fix | Delete
Notes:
[18] Fix | Delete
- fileno() is left unimplemented so that code which uses it triggers
[19] Fix | Delete
an exception early.
[20] Fix | Delete
- There's a simple test set (see end of this file) - not yet updated
[21] Fix | Delete
for DBRecIO.
[22] Fix | Delete
- readline() is not implemented yet.
[23] Fix | Delete
[24] Fix | Delete
[25] Fix | Delete
From:
[26] Fix | Delete
Itamar Shtull-Trauring <itamar@maxnm.com>
[27] Fix | Delete
"""
[28] Fix | Delete
[29] Fix | Delete
import errno
[30] Fix | Delete
import string
[31] Fix | Delete
[32] Fix | Delete
class DBRecIO:
[33] Fix | Delete
def __init__(self, db, key, txn=None):
[34] Fix | Delete
self.db = db
[35] Fix | Delete
self.key = key
[36] Fix | Delete
self.txn = txn
[37] Fix | Delete
self.len = None
[38] Fix | Delete
self.pos = 0
[39] Fix | Delete
self.closed = 0
[40] Fix | Delete
self.softspace = 0
[41] Fix | Delete
[42] Fix | Delete
def close(self):
[43] Fix | Delete
if not self.closed:
[44] Fix | Delete
self.closed = 1
[45] Fix | Delete
del self.db, self.txn
[46] Fix | Delete
[47] Fix | Delete
def isatty(self):
[48] Fix | Delete
if self.closed:
[49] Fix | Delete
raise ValueError, "I/O operation on closed file"
[50] Fix | Delete
return 0
[51] Fix | Delete
[52] Fix | Delete
def seek(self, pos, mode = 0):
[53] Fix | Delete
if self.closed:
[54] Fix | Delete
raise ValueError, "I/O operation on closed file"
[55] Fix | Delete
if mode == 1:
[56] Fix | Delete
pos = pos + self.pos
[57] Fix | Delete
elif mode == 2:
[58] Fix | Delete
pos = pos + self.len
[59] Fix | Delete
self.pos = max(0, pos)
[60] Fix | Delete
[61] Fix | Delete
def tell(self):
[62] Fix | Delete
if self.closed:
[63] Fix | Delete
raise ValueError, "I/O operation on closed file"
[64] Fix | Delete
return self.pos
[65] Fix | Delete
[66] Fix | Delete
def read(self, n = -1):
[67] Fix | Delete
if self.closed:
[68] Fix | Delete
raise ValueError, "I/O operation on closed file"
[69] Fix | Delete
if n < 0:
[70] Fix | Delete
newpos = self.len
[71] Fix | Delete
else:
[72] Fix | Delete
newpos = min(self.pos+n, self.len)
[73] Fix | Delete
[74] Fix | Delete
dlen = newpos - self.pos
[75] Fix | Delete
[76] Fix | Delete
r = self.db.get(self.key, txn=self.txn, dlen=dlen, doff=self.pos)
[77] Fix | Delete
self.pos = newpos
[78] Fix | Delete
return r
[79] Fix | Delete
[80] Fix | Delete
__fixme = """
[81] Fix | Delete
def readline(self, length=None):
[82] Fix | Delete
if self.closed:
[83] Fix | Delete
raise ValueError, "I/O operation on closed file"
[84] Fix | Delete
if self.buflist:
[85] Fix | Delete
self.buf = self.buf + string.joinfields(self.buflist, '')
[86] Fix | Delete
self.buflist = []
[87] Fix | Delete
i = string.find(self.buf, '\n', self.pos)
[88] Fix | Delete
if i < 0:
[89] Fix | Delete
newpos = self.len
[90] Fix | Delete
else:
[91] Fix | Delete
newpos = i+1
[92] Fix | Delete
if length is not None:
[93] Fix | Delete
if self.pos + length < newpos:
[94] Fix | Delete
newpos = self.pos + length
[95] Fix | Delete
r = self.buf[self.pos:newpos]
[96] Fix | Delete
self.pos = newpos
[97] Fix | Delete
return r
[98] Fix | Delete
[99] Fix | Delete
def readlines(self, sizehint = 0):
[100] Fix | Delete
total = 0
[101] Fix | Delete
lines = []
[102] Fix | Delete
line = self.readline()
[103] Fix | Delete
while line:
[104] Fix | Delete
lines.append(line)
[105] Fix | Delete
total += len(line)
[106] Fix | Delete
if 0 < sizehint <= total:
[107] Fix | Delete
break
[108] Fix | Delete
line = self.readline()
[109] Fix | Delete
return lines
[110] Fix | Delete
"""
[111] Fix | Delete
[112] Fix | Delete
def truncate(self, size=None):
[113] Fix | Delete
if self.closed:
[114] Fix | Delete
raise ValueError, "I/O operation on closed file"
[115] Fix | Delete
if size is None:
[116] Fix | Delete
size = self.pos
[117] Fix | Delete
elif size < 0:
[118] Fix | Delete
raise IOError(errno.EINVAL,
[119] Fix | Delete
"Negative size not allowed")
[120] Fix | Delete
elif size < self.pos:
[121] Fix | Delete
self.pos = size
[122] Fix | Delete
self.db.put(self.key, "", txn=self.txn, dlen=self.len-size, doff=size)
[123] Fix | Delete
[124] Fix | Delete
def write(self, s):
[125] Fix | Delete
if self.closed:
[126] Fix | Delete
raise ValueError, "I/O operation on closed file"
[127] Fix | Delete
if not s: return
[128] Fix | Delete
if self.pos > self.len:
[129] Fix | Delete
self.buflist.append('\0'*(self.pos - self.len))
[130] Fix | Delete
self.len = self.pos
[131] Fix | Delete
newpos = self.pos + len(s)
[132] Fix | Delete
self.db.put(self.key, s, txn=self.txn, dlen=len(s), doff=self.pos)
[133] Fix | Delete
self.pos = newpos
[134] Fix | Delete
[135] Fix | Delete
def writelines(self, list):
[136] Fix | Delete
self.write(string.joinfields(list, ''))
[137] Fix | Delete
[138] Fix | Delete
def flush(self):
[139] Fix | Delete
if self.closed:
[140] Fix | Delete
raise ValueError, "I/O operation on closed file"
[141] Fix | Delete
[142] Fix | Delete
[143] Fix | Delete
"""
[144] Fix | Delete
# A little test suite
[145] Fix | Delete
[146] Fix | Delete
def _test():
[147] Fix | Delete
import sys
[148] Fix | Delete
if sys.argv[1:]:
[149] Fix | Delete
file = sys.argv[1]
[150] Fix | Delete
else:
[151] Fix | Delete
file = '/etc/passwd'
[152] Fix | Delete
lines = open(file, 'r').readlines()
[153] Fix | Delete
text = open(file, 'r').read()
[154] Fix | Delete
f = StringIO()
[155] Fix | Delete
for line in lines[:-2]:
[156] Fix | Delete
f.write(line)
[157] Fix | Delete
f.writelines(lines[-2:])
[158] Fix | Delete
if f.getvalue() != text:
[159] Fix | Delete
raise RuntimeError, 'write failed'
[160] Fix | Delete
length = f.tell()
[161] Fix | Delete
print 'File length =', length
[162] Fix | Delete
f.seek(len(lines[0]))
[163] Fix | Delete
f.write(lines[1])
[164] Fix | Delete
f.seek(0)
[165] Fix | Delete
print 'First line =', repr(f.readline())
[166] Fix | Delete
here = f.tell()
[167] Fix | Delete
line = f.readline()
[168] Fix | Delete
print 'Second line =', repr(line)
[169] Fix | Delete
f.seek(-len(line), 1)
[170] Fix | Delete
line2 = f.read(len(line))
[171] Fix | Delete
if line != line2:
[172] Fix | Delete
raise RuntimeError, 'bad result after seek back'
[173] Fix | Delete
f.seek(len(line2), 1)
[174] Fix | Delete
list = f.readlines()
[175] Fix | Delete
line = list[-1]
[176] Fix | Delete
f.seek(f.tell() - len(line))
[177] Fix | Delete
line2 = f.read()
[178] Fix | Delete
if line != line2:
[179] Fix | Delete
raise RuntimeError, 'bad result after seek back from EOF'
[180] Fix | Delete
print 'Read', len(list), 'more lines'
[181] Fix | Delete
print 'File length =', f.tell()
[182] Fix | Delete
if f.tell() != length:
[183] Fix | Delete
raise RuntimeError, 'bad length'
[184] Fix | Delete
f.close()
[185] Fix | Delete
[186] Fix | Delete
if __name__ == '__main__':
[187] Fix | Delete
_test()
[188] Fix | Delete
"""
[189] Fix | Delete
[190] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function