Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ExeBy/smexe_ro.../usr/lib64/python2....
File: quopri.py
#! /usr/bin/python2.7
[0] Fix | Delete
[1] Fix | Delete
"""Conversions to/from quoted-printable transport encoding as per RFC 1521."""
[2] Fix | Delete
[3] Fix | Delete
# (Dec 1991 version).
[4] Fix | Delete
[5] Fix | Delete
__all__ = ["encode", "decode", "encodestring", "decodestring"]
[6] Fix | Delete
[7] Fix | Delete
ESCAPE = '='
[8] Fix | Delete
MAXLINESIZE = 76
[9] Fix | Delete
HEX = '0123456789ABCDEF'
[10] Fix | Delete
EMPTYSTRING = ''
[11] Fix | Delete
[12] Fix | Delete
try:
[13] Fix | Delete
from binascii import a2b_qp, b2a_qp
[14] Fix | Delete
except ImportError:
[15] Fix | Delete
a2b_qp = None
[16] Fix | Delete
b2a_qp = None
[17] Fix | Delete
[18] Fix | Delete
[19] Fix | Delete
def needsquoting(c, quotetabs, header):
[20] Fix | Delete
"""Decide whether a particular character needs to be quoted.
[21] Fix | Delete
[22] Fix | Delete
The 'quotetabs' flag indicates whether embedded tabs and spaces should be
[23] Fix | Delete
quoted. Note that line-ending tabs and spaces are always encoded, as per
[24] Fix | Delete
RFC 1521.
[25] Fix | Delete
"""
[26] Fix | Delete
if c in ' \t':
[27] Fix | Delete
return quotetabs
[28] Fix | Delete
# if header, we have to escape _ because _ is used to escape space
[29] Fix | Delete
if c == '_':
[30] Fix | Delete
return header
[31] Fix | Delete
return c == ESCAPE or not (' ' <= c <= '~')
[32] Fix | Delete
[33] Fix | Delete
def quote(c):
[34] Fix | Delete
"""Quote a single character."""
[35] Fix | Delete
i = ord(c)
[36] Fix | Delete
return ESCAPE + HEX[i//16] + HEX[i%16]
[37] Fix | Delete
[38] Fix | Delete
[39] Fix | Delete
[40] Fix | Delete
def encode(input, output, quotetabs, header = 0):
[41] Fix | Delete
"""Read 'input', apply quoted-printable encoding, and write to 'output'.
[42] Fix | Delete
[43] Fix | Delete
'input' and 'output' are files with readline() and write() methods.
[44] Fix | Delete
The 'quotetabs' flag indicates whether embedded tabs and spaces should be
[45] Fix | Delete
quoted. Note that line-ending tabs and spaces are always encoded, as per
[46] Fix | Delete
RFC 1521.
[47] Fix | Delete
The 'header' flag indicates whether we are encoding spaces as _ as per
[48] Fix | Delete
RFC 1522.
[49] Fix | Delete
"""
[50] Fix | Delete
[51] Fix | Delete
if b2a_qp is not None:
[52] Fix | Delete
data = input.read()
[53] Fix | Delete
odata = b2a_qp(data, quotetabs = quotetabs, header = header)
[54] Fix | Delete
output.write(odata)
[55] Fix | Delete
return
[56] Fix | Delete
[57] Fix | Delete
def write(s, output=output, lineEnd='\n'):
[58] Fix | Delete
# RFC 1521 requires that the line ending in a space or tab must have
[59] Fix | Delete
# that trailing character encoded.
[60] Fix | Delete
if s and s[-1:] in ' \t':
[61] Fix | Delete
output.write(s[:-1] + quote(s[-1]) + lineEnd)
[62] Fix | Delete
elif s == '.':
[63] Fix | Delete
output.write(quote(s) + lineEnd)
[64] Fix | Delete
else:
[65] Fix | Delete
output.write(s + lineEnd)
[66] Fix | Delete
[67] Fix | Delete
prevline = None
[68] Fix | Delete
while 1:
[69] Fix | Delete
line = input.readline()
[70] Fix | Delete
if not line:
[71] Fix | Delete
break
[72] Fix | Delete
outline = []
[73] Fix | Delete
# Strip off any readline induced trailing newline
[74] Fix | Delete
stripped = ''
[75] Fix | Delete
if line[-1:] == '\n':
[76] Fix | Delete
line = line[:-1]
[77] Fix | Delete
stripped = '\n'
[78] Fix | Delete
# Calculate the un-length-limited encoded line
[79] Fix | Delete
for c in line:
[80] Fix | Delete
if needsquoting(c, quotetabs, header):
[81] Fix | Delete
c = quote(c)
[82] Fix | Delete
if header and c == ' ':
[83] Fix | Delete
outline.append('_')
[84] Fix | Delete
else:
[85] Fix | Delete
outline.append(c)
[86] Fix | Delete
# First, write out the previous line
[87] Fix | Delete
if prevline is not None:
[88] Fix | Delete
write(prevline)
[89] Fix | Delete
# Now see if we need any soft line breaks because of RFC-imposed
[90] Fix | Delete
# length limitations. Then do the thisline->prevline dance.
[91] Fix | Delete
thisline = EMPTYSTRING.join(outline)
[92] Fix | Delete
while len(thisline) > MAXLINESIZE:
[93] Fix | Delete
# Don't forget to include the soft line break `=' sign in the
[94] Fix | Delete
# length calculation!
[95] Fix | Delete
write(thisline[:MAXLINESIZE-1], lineEnd='=\n')
[96] Fix | Delete
thisline = thisline[MAXLINESIZE-1:]
[97] Fix | Delete
# Write out the current line
[98] Fix | Delete
prevline = thisline
[99] Fix | Delete
# Write out the last line, without a trailing newline
[100] Fix | Delete
if prevline is not None:
[101] Fix | Delete
write(prevline, lineEnd=stripped)
[102] Fix | Delete
[103] Fix | Delete
def encodestring(s, quotetabs = 0, header = 0):
[104] Fix | Delete
if b2a_qp is not None:
[105] Fix | Delete
return b2a_qp(s, quotetabs = quotetabs, header = header)
[106] Fix | Delete
from cStringIO import StringIO
[107] Fix | Delete
infp = StringIO(s)
[108] Fix | Delete
outfp = StringIO()
[109] Fix | Delete
encode(infp, outfp, quotetabs, header)
[110] Fix | Delete
return outfp.getvalue()
[111] Fix | Delete
[112] Fix | Delete
[113] Fix | Delete
[114] Fix | Delete
def decode(input, output, header = 0):
[115] Fix | Delete
"""Read 'input', apply quoted-printable decoding, and write to 'output'.
[116] Fix | Delete
'input' and 'output' are files with readline() and write() methods.
[117] Fix | Delete
If 'header' is true, decode underscore as space (per RFC 1522)."""
[118] Fix | Delete
[119] Fix | Delete
if a2b_qp is not None:
[120] Fix | Delete
data = input.read()
[121] Fix | Delete
odata = a2b_qp(data, header = header)
[122] Fix | Delete
output.write(odata)
[123] Fix | Delete
return
[124] Fix | Delete
[125] Fix | Delete
new = ''
[126] Fix | Delete
while 1:
[127] Fix | Delete
line = input.readline()
[128] Fix | Delete
if not line: break
[129] Fix | Delete
i, n = 0, len(line)
[130] Fix | Delete
if n > 0 and line[n-1] == '\n':
[131] Fix | Delete
partial = 0; n = n-1
[132] Fix | Delete
# Strip trailing whitespace
[133] Fix | Delete
while n > 0 and line[n-1] in " \t\r":
[134] Fix | Delete
n = n-1
[135] Fix | Delete
else:
[136] Fix | Delete
partial = 1
[137] Fix | Delete
while i < n:
[138] Fix | Delete
c = line[i]
[139] Fix | Delete
if c == '_' and header:
[140] Fix | Delete
new = new + ' '; i = i+1
[141] Fix | Delete
elif c != ESCAPE:
[142] Fix | Delete
new = new + c; i = i+1
[143] Fix | Delete
elif i+1 == n and not partial:
[144] Fix | Delete
partial = 1; break
[145] Fix | Delete
elif i+1 < n and line[i+1] == ESCAPE:
[146] Fix | Delete
new = new + ESCAPE; i = i+2
[147] Fix | Delete
elif i+2 < n and ishex(line[i+1]) and ishex(line[i+2]):
[148] Fix | Delete
new = new + chr(unhex(line[i+1:i+3])); i = i+3
[149] Fix | Delete
else: # Bad escape sequence -- leave it in
[150] Fix | Delete
new = new + c; i = i+1
[151] Fix | Delete
if not partial:
[152] Fix | Delete
output.write(new + '\n')
[153] Fix | Delete
new = ''
[154] Fix | Delete
if new:
[155] Fix | Delete
output.write(new)
[156] Fix | Delete
[157] Fix | Delete
def decodestring(s, header = 0):
[158] Fix | Delete
if a2b_qp is not None:
[159] Fix | Delete
return a2b_qp(s, header = header)
[160] Fix | Delete
from cStringIO import StringIO
[161] Fix | Delete
infp = StringIO(s)
[162] Fix | Delete
outfp = StringIO()
[163] Fix | Delete
decode(infp, outfp, header = header)
[164] Fix | Delete
return outfp.getvalue()
[165] Fix | Delete
[166] Fix | Delete
[167] Fix | Delete
[168] Fix | Delete
# Other helper functions
[169] Fix | Delete
def ishex(c):
[170] Fix | Delete
"""Return true if the character 'c' is a hexadecimal digit."""
[171] Fix | Delete
return '0' <= c <= '9' or 'a' <= c <= 'f' or 'A' <= c <= 'F'
[172] Fix | Delete
[173] Fix | Delete
def unhex(s):
[174] Fix | Delete
"""Get the integer value of a hexadecimal number."""
[175] Fix | Delete
bits = 0
[176] Fix | Delete
for c in s:
[177] Fix | Delete
if '0' <= c <= '9':
[178] Fix | Delete
i = ord('0')
[179] Fix | Delete
elif 'a' <= c <= 'f':
[180] Fix | Delete
i = ord('a')-10
[181] Fix | Delete
elif 'A' <= c <= 'F':
[182] Fix | Delete
i = ord('A')-10
[183] Fix | Delete
else:
[184] Fix | Delete
break
[185] Fix | Delete
bits = bits*16 + (ord(c) - i)
[186] Fix | Delete
return bits
[187] Fix | Delete
[188] Fix | Delete
[189] Fix | Delete
[190] Fix | Delete
def main():
[191] Fix | Delete
import sys
[192] Fix | Delete
import getopt
[193] Fix | Delete
try:
[194] Fix | Delete
opts, args = getopt.getopt(sys.argv[1:], 'td')
[195] Fix | Delete
except getopt.error, msg:
[196] Fix | Delete
sys.stdout = sys.stderr
[197] Fix | Delete
print msg
[198] Fix | Delete
print "usage: quopri [-t | -d] [file] ..."
[199] Fix | Delete
print "-t: quote tabs"
[200] Fix | Delete
print "-d: decode; default encode"
[201] Fix | Delete
sys.exit(2)
[202] Fix | Delete
deco = 0
[203] Fix | Delete
tabs = 0
[204] Fix | Delete
for o, a in opts:
[205] Fix | Delete
if o == '-t': tabs = 1
[206] Fix | Delete
if o == '-d': deco = 1
[207] Fix | Delete
if tabs and deco:
[208] Fix | Delete
sys.stdout = sys.stderr
[209] Fix | Delete
print "-t and -d are mutually exclusive"
[210] Fix | Delete
sys.exit(2)
[211] Fix | Delete
if not args: args = ['-']
[212] Fix | Delete
sts = 0
[213] Fix | Delete
for file in args:
[214] Fix | Delete
if file == '-':
[215] Fix | Delete
fp = sys.stdin
[216] Fix | Delete
else:
[217] Fix | Delete
try:
[218] Fix | Delete
fp = open(file)
[219] Fix | Delete
except IOError, msg:
[220] Fix | Delete
sys.stderr.write("%s: can't open (%s)\n" % (file, msg))
[221] Fix | Delete
sts = 1
[222] Fix | Delete
continue
[223] Fix | Delete
if deco:
[224] Fix | Delete
decode(fp, sys.stdout)
[225] Fix | Delete
else:
[226] Fix | Delete
encode(fp, sys.stdout, tabs)
[227] Fix | Delete
if fp is not sys.stdin:
[228] Fix | Delete
fp.close()
[229] Fix | Delete
if sts:
[230] Fix | Delete
sys.exit(sts)
[231] Fix | Delete
[232] Fix | Delete
[233] Fix | Delete
[234] Fix | Delete
if __name__ == '__main__':
[235] Fix | Delete
main()
[236] Fix | Delete
[237] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function