annotate Lib/email/Message.py @ 27998:e27f58a50737 legacy-trunk

SF bug #690377: Dud footnote reference in distutils docs
author Raymond Hettinger <python@rcn.com>
date Fri, 21 Feb 2003 02:47:09 +0000
parents feb8863b6fc4
children fa18df746ec5
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
25469
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
19 try:
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
20 True, False
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
21 except NameError:
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
22 True = 1
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
23 False = 0
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
24
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
25 # 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
26 # 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
27 # 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
28 # 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
29 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
30 # 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
31 # 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
32 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
33
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
34
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
35
24052
cb16995aa7e8 Oleg Broytmann's support for RFC 2231 encoded parameters, SF patch #549133
Barry Warsaw <barry@python.org>
parents: 23638
diff changeset
36 # Helper functions
25469
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
37 def _formatparam(param, value=None, quote=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
38 """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
39
24052
cb16995aa7e8 Oleg Broytmann's support for RFC 2231 encoded parameters, SF patch #549133
Barry Warsaw <barry@python.org>
parents: 23638
diff changeset
40 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
41 """
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
42 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
43 # 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
44 # 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
45 # instance.
cb16995aa7e8 Oleg Broytmann's support for RFC 2231 encoded parameters, SF patch #549133
Barry Warsaw <barry@python.org>
parents: 23638
diff changeset
46 if isinstance(value, TupleType):
25135
0f0016632be4 _formatparam(), set_param(): RFC 2231 encoding support by Oleg
Barry Warsaw <barry@python.org>
parents: 25132
diff changeset
47 # Encode as per RFC 2231
0f0016632be4 _formatparam(), set_param(): RFC 2231 encoding support by Oleg
Barry Warsaw <barry@python.org>
parents: 25132
diff changeset
48 param += '*'
0f0016632be4 _formatparam(), set_param(): RFC 2231 encoding support by Oleg
Barry Warsaw <barry@python.org>
parents: 25132
diff changeset
49 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
50 # 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
51 # 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
52 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
53 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
54 else:
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
55 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
56 else:
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
57 return param
20104
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
58
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
59
24052
cb16995aa7e8 Oleg Broytmann's support for RFC 2231 encoded parameters, SF patch #549133
Barry Warsaw <barry@python.org>
parents: 23638
diff changeset
60 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
61 if isinstance(value, TupleType):
25447
e8cdea44f7ab Fixing some RFC 2231 related issues as reported in the Spambayes
Barry Warsaw <barry@python.org>
parents: 25205
diff changeset
62 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
63 else:
25003
c744144c4049 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 24912
diff changeset
64 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
65
cb16995aa7e8 Oleg Broytmann's support for RFC 2231 encoded parameters, SF patch #549133
Barry Warsaw <barry@python.org>
parents: 23638
diff changeset
66
25049
Barry Warsaw <barry@python.org>
parents: 25003
diff changeset
67
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
68 class Message:
25499
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
69 """Basic message object.
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
70
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
71 A message object is defined as something that has a bunch of RFC 2822
25499
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
72 headers and a payload. It may optionally have an envelope header
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
73 (a.k.a. Unix-From or From_ header). If the message is a container (i.e. a
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
74 multipart or a message/rfc822), then the payload is a list of Message
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
75 objects, otherwise it is a string.
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
76
25499
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
77 Message objects implement part of the `mapping' interface, which assumes
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
78 there is exactly one occurrance of the header per message. Some headers
25469
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
79 do in fact appear multiple times (e.g. Received) and for those headers,
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
80 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
81 the mapping methods are implemented.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
82 """
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
83 def __init__(self):
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
84 self._headers = []
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
85 self._unixfrom = None
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
86 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
87 self._charset = None
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
88 # Defaults for multipart messages
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
89 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
90 # 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
91 self._default_type = 'text/plain'
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
92
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
93 def __str__(self):
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
94 """Return the entire formatted message as a string.
25499
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
95 This includes the headers, body, and envelope header.
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
96 """
25469
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
97 return self.as_string(unixfrom=True)
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
98
25469
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
99 def as_string(self, unixfrom=False):
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
100 """Return the entire formatted message as a string.
25469
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
101 Optional `unixfrom' when True, means include the Unix From_ envelope
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
102 header.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
103 """
23638
8c215ddfd3d1 Use absolute import paths for intrapackage imports.
Barry Warsaw <barry@python.org>
parents: 23521
diff changeset
104 from email.Generator import Generator
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
105 fp = StringIO()
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
106 g = Generator(fp)
23638
8c215ddfd3d1 Use absolute import paths for intrapackage imports.
Barry Warsaw <barry@python.org>
parents: 23521
diff changeset
107 g.flatten(self, unixfrom=unixfrom)
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
108 return fp.getvalue()
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 def is_multipart(self):
25469
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
111 """Return True if the message consists of multiple parts."""
25470
0fbbe79b4edb is_multipart(): Use isinstance() instead of type equality.
Barry Warsaw <barry@python.org>
parents: 25469
diff changeset
112 if isinstance(self._payload, ListType):
25469
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
113 return True
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
114 return False
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
115
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
116 #
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
117 # Unix From_ line
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 def set_unixfrom(self, unixfrom):
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
120 self._unixfrom = unixfrom
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
121
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
122 def get_unixfrom(self):
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
123 return self._unixfrom
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
124
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
125 #
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
126 # Payload manipulation.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
127 #
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
128 def add_payload(self, payload):
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
129 """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
130
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
131 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
132 a scalar, set to the given value.
25499
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
133
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
134 Note: This method is deprecated. Use .attach() instead.
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
135 """
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
136 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
137 DeprecationWarning, 2)
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
138 if self._payload is None:
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
139 self._payload = payload
25469
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
140 elif isinstance(self._payload, ListType):
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
141 self._payload.append(payload)
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
142 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
143 raise Errors.MultipartConversionError(
25469
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
144 'Message main content type must be "multipart" or missing')
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
145 else:
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
146 self._payload = [self._payload, payload]
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
147
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
148 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
149 """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
150
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
151 The current payload will always be a list of objects after this method
25499
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
152 is called. If you want to set the payload to a scalar object, use
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
153 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
154 """
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
155 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
156 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
157 else:
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
158 self._payload.append(payload)
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
159
25469
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
160 def get_payload(self, i=None, decode=False):
25205
2d13f9d98135 get_payload(): Document that calling it with no arguments returns a
Barry Warsaw <barry@python.org>
parents: 25135
diff changeset
161 """Return a reference to the payload.
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
162
25499
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
163 The payload will either be a list object or a string. If you mutate
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
164 the list object, you modify the message's payload in place. Optional
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
165 i returns that index into the payload.
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
166
25499
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
167 Optional decode is a flag (defaulting to False) indicating whether the
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
168 payload should be decoded or not, according to the
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
169 Content-Transfer-Encoding header. When True and the message is not a
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
170 multipart, the payload will be decoded if this header's value is
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
171 `quoted-printable' or `base64'. If some other encoding is used, or
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
172 the header is missing, the payload is returned as-is (undecoded). If
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
173 the message is a multipart and the decode flag is True, then None is
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
174 returned.
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
175 """
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
176 if i is None:
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
177 payload = self._payload
25469
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
178 elif not isinstance(self._payload, ListType):
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
179 raise TypeError, i
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
180 else:
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
181 payload = self._payload[i]
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
182 if decode:
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
183 if self.is_multipart():
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
184 return None
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
185 cte = self.get('content-transfer-encoding', '')
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
186 if cte.lower() == 'quoted-printable':
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
187 return Utils._qdecode(payload)
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
188 elif cte.lower() == 'base64':
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
189 return Utils._bdecode(payload)
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
190 # 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
191 # unchanged.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
192 return payload
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
193
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
194 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
195 """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
196
25499
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
197 Optional charset sets the message's default character set. See
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
198 set_charset() for details.
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
199 """
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
200 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
201 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
202 self.set_charset(charset)
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
203
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
204 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
205 """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
206
25499
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
207 charset can be a Charset instance, a string naming a character set, or
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
208 None. If it is a string it will be converted to a Charset instance.
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
209 If charset is None, the charset parameter will be removed from the
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
210 Content-Type field. Anything else will generate a TypeError.
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
211
25499
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
212 The message will be assumed to be of type text/* encoded with
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
213 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
214 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
215 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
216 Content-Type, Content-Transfer-Encoding) will be added as needed.
25499
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
217
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
218 """
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
219 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
220 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
221 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
222 return
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
223 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
224 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
225 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
226 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
227 # 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
228 # 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
229 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
230 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
231 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
232 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
233 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
234 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
235 else:
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
236 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
237 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
238 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
239 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
240 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
241 else:
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
242 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
243
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
244 def get_charset(self):
25499
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
245 """Return the Charset instance associated with the message's payload.
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
246 """
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
247 return self._charset
23521
a90faa5f9417 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 23473
diff changeset
248
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
249 #
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
250 # MAPPING INTERFACE (partial)
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 def __len__(self):
20104
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
253 """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
254 return len(self._headers)
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
255
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
256 def __getitem__(self, name):
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
257 """Get a header value.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
258
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
259 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
260
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
261 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
262 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
263 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
264 """
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
265 return self.get(name)
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 def __setitem__(self, name, val):
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
268 """Set the value of a header.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
269
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
270 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
271 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
272 """
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
273 self._headers.append((name, val))
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
274
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
275 def __delitem__(self, name):
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
276 """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
277
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
278 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
279 """
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
280 name = name.lower()
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
281 newheaders = []
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
282 for k, v in self._headers:
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
283 if k.lower() <> name:
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
284 newheaders.append((k, v))
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
285 self._headers = newheaders
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
286
25499
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
287 def __contains__(self, name):
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
288 return name.lower() in [k.lower() for k, v in self._headers]
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
289
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
290 def has_key(self, name):
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
291 """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
292 missing = []
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
293 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
294
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
295 def keys(self):
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
296 """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
297
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
298 These will be sorted in the order they appeared in the original
25499
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
299 message, or were added to the message, and may contain duplicates.
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
300 Any fields deleted and re-inserted are always appended to the header
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
301 list.
20042
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 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
304
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
305 def values(self):
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
306 """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
307
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
308 These will be sorted in the order they appeared in the original
25499
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
309 message, or were added to the message, and may contain duplicates.
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
310 Any fields deleted and re-inserted are always appended to the header
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
311 list.
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
312 """
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
313 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
314
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
315 def items(self):
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
316 """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
317
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
318 These will be sorted in the order they appeared in the original
25499
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
319 message, or were added to the message, and may contain duplicates.
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
320 Any fields deleted and re-inserted are always appended to the header
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
321 list.
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
322 """
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
323 return self._headers[:]
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(self, name, failobj=None):
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
326 """Get a header value.
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 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
329 is missing.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
330 """
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
331 name = name.lower()
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
332 for k, v in self._headers:
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
333 if k.lower() == name:
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
334 return v
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
335 return failobj
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
336
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
337 #
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
338 # Additional useful stuff
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
339 #
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
340
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
341 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
342 """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
343
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
344 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
345 message, and may contain duplicates. Any fields deleted and
21042
0846b4e2517d Docstring typo fix.
Greg Ward <gward@python.net>
parents: 20886
diff changeset
346 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
347
86896b5f1ebc get_all(): We never returned failobj if we found no matching headers.
Barry Warsaw <barry@python.org>
parents: 20253
diff changeset
348 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
349 """
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
350 values = []
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
351 name = name.lower()
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
352 for k, v in self._headers:
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
353 if k.lower() == name:
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
354 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
355 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
356 return failobj
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
357 return values
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
358
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
359 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
360 """Extended header setting.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
361
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
362 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
363 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
364 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
365 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
366
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
367 Example:
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
368
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
369 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
370 """
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
371 parts = []
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
372 for k, v in _params.items():
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
373 if v is None:
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
374 parts.append(k.replace('_', '-'))
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
375 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
376 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
377 if _value is not None:
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
378 parts.insert(0, _value)
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
379 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
380
25132
b89ebd57ceb4 replace_header(): New method given by Skip Montanaro in SF patch
Barry Warsaw <barry@python.org>
parents: 25049
diff changeset
381 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
382 """Replace a header.
b89ebd57ceb4 replace_header(): New method given by Skip Montanaro in SF patch
Barry Warsaw <barry@python.org>
parents: 25049
diff changeset
383
b89ebd57ceb4 replace_header(): New method given by Skip Montanaro in SF patch
Barry Warsaw <barry@python.org>
parents: 25049
diff changeset
384 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
385 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
386 raised.
b89ebd57ceb4 replace_header(): New method given by Skip Montanaro in SF patch
Barry Warsaw <barry@python.org>
parents: 25049
diff changeset
387 """
b89ebd57ceb4 replace_header(): New method given by Skip Montanaro in SF patch
Barry Warsaw <barry@python.org>
parents: 25049
diff changeset
388 _name = _name.lower()
b89ebd57ceb4 replace_header(): New method given by Skip Montanaro in SF patch
Barry Warsaw <barry@python.org>
parents: 25049
diff changeset
389 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
390 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
391 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
392 break
b89ebd57ceb4 replace_header(): New method given by Skip Montanaro in SF patch
Barry Warsaw <barry@python.org>
parents: 25049
diff changeset
393 else:
b89ebd57ceb4 replace_header(): New method given by Skip Montanaro in SF patch
Barry Warsaw <barry@python.org>
parents: 25049
diff changeset
394 raise KeyError, _name
b89ebd57ceb4 replace_header(): New method given by Skip Montanaro in SF patch
Barry Warsaw <barry@python.org>
parents: 25049
diff changeset
395
24332
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
396 #
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
397 # 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
398 # 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
399 #
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
400
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
401 def get_type(self, failobj=None):
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
402 """Returns the message's content type.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
403
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
404 The returned string is coerced to lowercase and returned as a single
25469
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
405 string of the form `maintype/subtype'. If there was no Content-Type
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
406 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
407 """
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
408 missing = []
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
409 value = self.get('content-type', missing)
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
410 if value is missing:
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
411 return failobj
24308
69134c61f87c Anthony Baxter's cleanup patch. Python project SF patch # 583190,
Barry Warsaw <barry@python.org>
parents: 24153
diff changeset
412 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
413
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
414 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
415 """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
416 missing = []
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
417 ctype = self.get_type(missing)
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
418 if ctype is missing:
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
419 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
420 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
421 return failobj
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
422 return ctype.split('/')[0]
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
423
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
424 def get_subtype(self, failobj=None):
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
425 """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
426 missing = []
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
427 ctype = self.get_type(missing)
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
428 if ctype is missing:
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
429 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
430 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
431 return failobj
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
432 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
433
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 # 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
436 #
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
437
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
438 def get_content_type(self):
25499
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
439 """Return the message's content type.
24332
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
440
25499
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
441 The returned string is coerced to lower case of the form
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
442 `maintype/subtype'. If there was no Content-Type header in the
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
443 message, the default type as given by get_default_type() will be
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
444 returned. Since according to RFC 2045, messages always have a default
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
445 type this will always return a value.
24332
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
446
25499
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
447 RFC 2045 defines a message's default type to be text/plain unless it
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
448 appears inside a multipart/digest container, in which case it would be
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
449 message/rfc822.
24332
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
450 """
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
451 missing = []
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
452 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
453 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
454 # 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
455 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
456 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
457 # 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
458 if ctype.count('/') <> 1:
0ee48aefa474 get_content_type(), get_content_maintype(), get_content_subtype(): RFC
Barry Warsaw <barry@python.org>
parents: 24332
diff changeset
459 return 'text/plain'
0ee48aefa474 get_content_type(), get_content_maintype(), get_content_subtype(): RFC
Barry Warsaw <barry@python.org>
parents: 24332
diff changeset
460 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
461
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
462 def get_content_maintype(self):
25499
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
463 """Return the message's main content type.
24332
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
464
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
465 This is the `maintype' part of the string returned by
25499
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
466 get_content_type().
24332
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
467 """
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
468 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
469 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
470
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
471 def get_content_subtype(self):
25499
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
472 """Returns the message's sub-content type.
24332
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
473
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
474 This is the `subtype' part of the string returned by
25499
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
475 get_content_type().
24332
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
476 """
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
477 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
478 return ctype.split('/')[1]
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
479
24153
a59df2fa4ec8 Add the concept of a "default type". Normally the default type is
Barry Warsaw <barry@python.org>
parents: 24052
diff changeset
480 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
481 """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
482
a59df2fa4ec8 Add the concept of a "default type". Normally the default type is
Barry Warsaw <barry@python.org>
parents: 24052
diff changeset
483 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
484 messages that are subparts of multipart/digest containers. Such
25499
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
485 subparts have a default content type of message/rfc822.
24153
a59df2fa4ec8 Add the concept of a "default type". Normally the default type is
Barry Warsaw <barry@python.org>
parents: 24052
diff changeset
486 """
a59df2fa4ec8 Add the concept of a "default type". Normally the default type is
Barry Warsaw <barry@python.org>
parents: 24052
diff changeset
487 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
488
a59df2fa4ec8 Add the concept of a "default type". Normally the default type is
Barry Warsaw <barry@python.org>
parents: 24052
diff changeset
489 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
490 """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
491
24332
8b4a165baaee To better support default content types, fix an API wart, and preserve
Barry Warsaw <barry@python.org>
parents: 24308
diff changeset
492 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
493 is not enforced. The default content type is not stored in the
25469
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
494 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
495 """
a59df2fa4ec8 Add the concept of a "default type". Normally the default type is
Barry Warsaw <barry@python.org>
parents: 24052
diff changeset
496 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
497
20104
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
498 def _get_params_preserve(self, failobj, header):
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
499 # 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
500 # 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
501 missing = []
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
502 value = self.get(header, missing)
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
503 if value is missing:
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
504 return failobj
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
505 params = []
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
506 for p in paramre.split(value):
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
507 try:
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
508 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
509 name = name.strip()
69134c61f87c Anthony Baxter's cleanup patch. Python project SF patch # 583190,
Barry Warsaw <barry@python.org>
parents: 24153
diff changeset
510 val = val.strip()
20104
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
511 except ValueError:
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
512 # 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
513 name = p.strip()
20104
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
514 val = ''
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
515 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
516 params = Utils.decode_params(params)
20104
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
517 return params
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
518
25469
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
519 def get_params(self, failobj=None, header='content-type', unquote=True):
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
520 """Return the message's Content-Type parameters, as a list.
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
521
20104
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
522 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
523 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
524 while the right hand side is the value. If there is no `=' sign in
25447
e8cdea44f7ab Fixing some RFC 2231 related issues as reported in the Spambayes
Barry Warsaw <barry@python.org>
parents: 25205
diff changeset
525 the parameter the value is the empty string. The value is as
e8cdea44f7ab Fixing some RFC 2231 related issues as reported in the Spambayes
Barry Warsaw <barry@python.org>
parents: 25205
diff changeset
526 described in the get_param() method.
20104
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
527
25469
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
528 Optional failobj is the object to return if there is no Content-Type
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
529 header. Optional header is the header to search instead of
25469
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
530 Content-Type. If unquote is True, the value is unquoted.
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
531 """
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
532 missing = []
20104
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
533 params = self._get_params_preserve(missing, header)
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
534 if params is missing:
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
535 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
536 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
537 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
538 else:
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
539 return params
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
540
25469
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
541 def get_param(self, param, failobj=None, header='content-type',
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
542 unquote=True):
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
543 """Return the parameter value if found in the Content-Type header.
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
544
25469
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
545 Optional failobj is the object to return if there is no Content-Type
25447
e8cdea44f7ab Fixing some RFC 2231 related issues as reported in the Spambayes
Barry Warsaw <barry@python.org>
parents: 25205
diff changeset
546 header, or the Content-Type header has no such parameter. Optional
25469
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
547 header is the header to search instead of Content-Type.
20104
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
548
25447
e8cdea44f7ab Fixing some RFC 2231 related issues as reported in the Spambayes
Barry Warsaw <barry@python.org>
parents: 25205
diff changeset
549 Parameter keys are always compared case insensitively. The return
e8cdea44f7ab Fixing some RFC 2231 related issues as reported in the Spambayes
Barry Warsaw <barry@python.org>
parents: 25205
diff changeset
550 value can either be a string, or a 3-tuple if the parameter was RFC
e8cdea44f7ab Fixing some RFC 2231 related issues as reported in the Spambayes
Barry Warsaw <barry@python.org>
parents: 25205
diff changeset
551 2231 encoded. When it's a 3-tuple, the elements of the value are of
e8cdea44f7ab Fixing some RFC 2231 related issues as reported in the Spambayes
Barry Warsaw <barry@python.org>
parents: 25205
diff changeset
552 the form (CHARSET, LANGUAGE, VALUE), where LANGUAGE may be the empty
e8cdea44f7ab Fixing some RFC 2231 related issues as reported in the Spambayes
Barry Warsaw <barry@python.org>
parents: 25205
diff changeset
553 string. Your application should be prepared to deal with these, and
e8cdea44f7ab Fixing some RFC 2231 related issues as reported in the Spambayes
Barry Warsaw <barry@python.org>
parents: 25205
diff changeset
554 can convert the parameter to a Unicode string like so:
e8cdea44f7ab Fixing some RFC 2231 related issues as reported in the Spambayes
Barry Warsaw <barry@python.org>
parents: 25205
diff changeset
555
e8cdea44f7ab Fixing some RFC 2231 related issues as reported in the Spambayes
Barry Warsaw <barry@python.org>
parents: 25205
diff changeset
556 param = msg.get_param('foo')
e8cdea44f7ab Fixing some RFC 2231 related issues as reported in the Spambayes
Barry Warsaw <barry@python.org>
parents: 25205
diff changeset
557 if isinstance(param, tuple):
e8cdea44f7ab Fixing some RFC 2231 related issues as reported in the Spambayes
Barry Warsaw <barry@python.org>
parents: 25205
diff changeset
558 param = unicode(param[2], param[0])
e8cdea44f7ab Fixing some RFC 2231 related issues as reported in the Spambayes
Barry Warsaw <barry@python.org>
parents: 25205
diff changeset
559
e8cdea44f7ab Fixing some RFC 2231 related issues as reported in the Spambayes
Barry Warsaw <barry@python.org>
parents: 25205
diff changeset
560 In any case, the parameter value (either the returned string, or the
e8cdea44f7ab Fixing some RFC 2231 related issues as reported in the Spambayes
Barry Warsaw <barry@python.org>
parents: 25205
diff changeset
561 VALUE item in the 3-tuple) is always unquoted, unless unquote is set
25469
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
562 to False.
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
563 """
20104
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
564 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
565 return failobj
20104
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
566 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
567 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
568 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
569 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
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 return v
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
572 return failobj
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
573
25469
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
574 def set_param(self, param, value, header='Content-Type', requote=True,
25135
0f0016632be4 _formatparam(), set_param(): RFC 2231 encoding support by Oleg
Barry Warsaw <barry@python.org>
parents: 25132
diff changeset
575 charset=None, language=''):
25469
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
576 """Set a parameter in the Content-Type header.
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
577
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
578 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
579 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
580
25499
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
581 If header is Content-Type and has not yet been defined for this
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
582 message, it will be set to "text/plain" and the new parameter and
25499
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
583 value will be appended as per RFC 2045.
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
584
25469
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
585 An alternate header can specified in the header argument, and all
25499
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
586 parameters will be quoted as necessary unless requote is False.
25135
0f0016632be4 _formatparam(), set_param(): RFC 2231 encoding support by Oleg
Barry Warsaw <barry@python.org>
parents: 25132
diff changeset
587
25499
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
588 If charset is specified, the parameter will be encoded according to RFC
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
589 2231. Optional language specifies the RFC 2231 language, defaulting
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
590 to the empty string. Both charset and language should be strings.
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
591 """
25135
0f0016632be4 _formatparam(), set_param(): RFC 2231 encoding support by Oleg
Barry Warsaw <barry@python.org>
parents: 25132
diff changeset
592 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
593 value = (charset, language, value)
0f0016632be4 _formatparam(), set_param(): RFC 2231 encoding support by Oleg
Barry Warsaw <barry@python.org>
parents: 25132
diff changeset
594
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
595 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
596 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
597 else:
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
598 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
599 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
600 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
601 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
602 else:
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
603 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
604 [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
605 else:
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
606 ctype = ''
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
607 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
608 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
609 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
610 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
611 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
612 else:
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
613 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
614 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
615 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
616 else:
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
617 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
618 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
619 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
620 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
621
25469
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
622 def del_param(self, param, header='content-type', requote=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
623 """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
624
25499
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
625 The header will be re-written in place without the parameter or its
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
626 value. All values will be quoted as necessary unless requote is
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
627 False. Optional header specifies an alternative to the Content-Type
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
628 header.
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
629 """
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
630 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
631 return
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
632 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
633 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
634 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
635 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
636 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
637 else:
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
638 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
639 _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
640 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
641 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
642 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
643
25469
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
644 def set_type(self, type, header='Content-Type', requote=True):
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
645 """Set the main type and subtype for the Content-Type header.
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
646
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
647 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
648 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
649
25469
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
650 This method replaces the Content-Type header, keeping all the
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
651 parameters in place. If requote is False, this leaves the existing
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
652 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
653 default).
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
654
25499
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
655 An alternative header can be specified in the header argument. When
0aa64eebd493 __contains__(): Change the second argument to `name' for consistency.
Barry Warsaw <barry@python.org>
parents: 25470
diff changeset
656 the Content-Type header is set, we'll always also add a MIME-Version
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
657 header.
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
658 """
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
659 # 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
660 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
661 raise ValueError
25469
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
662 # Set the Content-Type, you get a MIME-Version
22939
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
663 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
664 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
665 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
666 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
667 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
668 return
534aab5bccea Sync'ing with standalone email package 2.0.1. This adds support for
Barry Warsaw <barry@python.org>
parents: 21043
diff changeset
669 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
670 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
671 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
672 # 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
673 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
674 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
675
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
676 def get_filename(self, failobj=None):
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
677 """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
678
25469
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
679 The filename is extracted from the Content-Disposition header's
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
680 `filename' parameter, and it is unquoted.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
681 """
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
682 missing = []
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
683 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
684 if filename is missing:
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
685 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
686 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
687 # 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
688 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
689 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
690 else:
cb16995aa7e8 Oleg Broytmann's support for RFC 2231 encoded parameters, SF patch #549133
Barry Warsaw <barry@python.org>
parents: 23638
diff changeset
691 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
692 return newvalue
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
693
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
694 def get_boundary(self, failobj=None):
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
695 """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
696
25469
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
697 The boundary is extracted from the Content-Type header's `boundary'
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
698 parameter, and it is unquoted.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
699 """
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
700 missing = []
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
701 boundary = self.get_param('boundary', missing)
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
702 if boundary is missing:
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
703 return failobj
25447
e8cdea44f7ab Fixing some RFC 2231 related issues as reported in the Spambayes
Barry Warsaw <barry@python.org>
parents: 25205
diff changeset
704 if isinstance(boundary, TupleType):
e8cdea44f7ab Fixing some RFC 2231 related issues as reported in the Spambayes
Barry Warsaw <barry@python.org>
parents: 25205
diff changeset
705 # RFC 2231 encoded, so decode. It better end up as ascii
e8cdea44f7ab Fixing some RFC 2231 related issues as reported in the Spambayes
Barry Warsaw <barry@python.org>
parents: 25205
diff changeset
706 return unicode(boundary[2], boundary[0]).encode('us-ascii')
24052
cb16995aa7e8 Oleg Broytmann's support for RFC 2231 encoded parameters, SF patch #549133
Barry Warsaw <barry@python.org>
parents: 23638
diff changeset
707 return _unquotevalue(boundary.strip())
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
708
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
709 def set_boundary(self, boundary):
25469
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
710 """Set the boundary parameter in Content-Type to 'boundary'.
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
711
25469
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
712 This is subtly different than deleting the Content-Type header and
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
713 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
714 main difference is that using the set_boundary() method preserves the
25469
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
715 order of the Content-Type header in the original message.
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
716
25469
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
717 HeaderParseError is raised if the message has no Content-Type header.
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
718 """
20104
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
719 missing = []
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
720 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
721 if params is missing:
25469
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
722 # There was no Content-Type header, and we don't know what type
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
723 # to set it to, so raise an exception.
25469
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
724 raise Errors.HeaderParseError, 'No Content-Type header found'
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
725 newparams = []
25469
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
726 foundp = False
20104
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
727 for pk, pv in params:
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
728 if pk.lower() == 'boundary':
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
729 newparams.append(('boundary', '"%s"' % boundary))
25469
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
730 foundp = True
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
731 else:
20104
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
732 newparams.append((pk, pv))
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
733 if not foundp:
25469
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
734 # The original Content-Type header had no boundary attribute.
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
735 # 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
736 # instead???
20104
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
737 newparams.append(('boundary', '"%s"' % boundary))
25469
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
738 # Replace the existing Content-Type header with the new value
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
739 newheaders = []
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
740 for h, v in self._headers:
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
741 if h.lower() == 'content-type':
20104
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
742 parts = []
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
743 for k, v in newparams:
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
744 if v == '':
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
745 parts.append(k)
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
746 else:
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
747 parts.append('%s=%s' % (k, v))
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
748 newheaders.append((h, SEMISPACE.join(parts)))
8a1614ceb377 has_key(): Implement in terms of get().
Barry Warsaw <barry@python.org>
parents: 20042
diff changeset
749
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
750 else:
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
751 newheaders.append((h, v))
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
752 self._headers = newheaders
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
753
23473
077d44b0405b Complete a merge of the mimelib project and the Python cvs codebases
Barry Warsaw <barry@python.org>
parents: 22939
diff changeset
754 try:
077d44b0405b Complete a merge of the mimelib project and the Python cvs codebases
Barry Warsaw <barry@python.org>
parents: 22939
diff changeset
755 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
756 except SyntaxError:
077d44b0405b Complete a merge of the mimelib project and the Python cvs codebases
Barry Warsaw <barry@python.org>
parents: 22939
diff changeset
757 # 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
758 from email._compat21 import walk
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
759
25447
e8cdea44f7ab Fixing some RFC 2231 related issues as reported in the Spambayes
Barry Warsaw <barry@python.org>
parents: 25205
diff changeset
760 def get_content_charset(self, failobj=None):
e8cdea44f7ab Fixing some RFC 2231 related issues as reported in the Spambayes
Barry Warsaw <barry@python.org>
parents: 25205
diff changeset
761 """Return the charset parameter of the Content-Type header.
e8cdea44f7ab Fixing some RFC 2231 related issues as reported in the Spambayes
Barry Warsaw <barry@python.org>
parents: 25205
diff changeset
762
25698
feb8863b6fc4 get_content_charset(): RFC 2046 $4.1.2 says charsets are not case
Barry Warsaw <barry@python.org>
parents: 25499
diff changeset
763 The returned string is always coerced to lower case. If there is no
feb8863b6fc4 get_content_charset(): RFC 2046 $4.1.2 says charsets are not case
Barry Warsaw <barry@python.org>
parents: 25499
diff changeset
764 Content-Type header, or if that header has no charset parameter,
feb8863b6fc4 get_content_charset(): RFC 2046 $4.1.2 says charsets are not case
Barry Warsaw <barry@python.org>
parents: 25499
diff changeset
765 failobj is returned.
25447
e8cdea44f7ab Fixing some RFC 2231 related issues as reported in the Spambayes
Barry Warsaw <barry@python.org>
parents: 25205
diff changeset
766 """
e8cdea44f7ab Fixing some RFC 2231 related issues as reported in the Spambayes
Barry Warsaw <barry@python.org>
parents: 25205
diff changeset
767 missing = []
e8cdea44f7ab Fixing some RFC 2231 related issues as reported in the Spambayes
Barry Warsaw <barry@python.org>
parents: 25205
diff changeset
768 charset = self.get_param('charset', missing)
e8cdea44f7ab Fixing some RFC 2231 related issues as reported in the Spambayes
Barry Warsaw <barry@python.org>
parents: 25205
diff changeset
769 if charset is missing:
e8cdea44f7ab Fixing some RFC 2231 related issues as reported in the Spambayes
Barry Warsaw <barry@python.org>
parents: 25205
diff changeset
770 return failobj
e8cdea44f7ab Fixing some RFC 2231 related issues as reported in the Spambayes
Barry Warsaw <barry@python.org>
parents: 25205
diff changeset
771 if isinstance(charset, TupleType):
e8cdea44f7ab Fixing some RFC 2231 related issues as reported in the Spambayes
Barry Warsaw <barry@python.org>
parents: 25205
diff changeset
772 # RFC 2231 encoded, so decode it, and it better end up as ascii.
25698
feb8863b6fc4 get_content_charset(): RFC 2046 $4.1.2 says charsets are not case
Barry Warsaw <barry@python.org>
parents: 25499
diff changeset
773 charset = unicode(charset[2], charset[0]).encode('us-ascii')
feb8863b6fc4 get_content_charset(): RFC 2046 $4.1.2 says charsets are not case
Barry Warsaw <barry@python.org>
parents: 25499
diff changeset
774 # RFC 2046, $4.1.2 says charsets are not case sensitive
feb8863b6fc4 get_content_charset(): RFC 2046 $4.1.2 says charsets are not case
Barry Warsaw <barry@python.org>
parents: 25499
diff changeset
775 return charset.lower()
25447
e8cdea44f7ab Fixing some RFC 2231 related issues as reported in the Spambayes
Barry Warsaw <barry@python.org>
parents: 25205
diff changeset
776
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
777 def get_charsets(self, failobj=None):
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
778 """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
779
25469
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
780 The returned list of items describes the Content-Type headers'
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
781 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
782 payload.
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
783
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
784 Each item will either be a string (the value of the charset parameter
25469
55f090b38b88 Docstring and code cleanups, e.g. use True/False everywhere.
Barry Warsaw <barry@python.org>
parents: 25447
diff changeset
785 in the Content-Type header of that part) or the value of the
20042
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
786 '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
787 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
788
e08f423f9165 The email package version 1.0, prototyped as mimelib
Barry Warsaw <barry@python.org>
parents:
diff changeset
789 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
790 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
791 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
792 """
25447
e8cdea44f7ab Fixing some RFC 2231 related issues as reported in the Spambayes
Barry Warsaw <barry@python.org>
parents: 25205
diff changeset
793 return [part.get_content_charset(failobj) for part in self.walk()]