annotate Lib/MimeWriter.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents 5c29411a05a5
children
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
23565
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
3 This module defines the class MimeWriter. The MimeWriter class implements
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
4 a basic formatter for creating MIME multi-part files. It doesn't seek around
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
5 the output file nor does it use large amounts of buffer space. You must write
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
6 the parts out in the order that they should occur in the final file.
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
7 MimeWriter does buffer the headers you add, allowing you to rearrange their
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
8 order.
4149
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 """
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
11
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 import mimetools
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
14
16070
b5345e99596b added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents: 15868
diff changeset
15 __all__ = ["MimeWriter"]
4149
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
16
41915
5c29411a05a5 Have MimeWriter raise a DeprecationWarning as per PEP 4 and its documentation.
Brett Cannon <bcannon@gmail.com>
parents: 23565
diff changeset
17 import warnings
5c29411a05a5 Have MimeWriter raise a DeprecationWarning as per PEP 4 and its documentation.
Brett Cannon <bcannon@gmail.com>
parents: 23565
diff changeset
18
5c29411a05a5 Have MimeWriter raise a DeprecationWarning as per PEP 4 and its documentation.
Brett Cannon <bcannon@gmail.com>
parents: 23565
diff changeset
19 warnings.warn("the MimeWriter module is deprecated; use the email package instead",
5c29411a05a5 Have MimeWriter raise a DeprecationWarning as per PEP 4 and its documentation.
Brett Cannon <bcannon@gmail.com>
parents: 23565
diff changeset
20 DeprecationWarning, 2)
5c29411a05a5 Have MimeWriter raise a DeprecationWarning as per PEP 4 and its documentation.
Brett Cannon <bcannon@gmail.com>
parents: 23565
diff changeset
21
4149
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 class MimeWriter:
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
23
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 """Generic MIME writer.
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
25
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 Methods:
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 __init__()
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 addheader()
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 flushheaders()
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 startbody()
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 startmultipartbody()
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 nextpart()
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 lastpart()
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 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
37 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
38 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
39 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
40 headers you add, allowing you to rearrange their order.
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 8119
diff changeset
41
4149
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 General usage is:
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
43
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 f = <open the output file>
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 w = MimeWriter(f)
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 ...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
47
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 followed by either:
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
49
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 f = w.startbody(content_type)
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 ...call f.write(data) for body data...
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 or:
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
54
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 w.startmultipartbody(subtype)
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 for each part:
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 subwriter = w.nextpart()
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
58 ...use the subwriter's methods to create the subpart...
4149
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 w.lastpart()
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
60
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 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
62 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
63 writing recursive body parts is easy.
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
64
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 Warning: don't forget to call lastpart()!
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 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
68 are detected.
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
69
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 Some special cases:
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
71
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 - 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
73 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
74
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 - startmultipartbody() actually returns a file as well;
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 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
77 mailer is not MIME-aware' message.
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
78
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 - 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
80 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
81 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
82 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
83
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 - 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
85 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
86 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
87 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
88 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
89
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 """
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
91
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 def __init__(self, fp):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
93 self._fp = fp
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
94 self._headers = []
4149
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
95
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 def addheader(self, key, value, prefix=0):
23565
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
97 """Add a header line to the MIME message.
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
98
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
99 The key is the name of the header, where the value obviously provides
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
100 the value of the header. The optional argument prefix determines
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
101 where the header is inserted; 0 means append at the end, 1 means
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
102 insert at the start. The default is to append.
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
103
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
104 """
16499
58de2829cfdd String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16070
diff changeset
105 lines = value.split("\n")
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
106 while lines and not lines[-1]: del lines[-1]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
107 while lines and not lines[0]: del lines[0]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
108 for i in range(1, len(lines)):
16499
58de2829cfdd String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16070
diff changeset
109 lines[i] = " " + lines[i].strip()
58de2829cfdd String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16070
diff changeset
110 value = "\n".join(lines) + "\n"
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
111 line = key + ": " + value
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
112 if prefix:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
113 self._headers.insert(0, line)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
114 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
115 self._headers.append(line)
4149
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
116
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 def flushheaders(self):
23565
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
118 """Writes out and forgets all headers accumulated so far.
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
119
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
120 This is useful if you don't need a body part at all; for example,
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
121 for a subpart of type message/rfc822 that's (mis)used to store some
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
122 header-like information.
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
123
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
124 """
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
125 self._fp.writelines(self._headers)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
126 self._headers = []
4149
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
127
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 def startbody(self, ctype, plist=[], prefix=1):
23565
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
129 """Returns a file-like object for writing the body of the message.
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
130
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
131 The content-type is set to the provided ctype, and the optional
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
132 parameter, plist, provides additional parameters for the
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
133 content-type declaration. The optional argument prefix determines
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
134 where the header is inserted; 0 means append at the end, 1 means
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
135 insert at the start. The default is to insert at the start.
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
136
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
137 """
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
138 for name, value in plist:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
139 ctype = ctype + ';\n %s=\"%s\"' % (name, value)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
140 self.addheader("Content-Type", ctype, prefix=prefix)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
141 self.flushheaders()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
142 self._fp.write("\n")
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
143 return self._fp
4149
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
144
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 def startmultipartbody(self, subtype, boundary=None, plist=[], prefix=1):
23565
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
146 """Returns a file-like object for writing the body of the message.
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
147
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
148 Additionally, this method initializes the multi-part code, where the
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
149 subtype parameter provides the multipart subtype, the boundary
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
150 parameter may provide a user-defined boundary specification, and the
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
151 plist parameter provides optional parameters for the subtype. The
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
152 optional argument, prefix, determines where the header is inserted;
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
153 0 means append at the end, 1 means insert at the start. The default
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
154 is to insert at the start. Subparts should be created using the
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
155 nextpart() method.
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
156
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
157 """
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
158 self._boundary = boundary or mimetools.choose_boundary()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
159 return self.startbody("multipart/" + subtype,
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
160 [("boundary", self._boundary)] + plist,
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
161 prefix=prefix)
4149
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
162
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 def nextpart(self):
23565
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
164 """Returns a new instance of MimeWriter which represents an
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
165 individual part in a multipart message.
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
166
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
167 This may be used to write the part as well as used for creating
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
168 recursively complex multipart messages. The message must first be
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
169 initialized with the startmultipartbody() method before using the
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
170 nextpart() method.
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
171
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
172 """
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
173 self._fp.write("\n--" + self._boundary + "\n")
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
174 return self.__class__(self._fp)
4149
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
175
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 def lastpart(self):
23565
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
177 """This is used to designate the last part of a multipart message.
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
178
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
179 It should always be used when writing multipart messages.
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
180
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 16499
diff changeset
181 """
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
182 self._fp.write("\n--" + self._boundary + "--\n")
4149
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
183
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
184
4298ff222d76 Added Mime writer module (formerly in Grail)
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 if __name__ == '__main__':
8119
5aecc277e6ca Run the self-test (test.test_MimeWriter) when invoked as a script.
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
186 import test.test_MimeWriter