MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / encode

Method encode

tools/python-3.11.9-amd64/Lib/email/header.py:309–387  ·  view source on GitHub ↗

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

(self, splitchars=';, \t', maxlinelen=None, linesep='\n')

Source from the content-addressed store, hash-verified

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

Callers 15

_foldMethod · 0.95
testFunction · 0.45
quoprimime.pyFile · 0.45
_encodeFunction · 0.45
__init__Method · 0.45
body_encodeMethod · 0.45
_has_surrogatesFunction · 0.45
_sanitizeFunction · 0.45
formataddrFunction · 0.45
__str__Method · 0.45
appendMethod · 0.45
_QByteMapClass · 0.45

Calls 12

_normalizeMethod · 0.95
_nonctextMethod · 0.95
HeaderParseErrorClass · 0.90
_ValueFormatterClass · 0.85
add_transitionMethod · 0.80
splitlinesMethod · 0.80
newlineMethod · 0.80
lstripMethod · 0.80
feedMethod · 0.45
_strMethod · 0.45
searchMethod · 0.45
formatMethod · 0.45

Tested by 2

setUpMethod · 0.36
can_fs_encodeFunction · 0.36