Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python2....
File: multifile.py
"""A readline()-style interface to the parts of a multipart message.
[0] Fix | Delete
[1] Fix | Delete
The MultiFile class makes each part of a multipart message "feel" like
[2] Fix | Delete
an ordinary file, as long as you use fp.readline(). Allows recursive
[3] Fix | Delete
use, for nested multipart messages. Probably best used together
[4] Fix | Delete
with module mimetools.
[5] Fix | Delete
[6] Fix | Delete
Suggested use:
[7] Fix | Delete
[8] Fix | Delete
real_fp = open(...)
[9] Fix | Delete
fp = MultiFile(real_fp)
[10] Fix | Delete
[11] Fix | Delete
"read some lines from fp"
[12] Fix | Delete
fp.push(separator)
[13] Fix | Delete
while 1:
[14] Fix | Delete
"read lines from fp until it returns an empty string" (A)
[15] Fix | Delete
if not fp.next(): break
[16] Fix | Delete
fp.pop()
[17] Fix | Delete
"read remaining lines from fp until it returns an empty string"
[18] Fix | Delete
[19] Fix | Delete
The latter sequence may be used recursively at (A).
[20] Fix | Delete
It is also allowed to use multiple push()...pop() sequences.
[21] Fix | Delete
[22] Fix | Delete
If seekable is given as 0, the class code will not do the bookkeeping
[23] Fix | Delete
it normally attempts in order to make seeks relative to the beginning of the
[24] Fix | Delete
current file part. This may be useful when using MultiFile with a non-
[25] Fix | Delete
seekable stream object.
[26] Fix | Delete
"""
[27] Fix | Delete
from warnings import warn
[28] Fix | Delete
warn("the multifile module has been deprecated since Python 2.5",
[29] Fix | Delete
DeprecationWarning, stacklevel=2)
[30] Fix | Delete
del warn
[31] Fix | Delete
[32] Fix | Delete
__all__ = ["MultiFile","Error"]
[33] Fix | Delete
[34] Fix | Delete
class Error(Exception):
[35] Fix | Delete
pass
[36] Fix | Delete
[37] Fix | Delete
class MultiFile:
[38] Fix | Delete
[39] Fix | Delete
seekable = 0
[40] Fix | Delete
[41] Fix | Delete
def __init__(self, fp, seekable=1):
[42] Fix | Delete
self.fp = fp
[43] Fix | Delete
self.stack = []
[44] Fix | Delete
self.level = 0
[45] Fix | Delete
self.last = 0
[46] Fix | Delete
if seekable:
[47] Fix | Delete
self.seekable = 1
[48] Fix | Delete
self.start = self.fp.tell()
[49] Fix | Delete
self.posstack = []
[50] Fix | Delete
[51] Fix | Delete
def tell(self):
[52] Fix | Delete
if self.level > 0:
[53] Fix | Delete
return self.lastpos
[54] Fix | Delete
return self.fp.tell() - self.start
[55] Fix | Delete
[56] Fix | Delete
def seek(self, pos, whence=0):
[57] Fix | Delete
here = self.tell()
[58] Fix | Delete
if whence:
[59] Fix | Delete
if whence == 1:
[60] Fix | Delete
pos = pos + here
[61] Fix | Delete
elif whence == 2:
[62] Fix | Delete
if self.level > 0:
[63] Fix | Delete
pos = pos + self.lastpos
[64] Fix | Delete
else:
[65] Fix | Delete
raise Error, "can't use whence=2 yet"
[66] Fix | Delete
if not 0 <= pos <= here or \
[67] Fix | Delete
self.level > 0 and pos > self.lastpos:
[68] Fix | Delete
raise Error, 'bad MultiFile.seek() call'
[69] Fix | Delete
self.fp.seek(pos + self.start)
[70] Fix | Delete
self.level = 0
[71] Fix | Delete
self.last = 0
[72] Fix | Delete
[73] Fix | Delete
def readline(self):
[74] Fix | Delete
if self.level > 0:
[75] Fix | Delete
return ''
[76] Fix | Delete
line = self.fp.readline()
[77] Fix | Delete
# Real EOF?
[78] Fix | Delete
if not line:
[79] Fix | Delete
self.level = len(self.stack)
[80] Fix | Delete
self.last = (self.level > 0)
[81] Fix | Delete
if self.last:
[82] Fix | Delete
raise Error, 'sudden EOF in MultiFile.readline()'
[83] Fix | Delete
return ''
[84] Fix | Delete
assert self.level == 0
[85] Fix | Delete
# Fast check to see if this is just data
[86] Fix | Delete
if self.is_data(line):
[87] Fix | Delete
return line
[88] Fix | Delete
else:
[89] Fix | Delete
# Ignore trailing whitespace on marker lines
[90] Fix | Delete
marker = line.rstrip()
[91] Fix | Delete
# No? OK, try to match a boundary.
[92] Fix | Delete
# Return the line (unstripped) if we don't.
[93] Fix | Delete
for i, sep in enumerate(reversed(self.stack)):
[94] Fix | Delete
if marker == self.section_divider(sep):
[95] Fix | Delete
self.last = 0
[96] Fix | Delete
break
[97] Fix | Delete
elif marker == self.end_marker(sep):
[98] Fix | Delete
self.last = 1
[99] Fix | Delete
break
[100] Fix | Delete
else:
[101] Fix | Delete
return line
[102] Fix | Delete
# We only get here if we see a section divider or EOM line
[103] Fix | Delete
if self.seekable:
[104] Fix | Delete
self.lastpos = self.tell() - len(line)
[105] Fix | Delete
self.level = i+1
[106] Fix | Delete
if self.level > 1:
[107] Fix | Delete
raise Error,'Missing endmarker in MultiFile.readline()'
[108] Fix | Delete
return ''
[109] Fix | Delete
[110] Fix | Delete
def readlines(self):
[111] Fix | Delete
list = []
[112] Fix | Delete
while 1:
[113] Fix | Delete
line = self.readline()
[114] Fix | Delete
if not line: break
[115] Fix | Delete
list.append(line)
[116] Fix | Delete
return list
[117] Fix | Delete
[118] Fix | Delete
def read(self): # Note: no size argument -- read until EOF only!
[119] Fix | Delete
return ''.join(self.readlines())
[120] Fix | Delete
[121] Fix | Delete
def next(self):
[122] Fix | Delete
while self.readline(): pass
[123] Fix | Delete
if self.level > 1 or self.last:
[124] Fix | Delete
return 0
[125] Fix | Delete
self.level = 0
[126] Fix | Delete
self.last = 0
[127] Fix | Delete
if self.seekable:
[128] Fix | Delete
self.start = self.fp.tell()
[129] Fix | Delete
return 1
[130] Fix | Delete
[131] Fix | Delete
def push(self, sep):
[132] Fix | Delete
if self.level > 0:
[133] Fix | Delete
raise Error, 'bad MultiFile.push() call'
[134] Fix | Delete
self.stack.append(sep)
[135] Fix | Delete
if self.seekable:
[136] Fix | Delete
self.posstack.append(self.start)
[137] Fix | Delete
self.start = self.fp.tell()
[138] Fix | Delete
[139] Fix | Delete
def pop(self):
[140] Fix | Delete
if self.stack == []:
[141] Fix | Delete
raise Error, 'bad MultiFile.pop() call'
[142] Fix | Delete
if self.level <= 1:
[143] Fix | Delete
self.last = 0
[144] Fix | Delete
else:
[145] Fix | Delete
abslastpos = self.lastpos + self.start
[146] Fix | Delete
self.level = max(0, self.level - 1)
[147] Fix | Delete
self.stack.pop()
[148] Fix | Delete
if self.seekable:
[149] Fix | Delete
self.start = self.posstack.pop()
[150] Fix | Delete
if self.level > 0:
[151] Fix | Delete
self.lastpos = abslastpos - self.start
[152] Fix | Delete
[153] Fix | Delete
def is_data(self, line):
[154] Fix | Delete
return line[:2] != '--'
[155] Fix | Delete
[156] Fix | Delete
def section_divider(self, str):
[157] Fix | Delete
return "--" + str
[158] Fix | Delete
[159] Fix | Delete
def end_marker(self, str):
[160] Fix | Delete
return "--" + str + "--"
[161] Fix | Delete
[162] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function