blob: 6aab1cd5f1864d373b9c9ffb4e858c31e994f9e8 [file] [log] [blame]
Guido van Rossumc75db0b1996-08-26 16:33:30 +00001"""Generic MIME writer.
2
3Classes:
4
5MimeWriter - the only thing here.
6
7"""
8
Guido van Rossumc75db0b1996-08-26 16:33:30 +00009
10import string
11import mimetools
12
Skip Montanaroe99d5ea2001-01-20 19:54:20 +000013__all__ = ["MimeWriter"]
Guido van Rossumc75db0b1996-08-26 16:33:30 +000014
15class MimeWriter:
16
17 """Generic MIME writer.
18
19 Methods:
20
21 __init__()
22 addheader()
23 flushheaders()
24 startbody()
25 startmultipartbody()
26 nextpart()
27 lastpart()
28
29 A MIME writer is much more primitive than a MIME parser. It
30 doesn't seek around on the output file, and it doesn't use large
31 amounts of buffer space, so you have to write the parts in the
32 order they should occur on the output file. It does buffer the
33 headers you add, allowing you to rearrange their order.
Tim Peters07e99cb2001-01-14 23:47:14 +000034
Guido van Rossumc75db0b1996-08-26 16:33:30 +000035 General usage is:
36
37 f = <open the output file>
38 w = MimeWriter(f)
39 ...call w.addheader(key, value) 0 or more times...
40
41 followed by either:
42
43 f = w.startbody(content_type)
44 ...call f.write(data) for body data...
45
46 or:
47
48 w.startmultipartbody(subtype)
49 for each part:
50 subwriter = w.nextpart()
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000051 ...use the subwriter's methods to create the subpart...
Guido van Rossumc75db0b1996-08-26 16:33:30 +000052 w.lastpart()
53
54 The subwriter is another MimeWriter instance, and should be
55 treated in the same way as the toplevel MimeWriter. This way,
56 writing recursive body parts is easy.
57
58 Warning: don't forget to call lastpart()!
59
60 XXX There should be more state so calls made in the wrong order
61 are detected.
62
63 Some special cases:
64
65 - startbody() just returns the file passed to the constructor;
66 but don't use this knowledge, as it may be changed.
67
68 - startmultipartbody() actually returns a file as well;
69 this can be used to write the initial 'if you can read this your
70 mailer is not MIME-aware' message.
71
72 - If you call flushheaders(), the headers accumulated so far are
73 written out (and forgotten); this is useful if you don't need a
74 body part at all, e.g. for a subpart of type message/rfc822
75 that's (mis)used to store some header-like information.
76
77 - Passing a keyword argument 'prefix=<flag>' to addheader(),
78 start*body() affects where the header is inserted; 0 means
79 append at the end, 1 means insert at the start; default is
80 append for addheader(), but insert for start*body(), which use
81 it to determine where the Content-Type header goes.
82
83 """
84
85 def __init__(self, fp):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000086 self._fp = fp
87 self._headers = []
Guido van Rossumc75db0b1996-08-26 16:33:30 +000088
89 def addheader(self, key, value, prefix=0):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000090 lines = string.splitfields(value, "\n")
91 while lines and not lines[-1]: del lines[-1]
92 while lines and not lines[0]: del lines[0]
93 for i in range(1, len(lines)):
94 lines[i] = " " + string.strip(lines[i])
95 value = string.joinfields(lines, "\n") + "\n"
96 line = key + ": " + value
97 if prefix:
98 self._headers.insert(0, line)
99 else:
100 self._headers.append(line)
Guido van Rossumc75db0b1996-08-26 16:33:30 +0000101
102 def flushheaders(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000103 self._fp.writelines(self._headers)
104 self._headers = []
Guido van Rossumc75db0b1996-08-26 16:33:30 +0000105
106 def startbody(self, ctype, plist=[], prefix=1):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000107 for name, value in plist:
108 ctype = ctype + ';\n %s=\"%s\"' % (name, value)
109 self.addheader("Content-Type", ctype, prefix=prefix)
110 self.flushheaders()
111 self._fp.write("\n")
112 return self._fp
Guido van Rossumc75db0b1996-08-26 16:33:30 +0000113
114 def startmultipartbody(self, subtype, boundary=None, plist=[], prefix=1):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000115 self._boundary = boundary or mimetools.choose_boundary()
116 return self.startbody("multipart/" + subtype,
117 [("boundary", self._boundary)] + plist,
118 prefix=prefix)
Guido van Rossumc75db0b1996-08-26 16:33:30 +0000119
120 def nextpart(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000121 self._fp.write("\n--" + self._boundary + "\n")
122 return self.__class__(self._fp)
Guido van Rossumc75db0b1996-08-26 16:33:30 +0000123
124 def lastpart(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000125 self._fp.write("\n--" + self._boundary + "--\n")
Guido van Rossumc75db0b1996-08-26 16:33:30 +0000126
127
128if __name__ == '__main__':
Guido van Rossum5fd90681998-04-23 13:34:57 +0000129 import test.test_MimeWriter