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

Class _ValueFormatter

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

Source from the content-addressed store, hash-verified

406
407
408class _ValueFormatter:
409 def __init__(self, headerlen, maxlen, continuation_ws, splitchars):
410 self._maxlen = maxlen
411 self._continuation_ws = continuation_ws
412 self._continuation_ws_len = len(continuation_ws)
413 self._splitchars = splitchars
414 self._lines = []
415 self._current_line = _Accumulator(headerlen)
416
417 def _str(self, linesep):
418 self.newline()
419 return linesep.join(self._lines)
420
421 def __str__(self):
422 return self._str(NL)
423
424 def newline(self):
425 end_of_line = self._current_line.pop()
426 if end_of_line != (' ', ''):
427 self._current_line.push(*end_of_line)
428 if len(self._current_line) > 0:
429 if self._current_line.is_onlyws() and self._lines:
430 self._lines[-1] += str(self._current_line)
431 else:
432 self._lines.append(str(self._current_line))
433 self._current_line.reset()
434
435 def add_transition(self):
436 self._current_line.push(' ', '')
437
438 def feed(self, fws, string, charset):
439 # If the charset has no header encoding (i.e. it is an ASCII encoding)
440 # then we must split the header at the "highest level syntactic break"
441 # possible. Note that we don't have a lot of smarts about field
442 # syntax; we just try to break on semi-colons, then commas, then
443 # whitespace. Eventually, this should be pluggable.
444 if charset.header_encoding is None:
445 self._ascii_split(fws, string, self._splitchars)
446 return
447 # Otherwise, we're doing either a Base64 or a quoted-printable
448 # encoding which means we don't need to split the line on syntactic
449 # breaks. We can basically just find enough characters to fit on the
450 # current line, minus the RFC 2047 chrome. What makes this trickier
451 # though is that we have to split at octet boundaries, not character
452 # boundaries but it's only safe to split at character boundaries so at
453 # best we can only get close.
454 encoded_lines = charset.header_encode_lines(string, self._maxlengths())
455 # The first element extends the current line, but if it's None then
456 # nothing more fit on the current line so start a new line.
457 try:
458 first_line = encoded_lines.pop(0)
459 except IndexError:
460 # There are no encoded lines, so we're done.
461 return
462 if first_line is not None:
463 self._append_chunk(fws, first_line)
464 try:
465 last_line = encoded_lines.pop()

Callers 1

encodeMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected