Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python2....
File: MimeWriter.py
"""Generic MIME writer.
[0] Fix | Delete
[1] Fix | Delete
This module defines the class MimeWriter. The MimeWriter class implements
[2] Fix | Delete
a basic formatter for creating MIME multi-part files. It doesn't seek around
[3] Fix | Delete
the output file nor does it use large amounts of buffer space. You must write
[4] Fix | Delete
the parts out in the order that they should occur in the final file.
[5] Fix | Delete
MimeWriter does buffer the headers you add, allowing you to rearrange their
[6] Fix | Delete
order.
[7] Fix | Delete
[8] Fix | Delete
"""
[9] Fix | Delete
[10] Fix | Delete
[11] Fix | Delete
import mimetools
[12] Fix | Delete
[13] Fix | Delete
__all__ = ["MimeWriter"]
[14] Fix | Delete
[15] Fix | Delete
import warnings
[16] Fix | Delete
[17] Fix | Delete
warnings.warn("the MimeWriter module is deprecated; use the email package instead",
[18] Fix | Delete
DeprecationWarning, 2)
[19] Fix | Delete
[20] Fix | Delete
class MimeWriter:
[21] Fix | Delete
[22] Fix | Delete
"""Generic MIME writer.
[23] Fix | Delete
[24] Fix | Delete
Methods:
[25] Fix | Delete
[26] Fix | Delete
__init__()
[27] Fix | Delete
addheader()
[28] Fix | Delete
flushheaders()
[29] Fix | Delete
startbody()
[30] Fix | Delete
startmultipartbody()
[31] Fix | Delete
nextpart()
[32] Fix | Delete
lastpart()
[33] Fix | Delete
[34] Fix | Delete
A MIME writer is much more primitive than a MIME parser. It
[35] Fix | Delete
doesn't seek around on the output file, and it doesn't use large
[36] Fix | Delete
amounts of buffer space, so you have to write the parts in the
[37] Fix | Delete
order they should occur on the output file. It does buffer the
[38] Fix | Delete
headers you add, allowing you to rearrange their order.
[39] Fix | Delete
[40] Fix | Delete
General usage is:
[41] Fix | Delete
[42] Fix | Delete
f = <open the output file>
[43] Fix | Delete
w = MimeWriter(f)
[44] Fix | Delete
...call w.addheader(key, value) 0 or more times...
[45] Fix | Delete
[46] Fix | Delete
followed by either:
[47] Fix | Delete
[48] Fix | Delete
f = w.startbody(content_type)
[49] Fix | Delete
...call f.write(data) for body data...
[50] Fix | Delete
[51] Fix | Delete
or:
[52] Fix | Delete
[53] Fix | Delete
w.startmultipartbody(subtype)
[54] Fix | Delete
for each part:
[55] Fix | Delete
subwriter = w.nextpart()
[56] Fix | Delete
...use the subwriter's methods to create the subpart...
[57] Fix | Delete
w.lastpart()
[58] Fix | Delete
[59] Fix | Delete
The subwriter is another MimeWriter instance, and should be
[60] Fix | Delete
treated in the same way as the toplevel MimeWriter. This way,
[61] Fix | Delete
writing recursive body parts is easy.
[62] Fix | Delete
[63] Fix | Delete
Warning: don't forget to call lastpart()!
[64] Fix | Delete
[65] Fix | Delete
XXX There should be more state so calls made in the wrong order
[66] Fix | Delete
are detected.
[67] Fix | Delete
[68] Fix | Delete
Some special cases:
[69] Fix | Delete
[70] Fix | Delete
- startbody() just returns the file passed to the constructor;
[71] Fix | Delete
but don't use this knowledge, as it may be changed.
[72] Fix | Delete
[73] Fix | Delete
- startmultipartbody() actually returns a file as well;
[74] Fix | Delete
this can be used to write the initial 'if you can read this your
[75] Fix | Delete
mailer is not MIME-aware' message.
[76] Fix | Delete
[77] Fix | Delete
- If you call flushheaders(), the headers accumulated so far are
[78] Fix | Delete
written out (and forgotten); this is useful if you don't need a
[79] Fix | Delete
body part at all, e.g. for a subpart of type message/rfc822
[80] Fix | Delete
that's (mis)used to store some header-like information.
[81] Fix | Delete
[82] Fix | Delete
- Passing a keyword argument 'prefix=<flag>' to addheader(),
[83] Fix | Delete
start*body() affects where the header is inserted; 0 means
[84] Fix | Delete
append at the end, 1 means insert at the start; default is
[85] Fix | Delete
append for addheader(), but insert for start*body(), which use
[86] Fix | Delete
it to determine where the Content-Type header goes.
[87] Fix | Delete
[88] Fix | Delete
"""
[89] Fix | Delete
[90] Fix | Delete
def __init__(self, fp):
[91] Fix | Delete
self._fp = fp
[92] Fix | Delete
self._headers = []
[93] Fix | Delete
[94] Fix | Delete
def addheader(self, key, value, prefix=0):
[95] Fix | Delete
"""Add a header line to the MIME message.
[96] Fix | Delete
[97] Fix | Delete
The key is the name of the header, where the value obviously provides
[98] Fix | Delete
the value of the header. The optional argument prefix determines
[99] Fix | Delete
where the header is inserted; 0 means append at the end, 1 means
[100] Fix | Delete
insert at the start. The default is to append.
[101] Fix | Delete
[102] Fix | Delete
"""
[103] Fix | Delete
lines = value.split("\n")
[104] Fix | Delete
while lines and not lines[-1]: del lines[-1]
[105] Fix | Delete
while lines and not lines[0]: del lines[0]
[106] Fix | Delete
for i in range(1, len(lines)):
[107] Fix | Delete
lines[i] = " " + lines[i].strip()
[108] Fix | Delete
value = "\n".join(lines) + "\n"
[109] Fix | Delete
line = key + ": " + value
[110] Fix | Delete
if prefix:
[111] Fix | Delete
self._headers.insert(0, line)
[112] Fix | Delete
else:
[113] Fix | Delete
self._headers.append(line)
[114] Fix | Delete
[115] Fix | Delete
def flushheaders(self):
[116] Fix | Delete
"""Writes out and forgets all headers accumulated so far.
[117] Fix | Delete
[118] Fix | Delete
This is useful if you don't need a body part at all; for example,
[119] Fix | Delete
for a subpart of type message/rfc822 that's (mis)used to store some
[120] Fix | Delete
header-like information.
[121] Fix | Delete
[122] Fix | Delete
"""
[123] Fix | Delete
self._fp.writelines(self._headers)
[124] Fix | Delete
self._headers = []
[125] Fix | Delete
[126] Fix | Delete
def startbody(self, ctype, plist=[], prefix=1):
[127] Fix | Delete
"""Returns a file-like object for writing the body of the message.
[128] Fix | Delete
[129] Fix | Delete
The content-type is set to the provided ctype, and the optional
[130] Fix | Delete
parameter, plist, provides additional parameters for the
[131] Fix | Delete
content-type declaration. The optional argument prefix determines
[132] Fix | Delete
where the header is inserted; 0 means append at the end, 1 means
[133] Fix | Delete
insert at the start. The default is to insert at the start.
[134] Fix | Delete
[135] Fix | Delete
"""
[136] Fix | Delete
for name, value in plist:
[137] Fix | Delete
ctype = ctype + ';\n %s=\"%s\"' % (name, value)
[138] Fix | Delete
self.addheader("Content-Type", ctype, prefix=prefix)
[139] Fix | Delete
self.flushheaders()
[140] Fix | Delete
self._fp.write("\n")
[141] Fix | Delete
return self._fp
[142] Fix | Delete
[143] Fix | Delete
def startmultipartbody(self, subtype, boundary=None, plist=[], prefix=1):
[144] Fix | Delete
"""Returns a file-like object for writing the body of the message.
[145] Fix | Delete
[146] Fix | Delete
Additionally, this method initializes the multi-part code, where the
[147] Fix | Delete
subtype parameter provides the multipart subtype, the boundary
[148] Fix | Delete
parameter may provide a user-defined boundary specification, and the
[149] Fix | Delete
plist parameter provides optional parameters for the subtype. The
[150] Fix | Delete
optional argument, prefix, determines where the header is inserted;
[151] Fix | Delete
0 means append at the end, 1 means insert at the start. The default
[152] Fix | Delete
is to insert at the start. Subparts should be created using the
[153] Fix | Delete
nextpart() method.
[154] Fix | Delete
[155] Fix | Delete
"""
[156] Fix | Delete
self._boundary = boundary or mimetools.choose_boundary()
[157] Fix | Delete
return self.startbody("multipart/" + subtype,
[158] Fix | Delete
[("boundary", self._boundary)] + plist,
[159] Fix | Delete
prefix=prefix)
[160] Fix | Delete
[161] Fix | Delete
def nextpart(self):
[162] Fix | Delete
"""Returns a new instance of MimeWriter which represents an
[163] Fix | Delete
individual part in a multipart message.
[164] Fix | Delete
[165] Fix | Delete
This may be used to write the part as well as used for creating
[166] Fix | Delete
recursively complex multipart messages. The message must first be
[167] Fix | Delete
initialized with the startmultipartbody() method before using the
[168] Fix | Delete
nextpart() method.
[169] Fix | Delete
[170] Fix | Delete
"""
[171] Fix | Delete
self._fp.write("\n--" + self._boundary + "\n")
[172] Fix | Delete
return self.__class__(self._fp)
[173] Fix | Delete
[174] Fix | Delete
def lastpart(self):
[175] Fix | Delete
"""This is used to designate the last part of a multipart message.
[176] Fix | Delete
[177] Fix | Delete
It should always be used when writing multipart messages.
[178] Fix | Delete
[179] Fix | Delete
"""
[180] Fix | Delete
self._fp.write("\n--" + self._boundary + "--\n")
[181] Fix | Delete
[182] Fix | Delete
[183] Fix | Delete
if __name__ == '__main__':
[184] Fix | Delete
import test.test_MimeWriter
[185] Fix | Delete
[186] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function