annotate Lib/email/Message.py @ 25135:0f0016632be4 legacy-trunk

_formatparam(), set_param(): RFC 2231 encoding support by Oleg Broytmann in SF patch #600096. Specifically, the former function now encodes the triplets, while the latter adds optional charset and language arguments.
author Barry Warsaw <barry@python.org>
date Fri, 06 Sep 2002 03:55:04 +0000
parents b89ebd57ceb4
children 2d13f9d98135
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
1 # Copyright (C) 2001,2002 Python Software Foundation
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
2 # Author: barry@zope.com (Barry Warsaw)
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
3
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
4 """Basic message object for the email package object model.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
5 """
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
6
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
7 import re
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
8 import warnings
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
9 from cStringIO import StringIO
24052
cb16995aa7e8 Oleg Broytmann's support for RFC 2231 encoded parameters, SF patch #549133
Barry Warsaw <barry@python.org>
parents: 23638
diff changeset
10 from types import ListType, TupleType, StringType
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
11
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
12 # Intrapackage imports
23638
8c215ddfd3d1 Use absolute import paths for intrapackage imports.
Barry Warsaw <barry@python.org>
parents: 23521
diff changeset
13 from email import Errors
8c215ddfd3d1 Use absolute import paths for intrapackage imports.
Barry Warsaw <barry@python.org>
parents: 23521
diff changeset
14 from email import Utils
8c215ddfd3d1 Use absolute import paths for intrapackage imports.
Barry Warsaw <barry@python.org>
parents: 23521
diff changeset
15 from email import Charset
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
16
20104
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
17 SEMISPACE = '; '
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
18
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
19 # Regular expression used to split header parameters. BAW: this may be too
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
20 # simple. It isn't strictly RFC 2045 (section 5.1) compliant, but it catches
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
21 # most headers found in the wild. We may eventually need a full fledged
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
22 # parser eventually.
20675
7418e0d5fc01 A fix for SF bug #472560, extra newlines returned by get_param() when
Barry Warsaw <barry@python.org>
parents: 20333
diff changeset
23 paramre = re.compile(r'\s*;\s*')
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
24 # Regular expression that matches `special' characters in parameters, the
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
25 # existance of which force quoting of the parameter value.
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
26 tspecials = re.compile(r'[ \(\)<>@,;:\\"/\[\]\?=]')
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
27
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
28
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
29
24052
cb16995aa7e8 Oleg Broytmann's support for RFC 2231 encoded parameters, SF patch #549133
Barry Warsaw <barry@python.org>
parents: 23638
diff changeset
30 # Helper functions
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
31 def _formatparam(param, value=None, quote=1):
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
32 """Convenience function to format and return a key=value pair.
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
33
24052
cb16995aa7e8 Oleg Broytmann's support for RFC 2231 encoded parameters, SF patch #549133
Barry Warsaw <barry@python.org>
parents: 23638
diff changeset
34 This will quote the value if needed or if quote is true.
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
35 """
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
36 if value is not None and len(value) > 0:
24052
cb16995aa7e8 Oleg Broytmann's support for RFC 2231 encoded parameters, SF patch #549133
Barry Warsaw <barry@python.org>
parents: 23638
diff changeset
37 # TupleType is used for RFC 2231 encoded parameter values where items
cb16995aa7e8 Oleg Broytmann's support for RFC 2231 encoded parameters, SF patch #549133
Barry Warsaw <barry@python.org>
parents: 23638
diff changeset
38 # are (charset, language, value). charset is a string, not a Charset
cb16995aa7e8 Oleg Broytmann's support for RFC 2231 encoded parameters, SF patch #549133
Barry Warsaw <barry@python.org>
parents: 23638
diff changeset
39 # instance.
cb16995aa7e8 Oleg Broytmann's support for RFC 2231 encoded parameters, SF patch #549133
Barry Warsaw <barry@python.org>
parents: 23638
diff changeset
40 if isinstance(value, TupleType):
25135
0f0016632be4 _formatparam(), set_param(): RFC 2231 encoding support by Oleg
Barry Warsaw <barry@python.org>
parents: 25132
diff changeset
41 # Encode as per RFC 2231
0f0016632be4 _formatparam(), set_param(): RFC 2231 encoding support by Oleg
Barry Warsaw <barry@python.org>
parents: 25132
diff changeset
42 param += '*'
0f0016632be4 _formatparam(), set_param(): RFC 2231 encoding support by Oleg
Barry Warsaw <barry@python.org>
parents: 25132
diff changeset
43 value = Utils.encode_rfc2231(value[2], value[0], value[1])
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
44 # BAW: Please check this. I think that if quote is set it should
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
45 # force quoting even if not necessary.
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
46 if quote or tspecials.search(value):
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
47 return '%s="%s"' % (param, Utils.quote(value))
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
48 else:
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
49 return '%s=%s' % (param, value)
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
50 else:
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
51 return param
20104
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
52
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
53
24052
cb16995aa7e8 Oleg Broytmann's support for RFC 2231 encoded parameters, SF patch #549133
Barry Warsaw <barry@python.org>
parents: 23638
diff changeset
54 def _unquotevalue(value):
cb16995aa7e8 Oleg Broytmann's support for RFC 2231 encoded parameters, SF patch #549133
Barry Warsaw <barry@python.org>
parents: 23638
diff changeset
55 if isinstance(value, TupleType):
25003
c744144c4049 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 24912
diff changeset
56 return (value[0], value[1], Utils.unquote(value[2]))
24052
cb16995aa7e8 Oleg Broytmann's support for RFC 2231 encoded parameters, SF patch #549133
Barry Warsaw <barry@python.org>
parents: 23638
diff changeset
57 else:
25003
c744144c4049 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 24912
diff changeset
58 return Utils.unquote(value)
24052
cb16995aa7e8 Oleg Broytmann's support for RFC 2231 encoded parameters, SF patch #549133
Barry Warsaw <barry@python.org>
parents: 23638
diff changeset
59
cb16995aa7e8 Oleg Broytmann's support for RFC 2231 encoded parameters, SF patch #549133
Barry Warsaw <barry@python.org>
parents: 23638
diff changeset
60
25049
Barry Warsaw <barry@python.org>
parents: 25003
diff changeset
61
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
62 class Message:
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
63 """Basic message object for use inside the object tree.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
64
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
65 A message object is defined as something that has a bunch of RFC 2822
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
66 headers and a payload. If the body of the message is a multipart, then
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
67 the payload is a list of Messages, otherwise it is a string.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
68
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
69 These objects implement part of the `mapping' interface, which assumes
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
70 there is exactly one occurrance of the header per message. Some headers
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
71 do in fact appear multiple times (e.g. Received:) and for those headers,
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
72 you must use the explicit API to set or get all the headers. Not all of
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
73 the mapping methods are implemented.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
74
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
75 """
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
76 def __init__(self):
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
77 self._headers = []
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
78 self._unixfrom = None
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
79 self._payload = None
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
80 self._charset = None
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
81 # Defaults for multipart messages
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
82 self.preamble = self.epilogue = None
24153
a59df2fa4ec8 Add the concept of a "default type". Normally the default type is
Barry Warsaw <barry@python.org>
parents: 24052
diff changeset
83 # Default content type
a59df2fa4ec8 Add the concept of a "default type". Normally the default type is
Barry Warsaw <barry@python.org>
parents: 24052
diff changeset
84 self._default_type = 'text/plain'
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
85
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
86 def __str__(self):
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
87 """Return the entire formatted message as a string.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
88 This includes the headers, body, and `unixfrom' line.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
89 """
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
90 return self.as_string(unixfrom=1)
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
91
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
92 def as_string(self, unixfrom=0):
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
93 """Return the entire formatted message as a string.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
94 Optional `unixfrom' when true, means include the Unix From_ envelope
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
95 header.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
96 """
23638
8c215ddfd3d1 Use absolute import paths for intrapackage imports.
Barry Warsaw <barry@python.org>
parents: 23521
diff changeset
97 from email.Generator import Generator
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
98 fp = StringIO()
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
99 g = Generator(fp)
23638
8c215ddfd3d1 Use absolute import paths for intrapackage imports.
Barry Warsaw <barry@python.org>
parents: 23521
diff changeset
100 g.flatten(self, unixfrom=unixfrom)
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
101 return fp.getvalue()
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
102
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
103 def is_multipart(self):
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
104 """Return true if the message consists of multiple parts."""
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
105 if type(self._payload) is ListType:
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
106 return 1
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
107 return 0
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
108
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
109 #
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
110 # Unix From_ line
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
111 #
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
112 def set_unixfrom(self, unixfrom):
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
113 self._unixfrom = unixfrom
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
114
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
115 def get_unixfrom(self):
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
116 return self._unixfrom
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
117
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
118 #
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
119 # Payload manipulation.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
120 #
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
121 def add_payload(self, payload):
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
122 """Add the given payload to the current payload.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
123
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
124 If the current payload is empty, then the current payload will be made
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
125 a scalar, set to the given value.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
126 """
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
127 warnings.warn('add_payload() is deprecated, use attach() instead.',
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
128 DeprecationWarning, 2)
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
129 if self._payload is None:
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
130 self._payload = payload
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
131 elif type(self._payload) is ListType:
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
132 self._payload.append(payload)
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
133 elif self.get_main_type() not in (None, 'multipart'):
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
134 raise Errors.MultipartConversionError(
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
135 'Message main Content-Type: must be "multipart" or missing')
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
136 else:
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
137 self._payload = [self._payload, payload]
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
138
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
139 def attach(self, payload):
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
140 """Add the given payload to the current payload.
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
141
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
142 The current payload will always be a list of objects after this method
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
143 is called. If you want to set the payload to a scalar object
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
144 (e.g. because you're attaching a message/rfc822 subpart), use
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
145 set_payload() instead.
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
146 """
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
147 if self._payload is None:
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
148 self._payload = [payload]
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
149 else:
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
150 self._payload.append(payload)
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
151
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
152 def get_payload(self, i=None, decode=0):
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
153 """Return the current payload exactly as is.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
154
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
155 Optional i returns that index into the payload.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
156
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
157 Optional decode is a flag indicating whether the payload should be
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
158 decoded or not, according to the Content-Transfer-Encoding: header.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
159 When true and the message is not a multipart, the payload will be
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
160 decoded if this header's value is `quoted-printable' or `base64'. If
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
161 some other encoding is used, or the header is missing, the payload is
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
162 returned as-is (undecoded). If the message is a multipart and the
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
163 decode flag is true, then None is returned.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
164 """
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
165 if i is None:
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
166 payload = self._payload
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
167 elif type(self._payload) is not ListType:
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
168 raise TypeError, i
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
169 else:
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
170 payload = self._payload[i]
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
171 if decode:
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
172 if self.is_multipart():
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
173 return None
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
174 cte = self.get('content-transfer-encoding', '')
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
175 if cte.lower() == 'quoted-printable':
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
176 return Utils._qdecode(payload)
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
177 elif cte.lower() == 'base64':
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
178 return Utils._bdecode(payload)
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
179 # Everything else, including encodings with 8bit or 7bit are returned
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
180 # unchanged.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
181 return payload
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
182
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
183
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
184 def set_payload(self, payload, charset=None):
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
185 """Set the payload to the given value.
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
186
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
187 Optionally set the charset, which must be a Charset instance."""
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
188 self._payload = payload
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
189 if charset is not None:
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
190 self.set_charset(charset)
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
191
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
192 def set_charset(self, charset):
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
193 """Set the charset of the payload to a given character set.
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
194
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
195 charset can be a string or a Charset object. If it is a string, it
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
196 will be converted to a Charset object by calling Charset's
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
197 constructor. If charset is None, the charset parameter will be
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
198 removed from the Content-Type: field. Anything else will generate a
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
199 TypeError.
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
200
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
201 The message will be assumed to be a text message encoded with
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
202 charset.input_charset. It will be converted to charset.output_charset
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
203 and encoded properly, if needed, when generating the plain text
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
204 representation of the message. MIME headers (MIME-Version,
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
205 Content-Type, Content-Transfer-Encoding) will be added as needed.
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
206 """
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
207 if charset is None:
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
208 self.del_param('charset')
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
209 self._charset = None
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
210 return
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
211 if isinstance(charset, StringType):
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
212 charset = Charset.Charset(charset)
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
213 if not isinstance(charset, Charset.Charset):
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
214 raise TypeError, charset
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
215 # BAW: should we accept strings that can serve as arguments to the
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
216 # Charset constructor?
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
217 self._charset = charset
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
218 if not self.has_key('MIME-Version'):
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
219 self.add_header('MIME-Version', '1.0')
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
220 if not self.has_key('Content-Type'):
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
221 self.add_header('Content-Type', 'text/plain',
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
222 charset=charset.get_output_charset())
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
223 else:
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
224 self.set_param('charset', charset.get_output_charset())
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
225 if not self.has_key('Content-Transfer-Encoding'):
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
226 cte = charset.get_body_encoding()
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
227 if callable(cte):
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
228 cte(self)
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
229 else:
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
230 self.add_header('Content-Transfer-Encoding', cte)
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
231
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
232 def get_charset(self):
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
233 """Return the Charset object associated with the message's payload."""
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
234 return self._charset
23521
a90faa5f9417 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 23473
diff changeset
235
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
236 #
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
237 # MAPPING INTERFACE (partial)
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
238 #
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
239 def __len__(self):
20104
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
240 """Return the total number of headers, including duplicates."""
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
241 return len(self._headers)
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
242
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
243 def __getitem__(self, name):
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
244 """Get a header value.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
245
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
246 Return None if the header is missing instead of raising an exception.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
247
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
248 Note that if the header appeared multiple times, exactly which
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
249 occurrance gets returned is undefined. Use getall() to get all
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
250 the values matching a header field name.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
251 """
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
252 return self.get(name)
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
253
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
254 def __setitem__(self, name, val):
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
255 """Set the value of a header.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
256
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
257 Note: this does not overwrite an existing header with the same field
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
258 name. Use __delitem__() first to delete any existing headers.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
259 """
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
260 self._headers.append((name, val))
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
261
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
262 def __delitem__(self, name):
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
263 """Delete all occurrences of a header, if present.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
264
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
265 Does not raise an exception if the header is missing.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
266 """
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
267 name = name.lower()
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
268 newheaders = []
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
269 for k, v in self._headers:
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
270 if k.lower() <> name:
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
271 newheaders.append((k, v))
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
272 self._headers = newheaders
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
273
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
274 def __contains__(self, key):
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
275 return key.lower() in [k.lower() for k, v in self._headers]
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
276
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
277 def has_key(self, name):
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
278 """Return true if the message contains the header."""
20104
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
279 missing = []
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
280 return self.get(name, missing) is not missing
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
281
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
282 def keys(self):
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
283 """Return a list of all the message's header field names.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
284
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
285 These will be sorted in the order they appeared in the original
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
286 message, and may contain duplicates. Any fields deleted and
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
287 re-inserted are always appended to the header list.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
288 """
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
289 return [k for k, v in self._headers]
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
290
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
291 def values(self):
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
292 """Return a list of all the message's header values.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
293
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
294 These will be sorted in the order they appeared in the original
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
295 message, and may contain duplicates. Any fields deleted and
21043
70b4306f488d More typo fixes.
Barry Warsaw <barry@python.org>
parents: 21042
diff changeset
296 re-inserted are always appended to the header list.
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
297 """
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
298 return [v for k, v in self._headers]
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
299
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
300 def items(self):
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
301 """Get all the message's header fields and values.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
302
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
303 These will be sorted in the order they appeared in the original
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
304 message, and may contain duplicates. Any fields deleted and
21043
70b4306f488d More typo fixes.
Barry Warsaw <barry@python.org>
parents: 21042
diff changeset
305 re-inserted are always appended to the header list.
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
306 """
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
307 return self._headers[:]
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
308
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
309 def get(self, name, failobj=None):
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
310 """Get a header value.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
311
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
312 Like __getitem__() but return failobj instead of None when the field
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
313 is missing.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
314 """
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
315 name = name.lower()
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
316 for k, v in self._headers:
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
317 if k.lower() == name:
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
318 return v
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
319 return failobj
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
320
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
321 #
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
322 # Additional useful stuff
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
323 #
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
324
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
325 def get_all(self, name, failobj=None):
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
326 """Return a list of all the values for the named field.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
327
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
328 These will be sorted in the order they appeared in the original
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
329 message, and may contain duplicates. Any fields deleted and
21042
0846b4e2517d Docstring typo fix.
Greg Ward <gward@python.net>
parents: 20886
diff changeset
330 re-inserted are always appended to the header list.
20333
86896b5f1ebc get_all(): We never returned failobj if we found no matching headers.
Barry Warsaw <barry@python.org>
parents: 20253
diff changeset
331
86896b5f1ebc get_all(): We never returned failobj if we found no matching headers.
Barry Warsaw <barry@python.org>
parents: 20253
diff changeset
332 If no such fields exist, failobj is returned (defaults to None).
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
333 """
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
334 values = []
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
335 name = name.lower()
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
336 for k, v in self._headers:
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
337 if k.lower() == name:
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
338 values.append(v)
20333
86896b5f1ebc get_all(): We never returned failobj if we found no matching headers.
Barry Warsaw <barry@python.org>
parents: 20253
diff changeset
339 if not values:
86896b5f1ebc get_all(): We never returned failobj if we found no matching headers.
Barry Warsaw <barry@python.org>
parents: 20253
diff changeset
340 return failobj
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
341 return values
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
342
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
343 def add_header(self, _name, _value, **_params):
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
344 """Extended header setting.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
345
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
346 name is the header field to add. keyword arguments can be used to set
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
347 additional parameters for the header field, with underscores converted
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
348 to dashes. Normally the parameter will be added as key="value" unless
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
349 value is None, in which case only the key will be added.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
350
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
351 Example:
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
352
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
353 msg.add_header('content-disposition', 'attachment', filename='bud.gif')
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
354 """
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
355 parts = []
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
356 for k, v in _params.items():
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
357 if v is None:
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
358 parts.append(k.replace('_', '-'))
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
359 else:
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
360 parts.append(_formatparam(k.replace('_', '-'), v))
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
361 if _value is not None:
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
362 parts.insert(0, _value)
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
363 self._headers.append((_name, SEMISPACE.join(parts)))
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
364
25132
b89ebd57ceb4 replace_header(): New method given by Skip Montanaro in SF patch
Barry Warsaw <barry@python.org>
parents: 25049
diff changeset
365 def replace_header(self, _name, _value):
b89ebd57ceb4 replace_header(): New method given by Skip Montanaro in SF patch
Barry Warsaw <barry@python.org>
parents: 25049
diff changeset
366 """Replace a header.
b89ebd57ceb4 replace_header(): New method given by Skip Montanaro in SF patch
Barry Warsaw <barry@python.org>
parents: 25049
diff changeset
367
b89ebd57ceb4 replace_header(): New method given by Skip Montanaro in SF patch
Barry Warsaw <barry@python.org>
parents: 25049
diff changeset
368 Replace the first matching header found in the message, retaining
b89ebd57ceb4 replace_header(): New method given by Skip Montanaro in SF patch
Barry Warsaw <barry@python.org>
parents: 25049
diff changeset
369 header order and case. If no matching header was found, a KeyError is
b89ebd57ceb4 replace_header(): New method given by Skip Montanaro in SF patch
Barry Warsaw <barry@python.org>
parents: 25049
diff changeset
370 raised.
b89ebd57ceb4 replace_header(): New method given by Skip Montanaro in SF patch
Barry Warsaw <barry@python.org>
parents: 25049
diff changeset
371 """
b89ebd57ceb4 replace_header(): New method given by Skip Montanaro in SF patch
Barry Warsaw <barry@python.org>
parents: 25049
diff changeset
372 _name = _name.lower()
b89ebd57ceb4 replace_header(): New method given by Skip Montanaro in SF patch
Barry Warsaw <barry@python.org>
parents: 25049
diff changeset
373 for i, (k, v) in zip(range(len(self._headers)), self._headers):
b89ebd57ceb4 replace_header(): New method given by Skip Montanaro in SF patch
Barry Warsaw <barry@python.org>
parents: 25049
diff changeset
374 if k.lower() == _name:
b89ebd57ceb4 replace_header(): New method given by Skip Montanaro in SF patch
Barry Warsaw <barry@python.org>
parents: 25049
diff changeset
375 self._headers[i] = (k, _value)
b89ebd57ceb4 replace_header(): New method given by Skip Montanaro in SF patch
Barry Warsaw <barry@python.org>
parents: 25049
diff changeset
376 break
b89ebd57ceb4 replace_header(): New method given by Skip Montanaro in SF patch
Barry Warsaw <barry@python.org>
parents: 25049
diff changeset
377 else:
b89ebd57ceb4 replace_header(): New method given by Skip Montanaro in SF patch
Barry Warsaw <barry@python.org>
parents: 25049
diff changeset
378 raise KeyError, _name
b89ebd57ceb4 replace_header(): New method given by Skip Montanaro in SF patch
Barry Warsaw <barry@python.org>
parents: 25049
diff changeset
379
24332
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
380 #
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
381 # These methods are silently deprecated in favor of get_content_type() and
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
382 # friends (see below). They will be noisily deprecated in email 3.0.
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
383 #
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
384
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
385 def get_type(self, failobj=None):
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
386 """Returns the message's content type.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
387
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
388 The returned string is coerced to lowercase and returned as a single
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
389 string of the form `maintype/subtype'. If there was no Content-Type:
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
390 header in the message, failobj is returned (defaults to None).
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
391 """
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
392 missing = []
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
393 value = self.get('content-type', missing)
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
394 if value is missing:
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
395 return failobj
24308
69134c61f87c Anthony Baxter's cleanup patch. Python project SF patch # 583190,
Barry Warsaw <barry@python.org>
parents: 24153
diff changeset
396 return paramre.split(value)[0].lower().strip()
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
397
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
398 def get_main_type(self, failobj=None):
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
399 """Return the message's main content type if present."""
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
400 missing = []
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
401 ctype = self.get_type(missing)
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
402 if ctype is missing:
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
403 return failobj
24332
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
404 if ctype.count('/') <> 1:
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
405 return failobj
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
406 return ctype.split('/')[0]
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
407
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
408 def get_subtype(self, failobj=None):
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
409 """Return the message's content subtype if present."""
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
410 missing = []
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
411 ctype = self.get_type(missing)
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
412 if ctype is missing:
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
413 return failobj
24332
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
414 if ctype.count('/') <> 1:
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
415 return failobj
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
416 return ctype.split('/')[1]
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
417
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
418 #
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
419 # Use these three methods instead of the three above.
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
420 #
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
421
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
422 def get_content_type(self):
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
423 """Returns the message's content type.
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
424
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
425 The returned string is coerced to lowercase and returned as a ingle
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
426 string of the form `maintype/subtype'. If there was no Content-Type:
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
427 header in the message, the default type as give by get_default_type()
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
428 will be returned. Since messages always have a default type this will
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
429 always return a value.
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
430
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
431 The current state of RFC standards define a message's default type to
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
432 be text/plain unless it appears inside a multipart/digest container,
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
433 in which case it would be message/rfc822.
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
434 """
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
435 missing = []
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
436 value = self.get('content-type', missing)
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
437 if value is missing:
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
438 # This should have no parameters
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
439 return self.get_default_type()
24912
0ee48aefa474 get_content_type(), get_content_maintype(), get_content_subtype(): RFC
Barry Warsaw <barry@python.org>
parents: 24332
diff changeset
440 ctype = paramre.split(value)[0].lower().strip()
0ee48aefa474 get_content_type(), get_content_maintype(), get_content_subtype(): RFC
Barry Warsaw <barry@python.org>
parents: 24332
diff changeset
441 # RFC 2045, section 5.2 says if its invalid, use text/plain
0ee48aefa474 get_content_type(), get_content_maintype(), get_content_subtype(): RFC
Barry Warsaw <barry@python.org>
parents: 24332
diff changeset
442 if ctype.count('/') <> 1:
0ee48aefa474 get_content_type(), get_content_maintype(), get_content_subtype(): RFC
Barry Warsaw <barry@python.org>
parents: 24332
diff changeset
443 return 'text/plain'
0ee48aefa474 get_content_type(), get_content_maintype(), get_content_subtype(): RFC
Barry Warsaw <barry@python.org>
parents: 24332
diff changeset
444 return ctype
24332
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
445
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
446 def get_content_maintype(self):
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
447 """Returns the message's main content type.
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
448
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
449 This is the `maintype' part of the string returned by
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
450 get_content_type(). If no slash is found in the full content type, a
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
451 ValueError is raised.
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
452 """
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
453 ctype = self.get_content_type()
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
454 return ctype.split('/')[0]
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
455
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
456 def get_content_subtype(self):
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
457 """Returns the message's sub content type.
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
458
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
459 This is the `subtype' part of the string returned by
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
460 get_content_type(). If no slash is found in the full content type, a
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
461 ValueError is raised.
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
462 """
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
463 ctype = self.get_content_type()
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
464 return ctype.split('/')[1]
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
465
24153
a59df2fa4ec8 Add the concept of a "default type". Normally the default type is
Barry Warsaw <barry@python.org>
parents: 24052
diff changeset
466 def get_default_type(self):
a59df2fa4ec8 Add the concept of a "default type". Normally the default type is
Barry Warsaw <barry@python.org>
parents: 24052
diff changeset
467 """Return the `default' content type.
a59df2fa4ec8 Add the concept of a "default type". Normally the default type is
Barry Warsaw <barry@python.org>
parents: 24052
diff changeset
468
a59df2fa4ec8 Add the concept of a "default type". Normally the default type is
Barry Warsaw <barry@python.org>
parents: 24052
diff changeset
469 Most messages have a default content type of text/plain, except for
a59df2fa4ec8 Add the concept of a "default type". Normally the default type is
Barry Warsaw <barry@python.org>
parents: 24052
diff changeset
470 messages that are subparts of multipart/digest containers. Such
a59df2fa4ec8 Add the concept of a "default type". Normally the default type is
Barry Warsaw <barry@python.org>
parents: 24052
diff changeset
471 subparts then have a default content type of message/rfc822.
a59df2fa4ec8 Add the concept of a "default type". Normally the default type is
Barry Warsaw <barry@python.org>
parents: 24052
diff changeset
472 """
a59df2fa4ec8 Add the concept of a "default type". Normally the default type is
Barry Warsaw <barry@python.org>
parents: 24052
diff changeset
473 return self._default_type
a59df2fa4ec8 Add the concept of a "default type". Normally the default type is
Barry Warsaw <barry@python.org>
parents: 24052
diff changeset
474
a59df2fa4ec8 Add the concept of a "default type". Normally the default type is
Barry Warsaw <barry@python.org>
parents: 24052
diff changeset
475 def set_default_type(self, ctype):
a59df2fa4ec8 Add the concept of a "default type". Normally the default type is
Barry Warsaw <barry@python.org>
parents: 24052
diff changeset
476 """Set the `default' content type.
a59df2fa4ec8 Add the concept of a "default type". Normally the default type is
Barry Warsaw <barry@python.org>
parents: 24052
diff changeset
477
24332
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
478 ctype should be either "text/plain" or "message/rfc822", although this
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
479 is not enforced. The default content type is not stored in the
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
480 Content-Type: header.
24153
a59df2fa4ec8 Add the concept of a "default type". Normally the default type is
Barry Warsaw <barry@python.org>
parents: 24052
diff changeset
481 """
a59df2fa4ec8 Add the concept of a "default type". Normally the default type is
Barry Warsaw <barry@python.org>
parents: 24052
diff changeset
482 self._default_type = ctype
a59df2fa4ec8 Add the concept of a "default type". Normally the default type is
Barry Warsaw <barry@python.org>
parents: 24052
diff changeset
483
20104
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
484 def _get_params_preserve(self, failobj, header):
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
485 # Like get_params() but preserves the quoting of values. BAW:
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
486 # should this be part of the public interface?
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
487 missing = []
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
488 value = self.get(header, missing)
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
489 if value is missing:
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
490 return failobj
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
491 params = []
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
492 for p in paramre.split(value):
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
493 try:
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
494 name, val = p.split('=', 1)
24308
69134c61f87c Anthony Baxter's cleanup patch. Python project SF patch # 583190,
Barry Warsaw <barry@python.org>
parents: 24153
diff changeset
495 name = name.strip()
69134c61f87c Anthony Baxter's cleanup patch. Python project SF patch # 583190,
Barry Warsaw <barry@python.org>
parents: 24153
diff changeset
496 val = val.strip()
20104
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
497 except ValueError:
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
498 # Must have been a bare attribute
24308
69134c61f87c Anthony Baxter's cleanup patch. Python project SF patch # 583190,
Barry Warsaw <barry@python.org>
parents: 24153
diff changeset
499 name = p.strip()
20104
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
500 val = ''
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
501 params.append((name, val))
24052
cb16995aa7e8 Oleg Broytmann's support for RFC 2231 encoded parameters, SF patch #549133
Barry Warsaw <barry@python.org>
parents: 23638
diff changeset
502 params = Utils.decode_params(params)
20104
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
503 return params
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
504
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
505 def get_params(self, failobj=None, header='content-type', unquote=1):
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
506 """Return the message's Content-Type: parameters, as a list.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
507
20104
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
508 The elements of the returned list are 2-tuples of key/value pairs, as
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
509 split on the `=' sign. The left hand side of the `=' is the key,
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
510 while the right hand side is the value. If there is no `=' sign in
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
511 the parameter the value is the empty string. The value is always
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
512 unquoted, unless unquote is set to a false value.
20104
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
513
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
514 Optional failobj is the object to return if there is no Content-Type:
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
515 header. Optional header is the header to search instead of
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
516 Content-Type:.
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
517 """
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
518 missing = []
20104
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
519 params = self._get_params_preserve(missing, header)
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
520 if params is missing:
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
521 return failobj
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
522 if unquote:
24052
cb16995aa7e8 Oleg Broytmann's support for RFC 2231 encoded parameters, SF patch #549133
Barry Warsaw <barry@python.org>
parents: 23638
diff changeset
523 return [(k, _unquotevalue(v)) for k, v in params]
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
524 else:
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
525 return params
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
526
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
527 def get_param(self, param, failobj=None, header='content-type', unquote=1):
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
528 """Return the parameter value if found in the Content-Type: header.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
529
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
530 Optional failobj is the object to return if there is no Content-Type:
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
531 header. Optional header is the header to search instead of
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
532 Content-Type:
20104
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
533
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
534 Parameter keys are always compared case insensitively. Values are
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
535 always unquoted, unless unquote is set to a false value.
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
536 """
20104
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
537 if not self.has_key(header):
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
538 return failobj
20104
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
539 for k, v in self._get_params_preserve(failobj, header):
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
540 if k.lower() == param.lower():
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
541 if unquote:
24052
cb16995aa7e8 Oleg Broytmann's support for RFC 2231 encoded parameters, SF patch #549133
Barry Warsaw <barry@python.org>
parents: 23638
diff changeset
542 return _unquotevalue(v)
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
543 else:
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
544 return v
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
545 return failobj
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
546
25135
0f0016632be4 _formatparam(), set_param(): RFC 2231 encoding support by Oleg
Barry Warsaw <barry@python.org>
parents: 25132
diff changeset
547 def set_param(self, param, value, header='Content-Type', requote=1,
0f0016632be4 _formatparam(), set_param(): RFC 2231 encoding support by Oleg
Barry Warsaw <barry@python.org>
parents: 25132
diff changeset
548 charset=None, language=''):
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
549 """Set a parameter in the Content-Type: header.
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
550
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
551 If the parameter already exists in the header, its value will be
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
552 replaced with the new value.
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
553
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
554 If header is Content-Type: and has not yet been defined in this
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
555 message, it will be set to "text/plain" and the new parameter and
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
556 value will be appended, as per RFC 2045.
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
557
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
558 An alternate header can specified in the header argument, and
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
559 all parameters will be quoted as appropriate unless requote is
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
560 set to a false value.
25135
0f0016632be4 _formatparam(), set_param(): RFC 2231 encoding support by Oleg
Barry Warsaw <barry@python.org>
parents: 25132
diff changeset
561
0f0016632be4 _formatparam(), set_param(): RFC 2231 encoding support by Oleg
Barry Warsaw <barry@python.org>
parents: 25132
diff changeset
562 If charset is specified the parameter will be encoded according to RFC
0f0016632be4 _formatparam(), set_param(): RFC 2231 encoding support by Oleg
Barry Warsaw <barry@python.org>
parents: 25132
diff changeset
563 2231. In this case language is optional.
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
564 """
25135
0f0016632be4 _formatparam(), set_param(): RFC 2231 encoding support by Oleg
Barry Warsaw <barry@python.org>
parents: 25132
diff changeset
565 if not isinstance(value, TupleType) and charset:
0f0016632be4 _formatparam(), set_param(): RFC 2231 encoding support by Oleg
Barry Warsaw <barry@python.org>
parents: 25132
diff changeset
566 value = (charset, language, value)
0f0016632be4 _formatparam(), set_param(): RFC 2231 encoding support by Oleg
Barry Warsaw <barry@python.org>
parents: 25132
diff changeset
567
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
568 if not self.has_key(header) and header.lower() == 'content-type':
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
569 ctype = 'text/plain'
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
570 else:
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
571 ctype = self.get(header)
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
572 if not self.get_param(param, header=header):
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
573 if not ctype:
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
574 ctype = _formatparam(param, value, requote)
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
575 else:
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
576 ctype = SEMISPACE.join(
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
577 [ctype, _formatparam(param, value, requote)])
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
578 else:
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
579 ctype = ''
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
580 for old_param, old_value in self.get_params(header=header,
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
581 unquote=requote):
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
582 append_param = ''
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
583 if old_param.lower() == param.lower():
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
584 append_param = _formatparam(param, value, requote)
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
585 else:
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
586 append_param = _formatparam(old_param, old_value, requote)
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
587 if not ctype:
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
588 ctype = append_param
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
589 else:
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
590 ctype = SEMISPACE.join([ctype, append_param])
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
591 if ctype <> self.get(header):
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
592 del self[header]
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
593 self[header] = ctype
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
594
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
595 def del_param(self, param, header='content-type', requote=1):
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
596 """Remove the given parameter completely from the Content-Type header.
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
597
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
598 The header will be re-written in place without param or its value.
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
599 All values will be quoted as appropriate unless requote is set to a
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
600 false value.
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
601 """
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
602 if not self.has_key(header):
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
603 return
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
604 new_ctype = ''
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
605 for p, v in self.get_params(header, unquote=requote):
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
606 if p.lower() <> param.lower():
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
607 if not new_ctype:
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
608 new_ctype = _formatparam(p, v, requote)
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
609 else:
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
610 new_ctype = SEMISPACE.join([new_ctype,
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
611 _formatparam(p, v, requote)])
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
612 if new_ctype <> self.get(header):
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
613 del self[header]
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
614 self[header] = new_ctype
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
615
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
616 def set_type(self, type, header='Content-Type', requote=1):
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
617 """Set the main type and subtype for the Content-Type: header.
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
618
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
619 type must be a string in the form "maintype/subtype", otherwise a
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
620 ValueError is raised.
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
621
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
622 This method replaces the Content-Type: header, keeping all the
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
623 parameters in place. If requote is false, this leaves the existing
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
624 header's quoting as is. Otherwise, the parameters will be quoted (the
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
625 default).
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
626
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
627 An alternate header can be specified in the header argument. When the
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
628 Content-Type: header is set, we'll always also add a MIME-Version:
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
629 header.
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
630 """
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
631 # BAW: should we be strict?
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
632 if not type.count('/') == 1:
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
633 raise ValueError
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
634 # Set the Content-Type: you get a MIME-Version:
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
635 if header.lower() == 'content-type':
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
636 del self['mime-version']
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
637 self['MIME-Version'] = '1.0'
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
638 if not self.has_key(header):
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
639 self[header] = type
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
640 return
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
641 params = self.get_params(header, unquote=requote)
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
642 del self[header]
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
643 self[header] = type
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
644 # Skip the first param; it's the old type.
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
645 for p, v in params[1:]:
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
646 self.set_param(p, v, header, requote)
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
647
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
648 def get_filename(self, failobj=None):
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
649 """Return the filename associated with the payload if present.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
650
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
651 The filename is extracted from the Content-Disposition: header's
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
652 `filename' parameter, and it is unquoted.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
653 """
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
654 missing = []
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
655 filename = self.get_param('filename', missing, 'content-disposition')
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
656 if filename is missing:
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
657 return failobj
24052
cb16995aa7e8 Oleg Broytmann's support for RFC 2231 encoded parameters, SF patch #549133
Barry Warsaw <barry@python.org>
parents: 23638
diff changeset
658 if isinstance(filename, TupleType):
cb16995aa7e8 Oleg Broytmann's support for RFC 2231 encoded parameters, SF patch #549133
Barry Warsaw <barry@python.org>
parents: 23638
diff changeset
659 # It's an RFC 2231 encoded parameter
cb16995aa7e8 Oleg Broytmann's support for RFC 2231 encoded parameters, SF patch #549133
Barry Warsaw <barry@python.org>
parents: 23638
diff changeset
660 newvalue = _unquotevalue(filename)
cb16995aa7e8 Oleg Broytmann's support for RFC 2231 encoded parameters, SF patch #549133
Barry Warsaw <barry@python.org>
parents: 23638
diff changeset
661 return unicode(newvalue[2], newvalue[0])
cb16995aa7e8 Oleg Broytmann's support for RFC 2231 encoded parameters, SF patch #549133
Barry Warsaw <barry@python.org>
parents: 23638
diff changeset
662 else:
cb16995aa7e8 Oleg Broytmann's support for RFC 2231 encoded parameters, SF patch #549133
Barry Warsaw <barry@python.org>
parents: 23638
diff changeset
663 newvalue = _unquotevalue(filename.strip())
cb16995aa7e8 Oleg Broytmann's support for RFC 2231 encoded parameters, SF patch #549133
Barry Warsaw <barry@python.org>
parents: 23638
diff changeset
664 return newvalue
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
665
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
666 def get_boundary(self, failobj=None):
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
667 """Return the boundary associated with the payload if present.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
668
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
669 The boundary is extracted from the Content-Type: header's `boundary'
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
670 parameter, and it is unquoted.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
671 """
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
672 missing = []
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
673 boundary = self.get_param('boundary', missing)
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
674 if boundary is missing:
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
675 return failobj
24052
cb16995aa7e8 Oleg Broytmann's support for RFC 2231 encoded parameters, SF patch #549133
Barry Warsaw <barry@python.org>
parents: 23638
diff changeset
676 return _unquotevalue(boundary.strip())
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
677
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
678 def set_boundary(self, boundary):
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
679 """Set the boundary parameter in Content-Type: to 'boundary'.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
680
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
681 This is subtly different than deleting the Content-Type: header and
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
682 adding a new one with a new boundary parameter via add_header(). The
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
683 main difference is that using the set_boundary() method preserves the
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
684 order of the Content-Type: header in the original message.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
685
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
686 HeaderParseError is raised if the message has no Content-Type: header.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
687 """
20104
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
688 missing = []
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
689 params = self._get_params_preserve(missing, 'content-type')
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
690 if params is missing:
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
691 # There was no Content-Type: header, and we don't know what type
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
692 # to set it to, so raise an exception.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
693 raise Errors.HeaderParseError, 'No Content-Type: header found'
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
694 newparams = []
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
695 foundp = 0
20104
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
696 for pk, pv in params:
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
697 if pk.lower() == 'boundary':
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
698 newparams.append(('boundary', '"%s"' % boundary))
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
699 foundp = 1
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
700 else:
20104
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
701 newparams.append((pk, pv))
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
702 if not foundp:
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
703 # The original Content-Type: header had no boundary attribute.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
704 # Tack one one the end. BAW: should we raise an exception
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
705 # instead???
20104
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
706 newparams.append(('boundary', '"%s"' % boundary))
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
707 # Replace the existing Content-Type: header with the new value
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
708 newheaders = []
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
709 for h, v in self._headers:
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
710 if h.lower() == 'content-type':
20104
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
711 parts = []
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
712 for k, v in newparams:
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
713 if v == '':
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
714 parts.append(k)
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
715 else:
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
716 parts.append('%s=%s' % (k, v))
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
717 newheaders.append((h, SEMISPACE.join(parts)))
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
718
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
719 else:
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
720 newheaders.append((h, v))
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
721 self._headers = newheaders
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
722
23473
077d44b0405b Complete a merge of the mimelib project and the Python cvs codebases
Barry Warsaw <barry@python.org>
parents: 22939
diff changeset
723 try:
077d44b0405b Complete a merge of the mimelib project and the Python cvs codebases
Barry Warsaw <barry@python.org>
parents: 22939
diff changeset
724 from email._compat22 import walk
077d44b0405b Complete a merge of the mimelib project and the Python cvs codebases
Barry Warsaw <barry@python.org>
parents: 22939
diff changeset
725 except SyntaxError:
077d44b0405b Complete a merge of the mimelib project and the Python cvs codebases
Barry Warsaw <barry@python.org>
parents: 22939
diff changeset
726 # Must be using Python 2.1
077d44b0405b Complete a merge of the mimelib project and the Python cvs codebases
Barry Warsaw <barry@python.org>
parents: 22939
diff changeset
727 from email._compat21 import walk
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
728
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
729 def get_charsets(self, failobj=None):
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
730 """Return a list containing the charset(s) used in this message.
20240
5c4261e74478 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 20104
diff changeset
731
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
732 The returned list of items describes the Content-Type: headers'
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
733 charset parameter for this message and all the subparts in its
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
734 payload.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
735
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
736 Each item will either be a string (the value of the charset parameter
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
737 in the Content-Type: header of that part) or the value of the
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
738 'failobj' parameter (defaults to None), if the part does not have a
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
739 main MIME type of "text", or the charset is not defined.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
740
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
741 The list will contain one string for each part of the message, plus
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
742 one for the container message (i.e. self), so that a non-multipart
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
743 message will still return a list of length 1.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
744 """
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
745 return [part.get_param('charset', failobj) for part in self.walk()]