11"""Generic MIME writer.
22
3- Classes:
4-
5- MimeWriter - the only thing here.
3+ This module defines the class MimeWriter. The MimeWriter class implements
4+ a basic formatter for creating MIME multi-part files. It doesn't seek around
5+ the output file nor does it use large amounts of buffer space. You must write
6+ the parts out in the order that they should occur in the final file.
7+ MimeWriter does buffer the headers you add, allowing you to rearrange their
8+ order.
69
710"""
811
@@ -86,6 +89,14 @@ def __init__(self, fp):
8689 self ._headers = []
8790
8891 def addheader (self , key , value , prefix = 0 ):
92+ """Add a header line to the MIME message.
93+
94+ The key is the name of the header, where the value obviously provides
95+ the value of the header. The optional argument prefix determines
96+ where the header is inserted; 0 means append at the end, 1 means
97+ insert at the start. The default is to append.
98+
99+ """
89100 lines = value .split ("\n " )
90101 while lines and not lines [- 1 ]: del lines [- 1 ]
91102 while lines and not lines [0 ]: del lines [0 ]
@@ -99,10 +110,26 @@ def addheader(self, key, value, prefix=0):
99110 self ._headers .append (line )
100111
101112 def flushheaders (self ):
113+ """Writes out and forgets all headers accumulated so far.
114+
115+ This is useful if you don't need a body part at all; for example,
116+ for a subpart of type message/rfc822 that's (mis)used to store some
117+ header-like information.
118+
119+ """
102120 self ._fp .writelines (self ._headers )
103121 self ._headers = []
104122
105123 def startbody (self , ctype , plist = [], prefix = 1 ):
124+ """Returns a file-like object for writing the body of the message.
125+
126+ The content-type is set to the provided ctype, and the optional
127+ parameter, plist, provides additional parameters for the
128+ content-type declaration. The optional argument prefix determines
129+ where the header is inserted; 0 means append at the end, 1 means
130+ insert at the start. The default is to insert at the start.
131+
132+ """
106133 for name , value in plist :
107134 ctype = ctype + ';\n %s=\" %s\" ' % (name , value )
108135 self .addheader ("Content-Type" , ctype , prefix = prefix )
@@ -111,16 +138,42 @@ def startbody(self, ctype, plist=[], prefix=1):
111138 return self ._fp
112139
113140 def startmultipartbody (self , subtype , boundary = None , plist = [], prefix = 1 ):
141+ """Returns a file-like object for writing the body of the message.
142+
143+ Additionally, this method initializes the multi-part code, where the
144+ subtype parameter provides the multipart subtype, the boundary
145+ parameter may provide a user-defined boundary specification, and the
146+ plist parameter provides optional parameters for the subtype. The
147+ optional argument, prefix, determines where the header is inserted;
148+ 0 means append at the end, 1 means insert at the start. The default
149+ is to insert at the start. Subparts should be created using the
150+ nextpart() method.
151+
152+ """
114153 self ._boundary = boundary or mimetools .choose_boundary ()
115154 return self .startbody ("multipart/" + subtype ,
116155 [("boundary" , self ._boundary )] + plist ,
117156 prefix = prefix )
118157
119158 def nextpart (self ):
159+ """Returns a new instance of MimeWriter which represents an
160+ individual part in a multipart message.
161+
162+ This may be used to write the part as well as used for creating
163+ recursively complex multipart messages. The message must first be
164+ initialized with the startmultipartbody() method before using the
165+ nextpart() method.
166+
167+ """
120168 self ._fp .write ("\n --" + self ._boundary + "\n " )
121169 return self .__class__ (self ._fp )
122170
123171 def lastpart (self ):
172+ """This is used to designate the last part of a multipart message.
173+
174+ It should always be used when writing multipart messages.
175+
176+ """
124177 self ._fp .write ("\n --" + self ._boundary + "--\n " )
125178
126179
0 commit comments