Parse metadata from email headers. If *validate* is true, the metadata will be validated. All exceptions related to validation will be gathered and raised as an :class:`ExceptionGroup`.
(
cls, data: Union[bytes, str], *, validate: bool = True
)
| 705 | |
| 706 | @classmethod |
| 707 | def from_email( |
| 708 | cls, data: Union[bytes, str], *, validate: bool = True |
| 709 | ) -> "Metadata": |
| 710 | """Parse metadata from email headers. |
| 711 | |
| 712 | If *validate* is true, the metadata will be validated. All exceptions |
| 713 | related to validation will be gathered and raised as an :class:`ExceptionGroup`. |
| 714 | """ |
| 715 | raw, unparsed = parse_email(data) |
| 716 | |
| 717 | if validate: |
| 718 | exceptions: list[Exception] = [] |
| 719 | for unparsed_key in unparsed: |
| 720 | if unparsed_key in _EMAIL_TO_RAW_MAPPING: |
| 721 | message = f"{unparsed_key!r} has invalid data" |
| 722 | else: |
| 723 | message = f"unrecognized field: {unparsed_key!r}" |
| 724 | exceptions.append(InvalidMetadata(unparsed_key, message)) |
| 725 | |
| 726 | if exceptions: |
| 727 | raise ExceptionGroup("unparsed", exceptions) |
| 728 | |
| 729 | try: |
| 730 | return cls.from_raw(raw, validate=validate) |
| 731 | except ExceptionGroup as exc_group: |
| 732 | raise ExceptionGroup( |
| 733 | "invalid or unparsed metadata", exc_group.exceptions |
| 734 | ) from None |
| 735 | |
| 736 | metadata_version: _Validator[_MetadataVersion] = _Validator() |
| 737 | """:external:ref:`core-metadata-metadata-version` |
nothing calls this directly
no test coverage detected