1 """Generic MIME writer.
5 MimeWriter - the only thing here.
12 __all__
= ["MimeWriter"]
16 """Generic MIME writer.
28 A MIME writer is much more primitive than a MIME parser. It
29 doesn't seek around on the output file, and it doesn't use large
30 amounts of buffer space, so you have to write the parts in the
31 order they should occur on the output file. It does buffer the
32 headers you add, allowing you to rearrange their order.
36 f = <open the output file>
38 ...call w.addheader(key, value) 0 or more times...
42 f = w.startbody(content_type)
43 ...call f.write(data) for body data...
47 w.startmultipartbody(subtype)
49 subwriter = w.nextpart()
50 ...use the subwriter's methods to create the subpart...
53 The subwriter is another MimeWriter instance, and should be
54 treated in the same way as the toplevel MimeWriter. This way,
55 writing recursive body parts is easy.
57 Warning: don't forget to call lastpart()!
59 XXX There should be more state so calls made in the wrong order
64 - startbody() just returns the file passed to the constructor;
65 but don't use this knowledge, as it may be changed.
67 - startmultipartbody() actually returns a file as well;
68 this can be used to write the initial 'if you can read this your
69 mailer is not MIME-aware' message.
71 - If you call flushheaders(), the headers accumulated so far are
72 written out (and forgotten); this is useful if you don't need a
73 body part at all, e.g. for a subpart of type message/rfc822
74 that's (mis)used to store some header-like information.
76 - Passing a keyword argument 'prefix=<flag>' to addheader(),
77 start*body() affects where the header is inserted; 0 means
78 append at the end, 1 means insert at the start; default is
79 append for addheader(), but insert for start*body(), which use
80 it to determine where the Content-Type header goes.
84 def __init__(self
, fp
):
88 def addheader(self
, key
, value
, prefix
=0):
89 lines
= value
.split("\n")
90 while lines
and not lines
[-1]: del lines
[-1]
91 while lines
and not lines
[0]: del lines
[0]
92 for i
in range(1, len(lines
)):
93 lines
[i
] = " " + lines
[i
].strip()
94 value
= "\n".join(lines
) + "\n"
95 line
= key
+ ": " + value
97 self
._headers
.insert(0, line
)
99 self
._headers
.append(line
)
101 def flushheaders(self
):
102 self
._fp
.writelines(self
._headers
)
105 def startbody(self
, ctype
, plist
=[], prefix
=1):
106 for name
, value
in plist
:
107 ctype
= ctype
+ ';\n %s=\"%s\"' % (name
, value
)
108 self
.addheader("Content-Type", ctype
, prefix
=prefix
)
113 def startmultipartbody(self
, subtype
, boundary
=None, plist
=[], prefix
=1):
114 self
._boundary
= boundary
or mimetools
.choose_boundary()
115 return self
.startbody("multipart/" + subtype
,
116 [("boundary", self
._boundary
)] + plist
,
120 self
._fp
.write("\n--" + self
._boundary
+ "\n")
121 return self
.__class
__(self
._fp
)
124 self
._fp
.write("\n--" + self
._boundary
+ "--\n")
127 if __name__
== '__main__':
128 import test
.test_MimeWriter