Support converting parsed classes back to mail-like strings #54
Labels
attrs-parser
enhancement
New feature or request therefor
under consideration
Dev has not yet decided whether or how to implement
Milestone
Post #52:
Give
Field
et alii optionalencoder
parameters for specifying how to stringify attribute values when dumping withdump(parsable, fp)
etc. functions.Should this functionality be called "dumping" or "encoding" or something else?
print()
" isn't such a good idea.encoder
s are callables with the following signatures:Field
andMultiField
:(name: str, value: Any) -> Any
ExtraFields
andMultiExtraFields
:(value: Any) -> Sequence[tuple[str, Any]] | Mapping[str, Sequence[Any] | Any]
BodyField
:(value: Any) -> Any
Encoders must return one of the following:
None
— no value will be writtenField
andMultiField
:Sequence[Any]
— will be used as multiple field valuesAny
— will be stringified to be used as the field valueAny
— will be stringifiedSequence[tuple[str, Any]]
Mapping[str, Sequence[Any] | Any]
This will require also adding a
name_encoder
parameter to@parsable
Functions for "dumping":
dump(parseable, fp) -> None
dump_stream(fields: Iterable[Tuple[Optional[str], str]], fp: TextIO) -> None
dump_stanzas_stream(fields: Iterable[Iterable[Tuple[str, str]]], fp: TextIO) -> None
dumps*()
functions that return stringsGive the "dumping" functions keyword options for the following:
separator
indent
)auto_indent: bool = False
(Rethink name) — whenTrue
, field values in which all lines after the first are already indented (i.e., folded) are not indented againThe string-returning dump functions should be the "core" ones that the others are implemented in terms of, as we don't want to write anything to a file until we're sure that all the return values of the decoders are valid.
Line wrapping fields is the caller's job (but maybe add a helper function for that?).
None
(after serializing/encoding) field values are always skipped when dumping; if the user doesn't want that, they need to set a dumper that serializesNone
s to something else.Fields with aliases are dumped using the decoded aliases.
The text was updated successfully, but these errors were encountered: