annotate Lib/MimeWriter.py @ 5817:48b94de85bf3 legacy-trunk

Extensive changes by AMK.
author Guido van Rossum <guido@python.org>
date Thu, 17 Jul 1997 16:29:42 +0000
parents 3e77d6637a2b
children fb4b4d069cc7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4149
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 """Generic MIME writer.
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 Classes:
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 MimeWriter - the only thing here.
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 """
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
8
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
9
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 import string
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 import mimetools
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
12
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
13
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 class MimeWriter:
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
15
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 """Generic MIME writer.
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
17
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 Methods:
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
19
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 __init__()
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 addheader()
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 flushheaders()
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 startbody()
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 startmultipartbody()
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 nextpart()
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 lastpart()
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
27
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 A MIME writer is much more primitive than a MIME parser. It
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 doesn't seek around on the output file, and it doesn't use large
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 amounts of buffer space, so you have to write the parts in the
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 order they should occur on the output file. It does buffer the
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 headers you add, allowing you to rearrange their order.
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
33
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 General usage is:
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
35
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 f = <open the output file>
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 w = MimeWriter(f)
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 ...call w.addheader(key, value) 0 or more times...
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
39
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 followed by either:
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
41
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 f = w.startbody(content_type)
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 ...call f.write(data) for body data...
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
44
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 or:
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
46
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 w.startmultipartbody(subtype)
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 for each part:
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 subwriter = w.nextpart()
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 ...use the subwriter's methods to create the subpart...
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 w.lastpart()
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
52
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 The subwriter is another MimeWriter instance, and should be
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 treated in the same way as the toplevel MimeWriter. This way,
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 writing recursive body parts is easy.
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
56
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 Warning: don't forget to call lastpart()!
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
58
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 XXX There should be more state so calls made in the wrong order
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 are detected.
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
61
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 Some special cases:
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
63
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 - startbody() just returns the file passed to the constructor;
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 but don't use this knowledge, as it may be changed.
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
66
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 - startmultipartbody() actually returns a file as well;
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 this can be used to write the initial 'if you can read this your
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 mailer is not MIME-aware' message.
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
70
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 - If you call flushheaders(), the headers accumulated so far are
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 written out (and forgotten); this is useful if you don't need a
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 body part at all, e.g. for a subpart of type message/rfc822
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 that's (mis)used to store some header-like information.
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
75
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 - Passing a keyword argument 'prefix=<flag>' to addheader(),
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 start*body() affects where the header is inserted; 0 means
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 append at the end, 1 means insert at the start; default is
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 append for addheader(), but insert for start*body(), which use
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 it to determine where the Content-Type header goes.
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
81
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 """
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
83
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 def __init__(self, fp):
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 self._fp = fp
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 self._headers = []
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
87
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 def addheader(self, key, value, prefix=0):
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 lines = string.splitfields(value, "\n")
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 while lines and not lines[-1]: del lines[-1]
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 while lines and not lines[0]: del lines[0]
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 for i in range(1, len(lines)):
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 lines[i] = " " + string.strip(lines[i])
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 value = string.joinfields(lines, "\n") + "\n"
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 line = key + ": " + value
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 if prefix:
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 self._headers.insert(0, line)
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 else:
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 self._headers.append(line)
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
100
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 def flushheaders(self):
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 self._fp.writelines(self._headers)
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 self._headers = []
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
104
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 def startbody(self, ctype, plist=[], prefix=1):
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 for name, value in plist:
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 ctype = ctype + ';\n %s=\"%s\"' % (name, value)
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 self.addheader("Content-Type", ctype, prefix=prefix)
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 self.flushheaders()
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 self._fp.write("\n")
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 return self._fp
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
112
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 def startmultipartbody(self, subtype, boundary=None, plist=[], prefix=1):
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 self._boundary = boundary or mimetools.choose_boundary()
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 return self.startbody("multipart/" + subtype,
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 [("boundary", self._boundary)] + plist,
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 prefix=prefix)
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
118
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 def nextpart(self):
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 self._fp.write("\n--" + self._boundary + "\n")
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 return self.__class__(self._fp)
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
122
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 def lastpart(self):
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 self._fp.write("\n--" + self._boundary + "--\n")
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
125
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
126
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 if __name__ == '__main__':
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 print "To test the MimeWriter module, run TestMimeWriter.py."