R David Murray | 79cf3ba | 2012-05-27 17:10:36 -0400 | [diff] [blame] | 1 | :mod:`email.policy`: Policy Objects |
| 2 | ----------------------------------- |
R David Murray | 3edd22a | 2011-04-18 13:59:37 -0400 | [diff] [blame] | 3 | |
| 4 | .. module:: email.policy |
| 5 | :synopsis: Controlling the parsing and generating of messages |
| 6 | |
R David Murray | 79cf3ba | 2012-05-27 17:10:36 -0400 | [diff] [blame] | 7 | .. moduleauthor:: R. David Murray <rdmurray@bitdance.com> |
| 8 | .. sectionauthor:: R. David Murray <rdmurray@bitdance.com> |
| 9 | |
Éric Araujo | 54dbfbd | 2011-08-10 21:43:13 +0200 | [diff] [blame] | 10 | .. versionadded:: 3.3 |
R David Murray | 6a45d3b | 2011-04-18 16:00:47 -0400 | [diff] [blame] | 11 | |
R David Murray | 3edd22a | 2011-04-18 13:59:37 -0400 | [diff] [blame] | 12 | |
| 13 | The :mod:`email` package's prime focus is the handling of email messages as |
| 14 | described by the various email and MIME RFCs. However, the general format of |
| 15 | email messages (a block of header fields each consisting of a name followed by |
| 16 | a colon followed by a value, the whole block followed by a blank line and an |
| 17 | arbitrary 'body'), is a format that has found utility outside of the realm of |
| 18 | email. Some of these uses conform fairly closely to the main RFCs, some do |
| 19 | not. And even when working with email, there are times when it is desirable to |
| 20 | break strict compliance with the RFCs. |
| 21 | |
R David Murray | 6a45d3b | 2011-04-18 16:00:47 -0400 | [diff] [blame] | 22 | Policy objects give the email package the flexibility to handle all these |
| 23 | disparate use cases. |
R David Murray | 3edd22a | 2011-04-18 13:59:37 -0400 | [diff] [blame] | 24 | |
| 25 | A :class:`Policy` object encapsulates a set of attributes and methods that |
| 26 | control the behavior of various components of the email package during use. |
| 27 | :class:`Policy` instances can be passed to various classes and methods in the |
| 28 | email package to alter the default behavior. The settable values and their |
R David Murray | c27e522 | 2012-05-25 15:01:48 -0400 | [diff] [blame] | 29 | defaults are described below. |
R David Murray | 3edd22a | 2011-04-18 13:59:37 -0400 | [diff] [blame] | 30 | |
R David Murray | c27e522 | 2012-05-25 15:01:48 -0400 | [diff] [blame] | 31 | There is a default policy used by all classes in the email package. This |
| 32 | policy is named :class:`Compat32`, with a corresponding pre-defined instance |
| 33 | named :const:`compat32`. It provides for complete backward compatibility (in |
| 34 | some cases, including bug compatibility) with the pre-Python3.3 version of the |
| 35 | email package. |
| 36 | |
| 37 | The first part of this documentation covers the features of :class:`Policy`, an |
| 38 | :term:`abstract base class` that defines the features that are common to all |
| 39 | policy objects, including :const:`compat32`. This includes certain hook |
| 40 | methods that are called internally by the email package, which a custom policy |
| 41 | could override to obtain different behavior. |
| 42 | |
| 43 | When a :class:`~email.message.Message` object is created, it acquires a policy. |
| 44 | By default this will be :const:`compat32`, but a different policy can be |
| 45 | specified. If the ``Message`` is created by a :mod:`~email.parser`, a policy |
| 46 | passed to the parser will be the policy used by the ``Message`` it creates. If |
| 47 | the ``Message`` is created by the program, then the policy can be specified |
| 48 | when it is created. When a ``Message`` is passed to a :mod:`~email.generator`, |
| 49 | the generator uses the policy from the ``Message`` by default, but you can also |
| 50 | pass a specific policy to the generator that will override the one stored on |
| 51 | the ``Message`` object. |
| 52 | |
| 53 | :class:`Policy` instances are immutable, but they can be cloned, accepting the |
| 54 | same keyword arguments as the class constructor and returning a new |
| 55 | :class:`Policy` instance that is a copy of the original but with the specified |
| 56 | attributes values changed. |
R David Murray | 3edd22a | 2011-04-18 13:59:37 -0400 | [diff] [blame] | 57 | |
| 58 | As an example, the following code could be used to read an email message from a |
R David Murray | fdfb005 | 2013-07-29 15:49:58 -0400 | [diff] [blame] | 59 | file on disk and pass it to the system ``sendmail`` program on a Unix system: |
R David Murray | 3edd22a | 2011-04-18 13:59:37 -0400 | [diff] [blame] | 60 | |
R David Murray | fdfb005 | 2013-07-29 15:49:58 -0400 | [diff] [blame] | 61 | .. testsetup:: |
| 62 | |
| 63 | >>> from unittest import mock |
| 64 | >>> mocker = mock.patch('subprocess.Popen') |
| 65 | >>> m = mocker.start() |
| 66 | >>> proc = mock.MagicMock() |
| 67 | >>> m.return_value = proc |
| 68 | >>> proc.stdin.close.return_value = None |
| 69 | >>> mymsg = open('mymsg.txt', 'w') |
| 70 | >>> mymsg.write('To: abc@xyz.com\n\n') |
| 71 | 17 |
| 72 | >>> mymsg.flush() |
| 73 | |
| 74 | .. doctest:: |
| 75 | |
| 76 | >>> from email import message_from_binary_file |
R David Murray | 3edd22a | 2011-04-18 13:59:37 -0400 | [diff] [blame] | 77 | >>> from email.generator import BytesGenerator |
R David Murray | fdfb005 | 2013-07-29 15:49:58 -0400 | [diff] [blame] | 78 | >>> from email import policy |
R David Murray | 3edd22a | 2011-04-18 13:59:37 -0400 | [diff] [blame] | 79 | >>> from subprocess import Popen, PIPE |
R David Murray | fdfb005 | 2013-07-29 15:49:58 -0400 | [diff] [blame] | 80 | >>> with open('mymsg.txt', 'rb') as f: |
| 81 | ... msg = message_from_binary_file(f, policy=policy.default) |
| 82 | >>> p = Popen(['sendmail', msg['To'].addresses[0]], stdin=PIPE) |
R David Murray | c27e522 | 2012-05-25 15:01:48 -0400 | [diff] [blame] | 83 | >>> g = BytesGenerator(p.stdin, policy=msg.policy.clone(linesep='\r\n')) |
R David Murray | 3edd22a | 2011-04-18 13:59:37 -0400 | [diff] [blame] | 84 | >>> g.flatten(msg) |
| 85 | >>> p.stdin.close() |
| 86 | >>> rc = p.wait() |
| 87 | |
R David Murray | 11bfd32 | 2013-07-30 14:42:40 -0400 | [diff] [blame] | 88 | .. testsetup:: |
R David Murray | fdfb005 | 2013-07-29 15:49:58 -0400 | [diff] [blame] | 89 | |
| 90 | >>> mymsg.close() |
| 91 | >>> mocker.stop() |
| 92 | >>> import os |
| 93 | >>> os.remove('mymsg.txt') |
| 94 | |
R David Murray | c27e522 | 2012-05-25 15:01:48 -0400 | [diff] [blame] | 95 | Here we are telling :class:`~email.generator.BytesGenerator` to use the RFC |
| 96 | correct line separator characters when creating the binary string to feed into |
| 97 | ``sendmail's`` ``stdin``, where the default policy would use ``\n`` line |
| 98 | separators. |
Éric Araujo | fe0472e | 2011-12-03 16:00:56 +0100 | [diff] [blame] | 99 | |
R David Murray | 3edd22a | 2011-04-18 13:59:37 -0400 | [diff] [blame] | 100 | Some email package methods accept a *policy* keyword argument, allowing the |
R David Murray | 6a45d3b | 2011-04-18 16:00:47 -0400 | [diff] [blame] | 101 | policy to be overridden for that method. For example, the following code uses |
R David Murray | c27e522 | 2012-05-25 15:01:48 -0400 | [diff] [blame] | 102 | the :meth:`~email.message.Message.as_string` method of the *msg* object from |
| 103 | the previous example and writes the message to a file using the native line |
| 104 | separators for the platform on which it is running:: |
R David Murray | 3edd22a | 2011-04-18 13:59:37 -0400 | [diff] [blame] | 105 | |
| 106 | >>> import os |
R David Murray | 3edd22a | 2011-04-18 13:59:37 -0400 | [diff] [blame] | 107 | >>> with open('converted.txt', 'wb') as f: |
R David Murray | bb17d2b | 2013-08-09 16:15:28 -0400 | [diff] [blame] | 108 | ... f.write(msg.as_bytes(policy=msg.policy.clone(linesep=os.linesep))) |
| 109 | 17 |
R David Murray | 3edd22a | 2011-04-18 13:59:37 -0400 | [diff] [blame] | 110 | |
| 111 | Policy objects can also be combined using the addition operator, producing a |
| 112 | policy object whose settings are a combination of the non-default values of the |
| 113 | summed objects:: |
| 114 | |
R David Murray | fdfb005 | 2013-07-29 15:49:58 -0400 | [diff] [blame] | 115 | >>> compat_SMTP = policy.compat32.clone(linesep='\r\n') |
| 116 | >>> compat_strict = policy.compat32.clone(raise_on_defect=True) |
R David Murray | c27e522 | 2012-05-25 15:01:48 -0400 | [diff] [blame] | 117 | >>> compat_strict_SMTP = compat_SMTP + compat_strict |
R David Murray | 3edd22a | 2011-04-18 13:59:37 -0400 | [diff] [blame] | 118 | |
| 119 | This operation is not commutative; that is, the order in which the objects are |
| 120 | added matters. To illustrate:: |
| 121 | |
R David Murray | fdfb005 | 2013-07-29 15:49:58 -0400 | [diff] [blame] | 122 | >>> policy100 = policy.compat32.clone(max_line_length=100) |
| 123 | >>> policy80 = policy.compat32.clone(max_line_length=80) |
| 124 | >>> apolicy = policy100 + policy80 |
R David Murray | 3edd22a | 2011-04-18 13:59:37 -0400 | [diff] [blame] | 125 | >>> apolicy.max_line_length |
| 126 | 80 |
R David Murray | c27e522 | 2012-05-25 15:01:48 -0400 | [diff] [blame] | 127 | >>> apolicy = policy80 + policy100 |
R David Murray | 3edd22a | 2011-04-18 13:59:37 -0400 | [diff] [blame] | 128 | >>> apolicy.max_line_length |
| 129 | 100 |
| 130 | |
| 131 | |
| 132 | .. class:: Policy(**kw) |
| 133 | |
R David Murray | c27e522 | 2012-05-25 15:01:48 -0400 | [diff] [blame] | 134 | This is the :term:`abstract base class` for all policy classes. It provides |
| 135 | default implementations for a couple of trivial methods, as well as the |
| 136 | implementation of the immutability property, the :meth:`clone` method, and |
| 137 | the constructor semantics. |
| 138 | |
| 139 | The constructor of a policy class can be passed various keyword arguments. |
| 140 | The arguments that may be specified are any non-method properties on this |
| 141 | class, plus any additional non-method properties on the concrete class. A |
| 142 | value specified in the constructor will override the default value for the |
| 143 | corresponding attribute. |
| 144 | |
| 145 | This class defines the following properties, and thus values for the |
| 146 | following may be passed in the constructor of any policy class: |
R David Murray | 3edd22a | 2011-04-18 13:59:37 -0400 | [diff] [blame] | 147 | |
| 148 | .. attribute:: max_line_length |
| 149 | |
| 150 | The maximum length of any line in the serialized output, not counting the |
| 151 | end of line character(s). Default is 78, per :rfc:`5322`. A value of |
| 152 | ``0`` or :const:`None` indicates that no line wrapping should be |
| 153 | done at all. |
| 154 | |
| 155 | .. attribute:: linesep |
| 156 | |
| 157 | The string to be used to terminate lines in serialized output. The |
R David Murray | 6a45d3b | 2011-04-18 16:00:47 -0400 | [diff] [blame] | 158 | default is ``\n`` because that's the internal end-of-line discipline used |
R David Murray | c27e522 | 2012-05-25 15:01:48 -0400 | [diff] [blame] | 159 | by Python, though ``\r\n`` is required by the RFCs. |
R David Murray | 3edd22a | 2011-04-18 13:59:37 -0400 | [diff] [blame] | 160 | |
R David Murray | c27e522 | 2012-05-25 15:01:48 -0400 | [diff] [blame] | 161 | .. attribute:: cte_type |
R David Murray | 3edd22a | 2011-04-18 13:59:37 -0400 | [diff] [blame] | 162 | |
R David Murray | c27e522 | 2012-05-25 15:01:48 -0400 | [diff] [blame] | 163 | Controls the type of Content Transfer Encodings that may be or are |
| 164 | required to be used. The possible values are: |
| 165 | |
Georg Brandl | 44ea77b | 2013-03-28 13:28:44 +0100 | [diff] [blame] | 166 | .. tabularcolumns:: |l|L| |
| 167 | |
R David Murray | c27e522 | 2012-05-25 15:01:48 -0400 | [diff] [blame] | 168 | ======== =============================================================== |
| 169 | ``7bit`` all data must be "7 bit clean" (ASCII-only). This means that |
| 170 | where necessary data will be encoded using either |
| 171 | quoted-printable or base64 encoding. |
| 172 | |
| 173 | ``8bit`` data is not constrained to be 7 bit clean. Data in headers is |
| 174 | still required to be ASCII-only and so will be encoded (see |
| 175 | 'binary_fold' below for an exception), but body parts may use |
| 176 | the ``8bit`` CTE. |
| 177 | ======== =============================================================== |
| 178 | |
| 179 | A ``cte_type`` value of ``8bit`` only works with ``BytesGenerator``, not |
| 180 | ``Generator``, because strings cannot contain binary data. If a |
| 181 | ``Generator`` is operating under a policy that specifies |
| 182 | ``cte_type=8bit``, it will act as if ``cte_type`` is ``7bit``. |
R David Murray | 3edd22a | 2011-04-18 13:59:37 -0400 | [diff] [blame] | 183 | |
| 184 | .. attribute:: raise_on_defect |
| 185 | |
| 186 | If :const:`True`, any defects encountered will be raised as errors. If |
| 187 | :const:`False` (the default), defects will be passed to the |
| 188 | :meth:`register_defect` method. |
| 189 | |
R David Murray | c27e522 | 2012-05-25 15:01:48 -0400 | [diff] [blame] | 190 | The following :class:`Policy` method is intended to be called by code using |
| 191 | the email library to create policy instances with custom settings: |
R David Murray | 6a45d3b | 2011-04-18 16:00:47 -0400 | [diff] [blame] | 192 | |
R David Murray | c27e522 | 2012-05-25 15:01:48 -0400 | [diff] [blame] | 193 | .. method:: clone(**kw) |
R David Murray | 3edd22a | 2011-04-18 13:59:37 -0400 | [diff] [blame] | 194 | |
| 195 | Return a new :class:`Policy` instance whose attributes have the same |
| 196 | values as the current instance, except where those attributes are |
| 197 | given new values by the keyword arguments. |
| 198 | |
R David Murray | c27e522 | 2012-05-25 15:01:48 -0400 | [diff] [blame] | 199 | The remaining :class:`Policy` methods are called by the email package code, |
| 200 | and are not intended to be called by an application using the email package. |
| 201 | A custom policy must implement all of these methods. |
R David Murray | 3edd22a | 2011-04-18 13:59:37 -0400 | [diff] [blame] | 202 | |
R David Murray | c27e522 | 2012-05-25 15:01:48 -0400 | [diff] [blame] | 203 | .. method:: handle_defect(obj, defect) |
R David Murray | 3edd22a | 2011-04-18 13:59:37 -0400 | [diff] [blame] | 204 | |
R David Murray | c27e522 | 2012-05-25 15:01:48 -0400 | [diff] [blame] | 205 | Handle a *defect* found on *obj*. When the email package calls this |
| 206 | method, *defect* will always be a subclass of |
| 207 | :class:`~email.errors.Defect`. |
R David Murray | 3edd22a | 2011-04-18 13:59:37 -0400 | [diff] [blame] | 208 | |
R David Murray | c27e522 | 2012-05-25 15:01:48 -0400 | [diff] [blame] | 209 | The default implementation checks the :attr:`raise_on_defect` flag. If |
| 210 | it is ``True``, *defect* is raised as an exception. If it is ``False`` |
| 211 | (the default), *obj* and *defect* are passed to :meth:`register_defect`. |
R David Murray | 3edd22a | 2011-04-18 13:59:37 -0400 | [diff] [blame] | 212 | |
R David Murray | c27e522 | 2012-05-25 15:01:48 -0400 | [diff] [blame] | 213 | .. method:: register_defect(obj, defect) |
R David Murray | 3edd22a | 2011-04-18 13:59:37 -0400 | [diff] [blame] | 214 | |
R David Murray | c27e522 | 2012-05-25 15:01:48 -0400 | [diff] [blame] | 215 | Register a *defect* on *obj*. In the email package, *defect* will always |
| 216 | be a subclass of :class:`~email.errors.Defect`. |
R David Murray | 3edd22a | 2011-04-18 13:59:37 -0400 | [diff] [blame] | 217 | |
R David Murray | c27e522 | 2012-05-25 15:01:48 -0400 | [diff] [blame] | 218 | The default implementation calls the ``append`` method of the ``defects`` |
| 219 | attribute of *obj*. When the email package calls :attr:`handle_defect`, |
| 220 | *obj* will normally have a ``defects`` attribute that has an ``append`` |
| 221 | method. Custom object types used with the email package (for example, |
| 222 | custom ``Message`` objects) should also provide such an attribute, |
| 223 | otherwise defects in parsed messages will raise unexpected errors. |
R David Murray | 3edd22a | 2011-04-18 13:59:37 -0400 | [diff] [blame] | 224 | |
R David Murray | abfc374 | 2012-05-29 09:14:44 -0400 | [diff] [blame] | 225 | .. method:: header_max_count(name) |
| 226 | |
| 227 | Return the maximum allowed number of headers named *name*. |
| 228 | |
| 229 | Called when a header is added to a :class:`~email.message.Message` |
| 230 | object. If the returned value is not ``0`` or ``None``, and there are |
| 231 | already a number of headers with the name *name* equal to the value |
| 232 | returned, a :exc:`ValueError` is raised. |
| 233 | |
| 234 | Because the default behavior of ``Message.__setitem__`` is to append the |
| 235 | value to the list of headers, it is easy to create duplicate headers |
| 236 | without realizing it. This method allows certain headers to be limited |
| 237 | in the number of instances of that header that may be added to a |
| 238 | ``Message`` programmatically. (The limit is not observed by the parser, |
| 239 | which will faithfully produce as many headers as exist in the message |
| 240 | being parsed.) |
| 241 | |
| 242 | The default implementation returns ``None`` for all header names. |
| 243 | |
R David Murray | c27e522 | 2012-05-25 15:01:48 -0400 | [diff] [blame] | 244 | .. method:: header_source_parse(sourcelines) |
R David Murray | 3edd22a | 2011-04-18 13:59:37 -0400 | [diff] [blame] | 245 | |
R David Murray | c27e522 | 2012-05-25 15:01:48 -0400 | [diff] [blame] | 246 | The email package calls this method with a list of strings, each string |
| 247 | ending with the line separation characters found in the source being |
| 248 | parsed. The first line includes the field header name and separator. |
| 249 | All whitespace in the source is preserved. The method should return the |
| 250 | ``(name, value)`` tuple that is to be stored in the ``Message`` to |
| 251 | represent the parsed header. |
R David Murray | 3edd22a | 2011-04-18 13:59:37 -0400 | [diff] [blame] | 252 | |
R David Murray | c27e522 | 2012-05-25 15:01:48 -0400 | [diff] [blame] | 253 | If an implementation wishes to retain compatibility with the existing |
| 254 | email package policies, *name* should be the case preserved name (all |
| 255 | characters up to the '``:``' separator), while *value* should be the |
| 256 | unfolded value (all line separator characters removed, but whitespace |
| 257 | kept intact), stripped of leading whitespace. |
R David Murray | 3edd22a | 2011-04-18 13:59:37 -0400 | [diff] [blame] | 258 | |
R David Murray | c27e522 | 2012-05-25 15:01:48 -0400 | [diff] [blame] | 259 | *sourcelines* may contain surrogateescaped binary data. |
| 260 | |
| 261 | There is no default implementation |
| 262 | |
| 263 | .. method:: header_store_parse(name, value) |
| 264 | |
| 265 | The email package calls this method with the name and value provided by |
| 266 | the application program when the application program is modifying a |
| 267 | ``Message`` programmatically (as opposed to a ``Message`` created by a |
| 268 | parser). The method should return the ``(name, value)`` tuple that is to |
| 269 | be stored in the ``Message`` to represent the header. |
| 270 | |
| 271 | If an implementation wishes to retain compatibility with the existing |
| 272 | email package policies, the *name* and *value* should be strings or |
| 273 | string subclasses that do not change the content of the passed in |
| 274 | arguments. |
| 275 | |
| 276 | There is no default implementation |
| 277 | |
| 278 | .. method:: header_fetch_parse(name, value) |
| 279 | |
| 280 | The email package calls this method with the *name* and *value* currently |
| 281 | stored in the ``Message`` when that header is requested by the |
| 282 | application program, and whatever the method returns is what is passed |
| 283 | back to the application as the value of the header being retrieved. |
| 284 | Note that there may be more than one header with the same name stored in |
| 285 | the ``Message``; the method is passed the specific name and value of the |
| 286 | header destined to be returned to the application. |
| 287 | |
| 288 | *value* may contain surrogateescaped binary data. There should be no |
| 289 | surrogateescaped binary data in the value returned by the method. |
| 290 | |
| 291 | There is no default implementation |
| 292 | |
| 293 | .. method:: fold(name, value) |
| 294 | |
| 295 | The email package calls this method with the *name* and *value* currently |
| 296 | stored in the ``Message`` for a given header. The method should return a |
| 297 | string that represents that header "folded" correctly (according to the |
| 298 | policy settings) by composing the *name* with the *value* and inserting |
| 299 | :attr:`linesep` characters at the appropriate places. See :rfc:`5322` |
| 300 | for a discussion of the rules for folding email headers. |
| 301 | |
| 302 | *value* may contain surrogateescaped binary data. There should be no |
| 303 | surrogateescaped binary data in the string returned by the method. |
| 304 | |
| 305 | .. method:: fold_binary(name, value) |
| 306 | |
| 307 | The same as :meth:`fold`, except that the returned value should be a |
| 308 | bytes object rather than a string. |
| 309 | |
| 310 | *value* may contain surrogateescaped binary data. These could be |
| 311 | converted back into binary data in the returned bytes object. |
| 312 | |
| 313 | |
| 314 | .. class:: Compat32(**kw) |
| 315 | |
| 316 | This concrete :class:`Policy` is the backward compatibility policy. It |
| 317 | replicates the behavior of the email package in Python 3.2. The |
Serhiy Storchaka | e0f0cf4 | 2013-08-19 09:59:18 +0300 | [diff] [blame] | 318 | :mod:`~email.policy` module also defines an instance of this class, |
R David Murray | c27e522 | 2012-05-25 15:01:48 -0400 | [diff] [blame] | 319 | :const:`compat32`, that is used as the default policy. Thus the default |
| 320 | behavior of the email package is to maintain compatibility with Python 3.2. |
| 321 | |
| 322 | The class provides the following concrete implementations of the |
| 323 | abstract methods of :class:`Policy`: |
| 324 | |
| 325 | .. method:: header_source_parse(sourcelines) |
| 326 | |
| 327 | The name is parsed as everything up to the '``:``' and returned |
| 328 | unmodified. The value is determined by stripping leading whitespace off |
| 329 | the remainder of the first line, joining all subsequent lines together, |
| 330 | and stripping any trailing carriage return or linefeed characters. |
| 331 | |
| 332 | .. method:: header_store_parse(name, value) |
| 333 | |
| 334 | The name and value are returned unmodified. |
| 335 | |
| 336 | .. method:: header_fetch_parse(name, value) |
| 337 | |
| 338 | If the value contains binary data, it is converted into a |
| 339 | :class:`~email.header.Header` object using the ``unknown-8bit`` charset. |
| 340 | Otherwise it is returned unmodified. |
| 341 | |
| 342 | .. method:: fold(name, value) |
| 343 | |
| 344 | Headers are folded using the :class:`~email.header.Header` folding |
| 345 | algorithm, which preserves existing line breaks in the value, and wraps |
| 346 | each resulting line to the ``max_line_length``. Non-ASCII binary data are |
| 347 | CTE encoded using the ``unknown-8bit`` charset. |
| 348 | |
| 349 | .. method:: fold_binary(name, value) |
| 350 | |
| 351 | Headers are folded using the :class:`~email.header.Header` folding |
| 352 | algorithm, which preserves existing line breaks in the value, and wraps |
| 353 | each resulting line to the ``max_line_length``. If ``cte_type`` is |
| 354 | ``7bit``, non-ascii binary data is CTE encoded using the ``unknown-8bit`` |
| 355 | charset. Otherwise the original source header is used, with its existing |
Terry Jan Reedy | 0f84764 | 2013-03-11 18:34:00 -0400 | [diff] [blame] | 356 | line breaks and any (RFC invalid) binary data it may contain. |
R David Murray | 0b6f6c8 | 2012-05-25 18:42:14 -0400 | [diff] [blame] | 357 | |
| 358 | |
| 359 | .. note:: |
| 360 | |
R David Murray | ea97668 | 2012-05-27 15:03:38 -0400 | [diff] [blame] | 361 | The documentation below describes new policies that are included in the |
| 362 | standard library on a :term:`provisional basis <provisional package>`. |
| 363 | Backwards incompatible changes (up to and including removal of the feature) |
| 364 | may occur if deemed necessary by the core developers. |
R David Murray | 0b6f6c8 | 2012-05-25 18:42:14 -0400 | [diff] [blame] | 365 | |
| 366 | |
| 367 | .. class:: EmailPolicy(**kw) |
| 368 | |
| 369 | This concrete :class:`Policy` provides behavior that is intended to be fully |
| 370 | compliant with the current email RFCs. These include (but are not limited |
| 371 | to) :rfc:`5322`, :rfc:`2047`, and the current MIME RFCs. |
| 372 | |
| 373 | This policy adds new header parsing and folding algorithms. Instead of |
| 374 | simple strings, headers are custom objects with custom attributes depending |
| 375 | on the type of the field. The parsing and folding algorithm fully implement |
| 376 | :rfc:`2047` and :rfc:`5322`. |
| 377 | |
| 378 | In addition to the settable attributes listed above that apply to all |
| 379 | policies, this policy adds the following additional attributes: |
| 380 | |
| 381 | .. attribute:: refold_source |
| 382 | |
| 383 | If the value for a header in the ``Message`` object originated from a |
| 384 | :mod:`~email.parser` (as opposed to being set by a program), this |
| 385 | attribute indicates whether or not a generator should refold that value |
| 386 | when transforming the message back into stream form. The possible values |
| 387 | are: |
| 388 | |
| 389 | ======== =============================================================== |
| 390 | ``none`` all source values use original folding |
| 391 | |
| 392 | ``long`` source values that have any line that is longer than |
| 393 | ``max_line_length`` will be refolded |
| 394 | |
| 395 | ``all`` all values are refolded. |
| 396 | ======== =============================================================== |
| 397 | |
| 398 | The default is ``long``. |
| 399 | |
| 400 | .. attribute:: header_factory |
| 401 | |
| 402 | A callable that takes two arguments, ``name`` and ``value``, where |
| 403 | ``name`` is a header field name and ``value`` is an unfolded header field |
R David Murray | ea97668 | 2012-05-27 15:03:38 -0400 | [diff] [blame] | 404 | value, and returns a string subclass that represents that header. A |
| 405 | default ``header_factory`` (see :mod:`~email.headerregistry`) is provided |
| 406 | that understands some of the :RFC:`5322` header field types. (Currently |
| 407 | address fields and date fields have special treatment, while all other |
| 408 | fields are treated as unstructured. This list will be completed before |
| 409 | the extension is marked stable.) |
R David Murray | 0b6f6c8 | 2012-05-25 18:42:14 -0400 | [diff] [blame] | 410 | |
| 411 | The class provides the following concrete implementations of the abstract |
| 412 | methods of :class:`Policy`: |
| 413 | |
R David Murray | abfc374 | 2012-05-29 09:14:44 -0400 | [diff] [blame] | 414 | .. method:: header_max_count(name) |
| 415 | |
| 416 | Returns the value of the |
| 417 | :attr:`~email.headerregistry.BaseHeader.max_count` attribute of the |
| 418 | specialized class used to represent the header with the given name. |
| 419 | |
R David Murray | 0b6f6c8 | 2012-05-25 18:42:14 -0400 | [diff] [blame] | 420 | .. method:: header_source_parse(sourcelines) |
| 421 | |
| 422 | The implementation of this method is the same as that for the |
| 423 | :class:`Compat32` policy. |
| 424 | |
| 425 | .. method:: header_store_parse(name, value) |
| 426 | |
| 427 | The name is returned unchanged. If the input value has a ``name`` |
| 428 | attribute and it matches *name* ignoring case, the value is returned |
| 429 | unchanged. Otherwise the *name* and *value* are passed to |
| 430 | ``header_factory``, and the resulting custom header object is returned as |
| 431 | the value. In this case a ``ValueError`` is raised if the input value |
| 432 | contains CR or LF characters. |
| 433 | |
| 434 | .. method:: header_fetch_parse(name, value) |
| 435 | |
| 436 | If the value has a ``name`` attribute, it is returned to unmodified. |
| 437 | Otherwise the *name*, and the *value* with any CR or LF characters |
| 438 | removed, are passed to the ``header_factory``, and the resulting custom |
| 439 | header object is returned. Any surrogateescaped bytes get turned into |
| 440 | the unicode unknown-character glyph. |
| 441 | |
| 442 | .. method:: fold(name, value) |
| 443 | |
| 444 | Header folding is controlled by the :attr:`refold_source` policy setting. |
| 445 | A value is considered to be a 'source value' if and only if it does not |
| 446 | have a ``name`` attribute (having a ``name`` attribute means it is a |
| 447 | header object of some sort). If a source value needs to be refolded |
| 448 | according to the policy, it is converted into a custom header object by |
| 449 | passing the *name* and the *value* with any CR and LF characters removed |
| 450 | to the ``header_factory``. Folding of a custom header object is done by |
| 451 | calling its ``fold`` method with the current policy. |
| 452 | |
| 453 | Source values are split into lines using :meth:`~str.splitlines`. If |
| 454 | the value is not to be refolded, the lines are rejoined using the |
| 455 | ``linesep`` from the policy and returned. The exception is lines |
| 456 | containing non-ascii binary data. In that case the value is refolded |
| 457 | regardless of the ``refold_source`` setting, which causes the binary data |
| 458 | to be CTE encoded using the ``unknown-8bit`` charset. |
| 459 | |
| 460 | .. method:: fold_binary(name, value) |
| 461 | |
Serhiy Storchaka | e0f0cf4 | 2013-08-19 09:59:18 +0300 | [diff] [blame] | 462 | The same as :meth:`fold` if :attr:`~Policy.cte_type` is ``7bit``, except |
| 463 | that the returned value is bytes. |
R David Murray | 0b6f6c8 | 2012-05-25 18:42:14 -0400 | [diff] [blame] | 464 | |
Serhiy Storchaka | e0f0cf4 | 2013-08-19 09:59:18 +0300 | [diff] [blame] | 465 | If :attr:`~Policy.cte_type` is ``8bit``, non-ASCII binary data is |
| 466 | converted back |
R David Murray | 0b6f6c8 | 2012-05-25 18:42:14 -0400 | [diff] [blame] | 467 | into bytes. Headers with binary data are not refolded, regardless of the |
| 468 | ``refold_header`` setting, since there is no way to know whether the |
| 469 | binary data consists of single byte characters or multibyte characters. |
| 470 | |
| 471 | The following instances of :class:`EmailPolicy` provide defaults suitable for |
| 472 | specific application domains. Note that in the future the behavior of these |
Georg Brandl | 38e0e1e | 2012-05-27 09:31:10 +0200 | [diff] [blame] | 473 | instances (in particular the ``HTTP`` instance) may be adjusted to conform even |
R David Murray | 0b6f6c8 | 2012-05-25 18:42:14 -0400 | [diff] [blame] | 474 | more closely to the RFCs relevant to their domains. |
| 475 | |
| 476 | .. data:: default |
| 477 | |
| 478 | An instance of ``EmailPolicy`` with all defaults unchanged. This policy |
| 479 | uses the standard Python ``\n`` line endings rather than the RFC-correct |
| 480 | ``\r\n``. |
| 481 | |
| 482 | .. data:: SMTP |
| 483 | |
| 484 | Suitable for serializing messages in conformance with the email RFCs. |
| 485 | Like ``default``, but with ``linesep`` set to ``\r\n``, which is RFC |
| 486 | compliant. |
| 487 | |
| 488 | .. data:: HTTP |
| 489 | |
| 490 | Suitable for serializing headers with for use in HTTP traffic. Like |
| 491 | ``SMTP`` except that ``max_line_length`` is set to ``None`` (unlimited). |
| 492 | |
| 493 | .. data:: strict |
| 494 | |
| 495 | Convenience instance. The same as ``default`` except that |
| 496 | ``raise_on_defect`` is set to ``True``. This allows any policy to be made |
| 497 | strict by writing:: |
| 498 | |
| 499 | somepolicy + policy.strict |
| 500 | |
| 501 | With all of these :class:`EmailPolicies <.EmailPolicy>`, the effective API of |
| 502 | the email package is changed from the Python 3.2 API in the following ways: |
| 503 | |
| 504 | * Setting a header on a :class:`~email.message.Message` results in that |
| 505 | header being parsed and a custom header object created. |
| 506 | |
| 507 | * Fetching a header value from a :class:`~email.message.Message` results |
| 508 | in that header being parsed and a custom header object created and |
| 509 | returned. |
| 510 | |
| 511 | * Any custom header object, or any header that is refolded due to the |
| 512 | policy settings, is folded using an algorithm that fully implements the |
| 513 | RFC folding algorithms, including knowing where encoded words are required |
| 514 | and allowed. |
| 515 | |
| 516 | From the application view, this means that any header obtained through the |
| 517 | :class:`~email.message.Message` is a custom header object with custom |
| 518 | attributes, whose string value is the fully decoded unicode value of the |
| 519 | header. Likewise, a header may be assigned a new value, or a new header |
| 520 | created, using a unicode string, and the policy will take care of converting |
| 521 | the unicode string into the correct RFC encoded form. |
| 522 | |
R David Murray | ea97668 | 2012-05-27 15:03:38 -0400 | [diff] [blame] | 523 | The custom header objects and their attributes are described in |
| 524 | :mod:`~email.headerregistry`. |