r"""Encode a message header into an RFC-compliant format. There are many issues involved in converting a given string for use in an email header. Only certain character sets are readable in most email clients, and as header strings can only contain a subset of 7-bit
(self, splitchars=';, \t', maxlinelen=None, linesep='\n')
| 316 | return s.isspace() or s in ('(', ')', '\\') |
| 317 | |
| 318 | def encode(self, splitchars=';, \t', maxlinelen=None, linesep='\n'): |
| 319 | r"""Encode a message header into an RFC-compliant format. |
| 320 | |
| 321 | There are many issues involved in converting a given string for use in |
| 322 | an email header. Only certain character sets are readable in most |
| 323 | email clients, and as header strings can only contain a subset of |
| 324 | 7-bit ASCII, care must be taken to properly convert and encode (with |
| 325 | Base64 or quoted-printable) header strings. In addition, there is a |
| 326 | 75-character length limit on any given encoded header field, so |
| 327 | line-wrapping must be performed, even with double-byte character sets. |
| 328 | |
| 329 | Optional maxlinelen specifies the maximum length of each generated |
| 330 | line, exclusive of the linesep string. Individual lines may be longer |
| 331 | than maxlinelen if a folding point cannot be found. The first line |
| 332 | will be shorter by the length of the header name plus ": " if a header |
| 333 | name was specified at Header construction time. The default value for |
| 334 | maxlinelen is determined at header construction time. |
| 335 | |
| 336 | Optional splitchars is a string containing characters which should be |
| 337 | given extra weight by the splitting algorithm during normal header |
| 338 | wrapping. This is in very rough support of RFC 2822's 'higher level |
| 339 | syntactic breaks': split points preceded by a splitchar are preferred |
| 340 | during line splitting, with the characters preferred in the order in |
| 341 | which they appear in the string. Space and tab may be included in the |
| 342 | string to indicate whether preference should be given to one over the |
| 343 | other as a split point when other split chars do not appear in the line |
| 344 | being split. Splitchars does not affect RFC 2047 encoded lines. |
| 345 | |
| 346 | Optional linesep is a string to be used to separate the lines of |
| 347 | the value. The default value is the most useful for typical |
| 348 | Python applications, but it can be set to \r\n to produce RFC-compliant |
| 349 | line separators when needed. |
| 350 | """ |
| 351 | self._normalize() |
| 352 | if maxlinelen is None: |
| 353 | maxlinelen = self._maxlinelen |
| 354 | # A maxlinelen of 0 means don't wrap. For all practical purposes, |
| 355 | # choosing a huge number here accomplishes that and makes the |
| 356 | # _ValueFormatter algorithm much simpler. |
| 357 | if maxlinelen == 0: |
| 358 | maxlinelen = 1000000 |
| 359 | formatter = _ValueFormatter(self._headerlen, maxlinelen, |
| 360 | self._continuation_ws, splitchars) |
| 361 | lastcs = None |
| 362 | hasspace = lastspace = None |
| 363 | for string, charset in self._chunks: |
| 364 | if hasspace is not None: |
| 365 | hasspace = string and self._nonctext(string[0]) |
| 366 | if lastcs not in (None, 'us-ascii'): |
| 367 | if not hasspace or charset not in (None, 'us-ascii'): |
| 368 | formatter.add_transition() |
| 369 | elif charset not in (None, 'us-ascii') and not lastspace: |
| 370 | formatter.add_transition() |
| 371 | lastspace = string and self._nonctext(string[-1]) |
| 372 | lastcs = charset |
| 373 | hasspace = False |
| 374 | lines = string.splitlines() |
| 375 | if lines: |