metadata.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. import email.feedparser
  2. import email.header
  3. import email.message
  4. import email.parser
  5. import email.policy
  6. import sys
  7. import typing
  8. from typing import (
  9. Any,
  10. Callable,
  11. Dict,
  12. Generic,
  13. List,
  14. Optional,
  15. Tuple,
  16. Type,
  17. Union,
  18. cast,
  19. )
  20. from . import requirements, specifiers, utils, version as version_module
  21. T = typing.TypeVar("T")
  22. if sys.version_info[:2] >= (3, 8): # pragma: no cover
  23. from typing import Literal, TypedDict
  24. else: # pragma: no cover
  25. if typing.TYPE_CHECKING:
  26. from typing_extensions import Literal, TypedDict
  27. else:
  28. try:
  29. from typing_extensions import Literal, TypedDict
  30. except ImportError:
  31. class Literal:
  32. def __init_subclass__(*_args, **_kwargs):
  33. pass
  34. class TypedDict:
  35. def __init_subclass__(*_args, **_kwargs):
  36. pass
  37. try:
  38. ExceptionGroup
  39. except NameError: # pragma: no cover
  40. class ExceptionGroup(Exception): # noqa: N818
  41. """A minimal implementation of :external:exc:`ExceptionGroup` from Python 3.11.
  42. If :external:exc:`ExceptionGroup` is already defined by Python itself,
  43. that version is used instead.
  44. """
  45. message: str
  46. exceptions: List[Exception]
  47. def __init__(self, message: str, exceptions: List[Exception]) -> None:
  48. self.message = message
  49. self.exceptions = exceptions
  50. def __repr__(self) -> str:
  51. return f"{self.__class__.__name__}({self.message!r}, {self.exceptions!r})"
  52. else: # pragma: no cover
  53. ExceptionGroup = ExceptionGroup
  54. class InvalidMetadata(ValueError):
  55. """A metadata field contains invalid data."""
  56. field: str
  57. """The name of the field that contains invalid data."""
  58. def __init__(self, field: str, message: str) -> None:
  59. self.field = field
  60. super().__init__(message)
  61. # The RawMetadata class attempts to make as few assumptions about the underlying
  62. # serialization formats as possible. The idea is that as long as a serialization
  63. # formats offer some very basic primitives in *some* way then we can support
  64. # serializing to and from that format.
  65. class RawMetadata(TypedDict, total=False):
  66. """A dictionary of raw core metadata.
  67. Each field in core metadata maps to a key of this dictionary (when data is
  68. provided). The key is lower-case and underscores are used instead of dashes
  69. compared to the equivalent core metadata field. Any core metadata field that
  70. can be specified multiple times or can hold multiple values in a single
  71. field have a key with a plural name. See :class:`Metadata` whose attributes
  72. match the keys of this dictionary.
  73. Core metadata fields that can be specified multiple times are stored as a
  74. list or dict depending on which is appropriate for the field. Any fields
  75. which hold multiple values in a single field are stored as a list.
  76. """
  77. # Metadata 1.0 - PEP 241
  78. metadata_version: str
  79. name: str
  80. version: str
  81. platforms: List[str]
  82. summary: str
  83. description: str
  84. keywords: List[str]
  85. home_page: str
  86. author: str
  87. author_email: str
  88. license: str
  89. # Metadata 1.1 - PEP 314
  90. supported_platforms: List[str]
  91. download_url: str
  92. classifiers: List[str]
  93. requires: List[str]
  94. provides: List[str]
  95. obsoletes: List[str]
  96. # Metadata 1.2 - PEP 345
  97. maintainer: str
  98. maintainer_email: str
  99. requires_dist: List[str]
  100. provides_dist: List[str]
  101. obsoletes_dist: List[str]
  102. requires_python: str
  103. requires_external: List[str]
  104. project_urls: Dict[str, str]
  105. # Metadata 2.0
  106. # PEP 426 attempted to completely revamp the metadata format
  107. # but got stuck without ever being able to build consensus on
  108. # it and ultimately ended up withdrawn.
  109. #
  110. # However, a number of tools had started emitting METADATA with
  111. # `2.0` Metadata-Version, so for historical reasons, this version
  112. # was skipped.
  113. # Metadata 2.1 - PEP 566
  114. description_content_type: str
  115. provides_extra: List[str]
  116. # Metadata 2.2 - PEP 643
  117. dynamic: List[str]
  118. # Metadata 2.3 - PEP 685
  119. # No new fields were added in PEP 685, just some edge case were
  120. # tightened up to provide better interoptability.
  121. _STRING_FIELDS = {
  122. "author",
  123. "author_email",
  124. "description",
  125. "description_content_type",
  126. "download_url",
  127. "home_page",
  128. "license",
  129. "maintainer",
  130. "maintainer_email",
  131. "metadata_version",
  132. "name",
  133. "requires_python",
  134. "summary",
  135. "version",
  136. }
  137. _LIST_FIELDS = {
  138. "classifiers",
  139. "dynamic",
  140. "obsoletes",
  141. "obsoletes_dist",
  142. "platforms",
  143. "provides",
  144. "provides_dist",
  145. "provides_extra",
  146. "requires",
  147. "requires_dist",
  148. "requires_external",
  149. "supported_platforms",
  150. }
  151. _DICT_FIELDS = {
  152. "project_urls",
  153. }
  154. def _parse_keywords(data: str) -> List[str]:
  155. """Split a string of comma-separate keyboards into a list of keywords."""
  156. return [k.strip() for k in data.split(",")]
  157. def _parse_project_urls(data: List[str]) -> Dict[str, str]:
  158. """Parse a list of label/URL string pairings separated by a comma."""
  159. urls = {}
  160. for pair in data:
  161. # Our logic is slightly tricky here as we want to try and do
  162. # *something* reasonable with malformed data.
  163. #
  164. # The main thing that we have to worry about, is data that does
  165. # not have a ',' at all to split the label from the Value. There
  166. # isn't a singular right answer here, and we will fail validation
  167. # later on (if the caller is validating) so it doesn't *really*
  168. # matter, but since the missing value has to be an empty str
  169. # and our return value is dict[str, str], if we let the key
  170. # be the missing value, then they'd have multiple '' values that
  171. # overwrite each other in a accumulating dict.
  172. #
  173. # The other potentional issue is that it's possible to have the
  174. # same label multiple times in the metadata, with no solid "right"
  175. # answer with what to do in that case. As such, we'll do the only
  176. # thing we can, which is treat the field as unparseable and add it
  177. # to our list of unparsed fields.
  178. parts = [p.strip() for p in pair.split(",", 1)]
  179. parts.extend([""] * (max(0, 2 - len(parts)))) # Ensure 2 items
  180. # TODO: The spec doesn't say anything about if the keys should be
  181. # considered case sensitive or not... logically they should
  182. # be case-preserving and case-insensitive, but doing that
  183. # would open up more cases where we might have duplicate
  184. # entries.
  185. label, url = parts
  186. if label in urls:
  187. # The label already exists in our set of urls, so this field
  188. # is unparseable, and we can just add the whole thing to our
  189. # unparseable data and stop processing it.
  190. raise KeyError("duplicate labels in project urls")
  191. urls[label] = url
  192. return urls
  193. def _get_payload(msg: email.message.Message, source: Union[bytes, str]) -> str:
  194. """Get the body of the message."""
  195. # If our source is a str, then our caller has managed encodings for us,
  196. # and we don't need to deal with it.
  197. if isinstance(source, str):
  198. payload: str = msg.get_payload()
  199. return payload
  200. # If our source is a bytes, then we're managing the encoding and we need
  201. # to deal with it.
  202. else:
  203. bpayload: bytes = msg.get_payload(decode=True)
  204. try:
  205. return bpayload.decode("utf8", "strict")
  206. except UnicodeDecodeError:
  207. raise ValueError("payload in an invalid encoding")
  208. # The various parse_FORMAT functions here are intended to be as lenient as
  209. # possible in their parsing, while still returning a correctly typed
  210. # RawMetadata.
  211. #
  212. # To aid in this, we also generally want to do as little touching of the
  213. # data as possible, except where there are possibly some historic holdovers
  214. # that make valid data awkward to work with.
  215. #
  216. # While this is a lower level, intermediate format than our ``Metadata``
  217. # class, some light touch ups can make a massive difference in usability.
  218. # Map METADATA fields to RawMetadata.
  219. _EMAIL_TO_RAW_MAPPING = {
  220. "author": "author",
  221. "author-email": "author_email",
  222. "classifier": "classifiers",
  223. "description": "description",
  224. "description-content-type": "description_content_type",
  225. "download-url": "download_url",
  226. "dynamic": "dynamic",
  227. "home-page": "home_page",
  228. "keywords": "keywords",
  229. "license": "license",
  230. "maintainer": "maintainer",
  231. "maintainer-email": "maintainer_email",
  232. "metadata-version": "metadata_version",
  233. "name": "name",
  234. "obsoletes": "obsoletes",
  235. "obsoletes-dist": "obsoletes_dist",
  236. "platform": "platforms",
  237. "project-url": "project_urls",
  238. "provides": "provides",
  239. "provides-dist": "provides_dist",
  240. "provides-extra": "provides_extra",
  241. "requires": "requires",
  242. "requires-dist": "requires_dist",
  243. "requires-external": "requires_external",
  244. "requires-python": "requires_python",
  245. "summary": "summary",
  246. "supported-platform": "supported_platforms",
  247. "version": "version",
  248. }
  249. _RAW_TO_EMAIL_MAPPING = {raw: email for email, raw in _EMAIL_TO_RAW_MAPPING.items()}
  250. def parse_email(data: Union[bytes, str]) -> Tuple[RawMetadata, Dict[str, List[str]]]:
  251. """Parse a distribution's metadata stored as email headers (e.g. from ``METADATA``).
  252. This function returns a two-item tuple of dicts. The first dict is of
  253. recognized fields from the core metadata specification. Fields that can be
  254. parsed and translated into Python's built-in types are converted
  255. appropriately. All other fields are left as-is. Fields that are allowed to
  256. appear multiple times are stored as lists.
  257. The second dict contains all other fields from the metadata. This includes
  258. any unrecognized fields. It also includes any fields which are expected to
  259. be parsed into a built-in type but were not formatted appropriately. Finally,
  260. any fields that are expected to appear only once but are repeated are
  261. included in this dict.
  262. """
  263. raw: Dict[str, Union[str, List[str], Dict[str, str]]] = {}
  264. unparsed: Dict[str, List[str]] = {}
  265. if isinstance(data, str):
  266. parsed = email.parser.Parser(policy=email.policy.compat32).parsestr(data)
  267. else:
  268. parsed = email.parser.BytesParser(policy=email.policy.compat32).parsebytes(data)
  269. # We have to wrap parsed.keys() in a set, because in the case of multiple
  270. # values for a key (a list), the key will appear multiple times in the
  271. # list of keys, but we're avoiding that by using get_all().
  272. for name in frozenset(parsed.keys()):
  273. # Header names in RFC are case insensitive, so we'll normalize to all
  274. # lower case to make comparisons easier.
  275. name = name.lower()
  276. # We use get_all() here, even for fields that aren't multiple use,
  277. # because otherwise someone could have e.g. two Name fields, and we
  278. # would just silently ignore it rather than doing something about it.
  279. headers = parsed.get_all(name) or []
  280. # The way the email module works when parsing bytes is that it
  281. # unconditionally decodes the bytes as ascii using the surrogateescape
  282. # handler. When you pull that data back out (such as with get_all() ),
  283. # it looks to see if the str has any surrogate escapes, and if it does
  284. # it wraps it in a Header object instead of returning the string.
  285. #
  286. # As such, we'll look for those Header objects, and fix up the encoding.
  287. value = []
  288. # Flag if we have run into any issues processing the headers, thus
  289. # signalling that the data belongs in 'unparsed'.
  290. valid_encoding = True
  291. for h in headers:
  292. # It's unclear if this can return more types than just a Header or
  293. # a str, so we'll just assert here to make sure.
  294. assert isinstance(h, (email.header.Header, str))
  295. # If it's a header object, we need to do our little dance to get
  296. # the real data out of it. In cases where there is invalid data
  297. # we're going to end up with mojibake, but there's no obvious, good
  298. # way around that without reimplementing parts of the Header object
  299. # ourselves.
  300. #
  301. # That should be fine since, if mojibacked happens, this key is
  302. # going into the unparsed dict anyways.
  303. if isinstance(h, email.header.Header):
  304. # The Header object stores it's data as chunks, and each chunk
  305. # can be independently encoded, so we'll need to check each
  306. # of them.
  307. chunks: List[Tuple[bytes, Optional[str]]] = []
  308. for bin, encoding in email.header.decode_header(h):
  309. try:
  310. bin.decode("utf8", "strict")
  311. except UnicodeDecodeError:
  312. # Enable mojibake.
  313. encoding = "latin1"
  314. valid_encoding = False
  315. else:
  316. encoding = "utf8"
  317. chunks.append((bin, encoding))
  318. # Turn our chunks back into a Header object, then let that
  319. # Header object do the right thing to turn them into a
  320. # string for us.
  321. value.append(str(email.header.make_header(chunks)))
  322. # This is already a string, so just add it.
  323. else:
  324. value.append(h)
  325. # We've processed all of our values to get them into a list of str,
  326. # but we may have mojibake data, in which case this is an unparsed
  327. # field.
  328. if not valid_encoding:
  329. unparsed[name] = value
  330. continue
  331. raw_name = _EMAIL_TO_RAW_MAPPING.get(name)
  332. if raw_name is None:
  333. # This is a bit of a weird situation, we've encountered a key that
  334. # we don't know what it means, so we don't know whether it's meant
  335. # to be a list or not.
  336. #
  337. # Since we can't really tell one way or another, we'll just leave it
  338. # as a list, even though it may be a single item list, because that's
  339. # what makes the most sense for email headers.
  340. unparsed[name] = value
  341. continue
  342. # If this is one of our string fields, then we'll check to see if our
  343. # value is a list of a single item. If it is then we'll assume that
  344. # it was emitted as a single string, and unwrap the str from inside
  345. # the list.
  346. #
  347. # If it's any other kind of data, then we haven't the faintest clue
  348. # what we should parse it as, and we have to just add it to our list
  349. # of unparsed stuff.
  350. if raw_name in _STRING_FIELDS and len(value) == 1:
  351. raw[raw_name] = value[0]
  352. # If this is one of our list of string fields, then we can just assign
  353. # the value, since email *only* has strings, and our get_all() call
  354. # above ensures that this is a list.
  355. elif raw_name in _LIST_FIELDS:
  356. raw[raw_name] = value
  357. # Special Case: Keywords
  358. # The keywords field is implemented in the metadata spec as a str,
  359. # but it conceptually is a list of strings, and is serialized using
  360. # ", ".join(keywords), so we'll do some light data massaging to turn
  361. # this into what it logically is.
  362. elif raw_name == "keywords" and len(value) == 1:
  363. raw[raw_name] = _parse_keywords(value[0])
  364. # Special Case: Project-URL
  365. # The project urls is implemented in the metadata spec as a list of
  366. # specially-formatted strings that represent a key and a value, which
  367. # is fundamentally a mapping, however the email format doesn't support
  368. # mappings in a sane way, so it was crammed into a list of strings
  369. # instead.
  370. #
  371. # We will do a little light data massaging to turn this into a map as
  372. # it logically should be.
  373. elif raw_name == "project_urls":
  374. try:
  375. raw[raw_name] = _parse_project_urls(value)
  376. except KeyError:
  377. unparsed[name] = value
  378. # Nothing that we've done has managed to parse this, so it'll just
  379. # throw it in our unparseable data and move on.
  380. else:
  381. unparsed[name] = value
  382. # We need to support getting the Description from the message payload in
  383. # addition to getting it from the the headers. This does mean, though, there
  384. # is the possibility of it being set both ways, in which case we put both
  385. # in 'unparsed' since we don't know which is right.
  386. try:
  387. payload = _get_payload(parsed, data)
  388. except ValueError:
  389. unparsed.setdefault("description", []).append(
  390. parsed.get_payload(decode=isinstance(data, bytes))
  391. )
  392. else:
  393. if payload:
  394. # Check to see if we've already got a description, if so then both
  395. # it, and this body move to unparseable.
  396. if "description" in raw:
  397. description_header = cast(str, raw.pop("description"))
  398. unparsed.setdefault("description", []).extend(
  399. [description_header, payload]
  400. )
  401. elif "description" in unparsed:
  402. unparsed["description"].append(payload)
  403. else:
  404. raw["description"] = payload
  405. # We need to cast our `raw` to a metadata, because a TypedDict only support
  406. # literal key names, but we're computing our key names on purpose, but the
  407. # way this function is implemented, our `TypedDict` can only have valid key
  408. # names.
  409. return cast(RawMetadata, raw), unparsed
  410. _NOT_FOUND = object()
  411. # Keep the two values in sync.
  412. _VALID_METADATA_VERSIONS = ["1.0", "1.1", "1.2", "2.1", "2.2", "2.3"]
  413. _MetadataVersion = Literal["1.0", "1.1", "1.2", "2.1", "2.2", "2.3"]
  414. _REQUIRED_ATTRS = frozenset(["metadata_version", "name", "version"])
  415. class _Validator(Generic[T]):
  416. """Validate a metadata field.
  417. All _process_*() methods correspond to a core metadata field. The method is
  418. called with the field's raw value. If the raw value is valid it is returned
  419. in its "enriched" form (e.g. ``version.Version`` for the ``Version`` field).
  420. If the raw value is invalid, :exc:`InvalidMetadata` is raised (with a cause
  421. as appropriate).
  422. """
  423. name: str
  424. raw_name: str
  425. added: _MetadataVersion
  426. def __init__(
  427. self,
  428. *,
  429. added: _MetadataVersion = "1.0",
  430. ) -> None:
  431. self.added = added
  432. def __set_name__(self, _owner: "Metadata", name: str) -> None:
  433. self.name = name
  434. self.raw_name = _RAW_TO_EMAIL_MAPPING[name]
  435. def __get__(self, instance: "Metadata", _owner: Type["Metadata"]) -> T:
  436. # With Python 3.8, the caching can be replaced with functools.cached_property().
  437. # No need to check the cache as attribute lookup will resolve into the
  438. # instance's __dict__ before __get__ is called.
  439. cache = instance.__dict__
  440. value = instance._raw.get(self.name)
  441. # To make the _process_* methods easier, we'll check if the value is None
  442. # and if this field is NOT a required attribute, and if both of those
  443. # things are true, we'll skip the the converter. This will mean that the
  444. # converters never have to deal with the None union.
  445. if self.name in _REQUIRED_ATTRS or value is not None:
  446. try:
  447. converter: Callable[[Any], T] = getattr(self, f"_process_{self.name}")
  448. except AttributeError:
  449. pass
  450. else:
  451. value = converter(value)
  452. cache[self.name] = value
  453. try:
  454. del instance._raw[self.name] # type: ignore[misc]
  455. except KeyError:
  456. pass
  457. return cast(T, value)
  458. def _invalid_metadata(
  459. self, msg: str, cause: Optional[Exception] = None
  460. ) -> InvalidMetadata:
  461. exc = InvalidMetadata(
  462. self.raw_name, msg.format_map({"field": repr(self.raw_name)})
  463. )
  464. exc.__cause__ = cause
  465. return exc
  466. def _process_metadata_version(self, value: str) -> _MetadataVersion:
  467. # Implicitly makes Metadata-Version required.
  468. if value not in _VALID_METADATA_VERSIONS:
  469. raise self._invalid_metadata(f"{value!r} is not a valid metadata version")
  470. return cast(_MetadataVersion, value)
  471. def _process_name(self, value: str) -> str:
  472. if not value:
  473. raise self._invalid_metadata("{field} is a required field")
  474. # Validate the name as a side-effect.
  475. try:
  476. utils.canonicalize_name(value, validate=True)
  477. except utils.InvalidName as exc:
  478. raise self._invalid_metadata(
  479. f"{value!r} is invalid for {{field}}", cause=exc
  480. )
  481. else:
  482. return value
  483. def _process_version(self, value: str) -> version_module.Version:
  484. if not value:
  485. raise self._invalid_metadata("{field} is a required field")
  486. try:
  487. return version_module.parse(value)
  488. except version_module.InvalidVersion as exc:
  489. raise self._invalid_metadata(
  490. f"{value!r} is invalid for {{field}}", cause=exc
  491. )
  492. def _process_summary(self, value: str) -> str:
  493. """Check the field contains no newlines."""
  494. if "\n" in value:
  495. raise self._invalid_metadata("{field} must be a single line")
  496. return value
  497. def _process_description_content_type(self, value: str) -> str:
  498. content_types = {"text/plain", "text/x-rst", "text/markdown"}
  499. message = email.message.EmailMessage()
  500. message["content-type"] = value
  501. content_type, parameters = (
  502. # Defaults to `text/plain` if parsing failed.
  503. message.get_content_type().lower(),
  504. message["content-type"].params,
  505. )
  506. # Check if content-type is valid or defaulted to `text/plain` and thus was
  507. # not parseable.
  508. if content_type not in content_types or content_type not in value.lower():
  509. raise self._invalid_metadata(
  510. f"{{field}} must be one of {list(content_types)}, not {value!r}"
  511. )
  512. charset = parameters.get("charset", "UTF-8")
  513. if charset != "UTF-8":
  514. raise self._invalid_metadata(
  515. f"{{field}} can only specify the UTF-8 charset, not {list(charset)}"
  516. )
  517. markdown_variants = {"GFM", "CommonMark"}
  518. variant = parameters.get("variant", "GFM") # Use an acceptable default.
  519. if content_type == "text/markdown" and variant not in markdown_variants:
  520. raise self._invalid_metadata(
  521. f"valid Markdown variants for {{field}} are {list(markdown_variants)}, "
  522. f"not {variant!r}",
  523. )
  524. return value
  525. def _process_dynamic(self, value: List[str]) -> List[str]:
  526. for dynamic_field in map(str.lower, value):
  527. if dynamic_field in {"name", "version", "metadata-version"}:
  528. raise self._invalid_metadata(
  529. f"{value!r} is not allowed as a dynamic field"
  530. )
  531. elif dynamic_field not in _EMAIL_TO_RAW_MAPPING:
  532. raise self._invalid_metadata(f"{value!r} is not a valid dynamic field")
  533. return list(map(str.lower, value))
  534. def _process_provides_extra(
  535. self,
  536. value: List[str],
  537. ) -> List[utils.NormalizedName]:
  538. normalized_names = []
  539. try:
  540. for name in value:
  541. normalized_names.append(utils.canonicalize_name(name, validate=True))
  542. except utils.InvalidName as exc:
  543. raise self._invalid_metadata(
  544. f"{name!r} is invalid for {{field}}", cause=exc
  545. )
  546. else:
  547. return normalized_names
  548. def _process_requires_python(self, value: str) -> specifiers.SpecifierSet:
  549. try:
  550. return specifiers.SpecifierSet(value)
  551. except specifiers.InvalidSpecifier as exc:
  552. raise self._invalid_metadata(
  553. f"{value!r} is invalid for {{field}}", cause=exc
  554. )
  555. def _process_requires_dist(
  556. self,
  557. value: List[str],
  558. ) -> List[requirements.Requirement]:
  559. reqs = []
  560. try:
  561. for req in value:
  562. reqs.append(requirements.Requirement(req))
  563. except requirements.InvalidRequirement as exc:
  564. raise self._invalid_metadata(f"{req!r} is invalid for {{field}}", cause=exc)
  565. else:
  566. return reqs
  567. class Metadata:
  568. """Representation of distribution metadata.
  569. Compared to :class:`RawMetadata`, this class provides objects representing
  570. metadata fields instead of only using built-in types. Any invalid metadata
  571. will cause :exc:`InvalidMetadata` to be raised (with a
  572. :py:attr:`~BaseException.__cause__` attribute as appropriate).
  573. """
  574. _raw: RawMetadata
  575. @classmethod
  576. def from_raw(cls, data: RawMetadata, *, validate: bool = True) -> "Metadata":
  577. """Create an instance from :class:`RawMetadata`.
  578. If *validate* is true, all metadata will be validated. All exceptions
  579. related to validation will be gathered and raised as an :class:`ExceptionGroup`.
  580. """
  581. ins = cls()
  582. ins._raw = data.copy() # Mutations occur due to caching enriched values.
  583. if validate:
  584. exceptions: List[Exception] = []
  585. try:
  586. metadata_version = ins.metadata_version
  587. metadata_age = _VALID_METADATA_VERSIONS.index(metadata_version)
  588. except InvalidMetadata as metadata_version_exc:
  589. exceptions.append(metadata_version_exc)
  590. metadata_version = None
  591. # Make sure to check for the fields that are present, the required
  592. # fields (so their absence can be reported).
  593. fields_to_check = frozenset(ins._raw) | _REQUIRED_ATTRS
  594. # Remove fields that have already been checked.
  595. fields_to_check -= {"metadata_version"}
  596. for key in fields_to_check:
  597. try:
  598. if metadata_version:
  599. # Can't use getattr() as that triggers descriptor protocol which
  600. # will fail due to no value for the instance argument.
  601. try:
  602. field_metadata_version = cls.__dict__[key].added
  603. except KeyError:
  604. exc = InvalidMetadata(key, f"unrecognized field: {key!r}")
  605. exceptions.append(exc)
  606. continue
  607. field_age = _VALID_METADATA_VERSIONS.index(
  608. field_metadata_version
  609. )
  610. if field_age > metadata_age:
  611. field = _RAW_TO_EMAIL_MAPPING[key]
  612. exc = InvalidMetadata(
  613. field,
  614. "{field} introduced in metadata version "
  615. "{field_metadata_version}, not {metadata_version}",
  616. )
  617. exceptions.append(exc)
  618. continue
  619. getattr(ins, key)
  620. except InvalidMetadata as exc:
  621. exceptions.append(exc)
  622. if exceptions:
  623. raise ExceptionGroup("invalid metadata", exceptions)
  624. return ins
  625. @classmethod
  626. def from_email(
  627. cls, data: Union[bytes, str], *, validate: bool = True
  628. ) -> "Metadata":
  629. """Parse metadata from email headers.
  630. If *validate* is true, the metadata will be validated. All exceptions
  631. related to validation will be gathered and raised as an :class:`ExceptionGroup`.
  632. """
  633. raw, unparsed = parse_email(data)
  634. if validate:
  635. exceptions: list[Exception] = []
  636. for unparsed_key in unparsed:
  637. if unparsed_key in _EMAIL_TO_RAW_MAPPING:
  638. message = f"{unparsed_key!r} has invalid data"
  639. else:
  640. message = f"unrecognized field: {unparsed_key!r}"
  641. exceptions.append(InvalidMetadata(unparsed_key, message))
  642. if exceptions:
  643. raise ExceptionGroup("unparsed", exceptions)
  644. try:
  645. return cls.from_raw(raw, validate=validate)
  646. except ExceptionGroup as exc_group:
  647. raise ExceptionGroup(
  648. "invalid or unparsed metadata", exc_group.exceptions
  649. ) from None
  650. metadata_version: _Validator[_MetadataVersion] = _Validator()
  651. """:external:ref:`core-metadata-metadata-version`
  652. (required; validated to be a valid metadata version)"""
  653. name: _Validator[str] = _Validator()
  654. """:external:ref:`core-metadata-name`
  655. (required; validated using :func:`~packaging.utils.canonicalize_name` and its
  656. *validate* parameter)"""
  657. version: _Validator[version_module.Version] = _Validator()
  658. """:external:ref:`core-metadata-version` (required)"""
  659. dynamic: _Validator[Optional[List[str]]] = _Validator(
  660. added="2.2",
  661. )
  662. """:external:ref:`core-metadata-dynamic`
  663. (validated against core metadata field names and lowercased)"""
  664. platforms: _Validator[Optional[List[str]]] = _Validator()
  665. """:external:ref:`core-metadata-platform`"""
  666. supported_platforms: _Validator[Optional[List[str]]] = _Validator(added="1.1")
  667. """:external:ref:`core-metadata-supported-platform`"""
  668. summary: _Validator[Optional[str]] = _Validator()
  669. """:external:ref:`core-metadata-summary` (validated to contain no newlines)"""
  670. description: _Validator[Optional[str]] = _Validator() # TODO 2.1: can be in body
  671. """:external:ref:`core-metadata-description`"""
  672. description_content_type: _Validator[Optional[str]] = _Validator(added="2.1")
  673. """:external:ref:`core-metadata-description-content-type` (validated)"""
  674. keywords: _Validator[Optional[List[str]]] = _Validator()
  675. """:external:ref:`core-metadata-keywords`"""
  676. home_page: _Validator[Optional[str]] = _Validator()
  677. """:external:ref:`core-metadata-home-page`"""
  678. download_url: _Validator[Optional[str]] = _Validator(added="1.1")
  679. """:external:ref:`core-metadata-download-url`"""
  680. author: _Validator[Optional[str]] = _Validator()
  681. """:external:ref:`core-metadata-author`"""
  682. author_email: _Validator[Optional[str]] = _Validator()
  683. """:external:ref:`core-metadata-author-email`"""
  684. maintainer: _Validator[Optional[str]] = _Validator(added="1.2")
  685. """:external:ref:`core-metadata-maintainer`"""
  686. maintainer_email: _Validator[Optional[str]] = _Validator(added="1.2")
  687. """:external:ref:`core-metadata-maintainer-email`"""
  688. license: _Validator[Optional[str]] = _Validator()
  689. """:external:ref:`core-metadata-license`"""
  690. classifiers: _Validator[Optional[List[str]]] = _Validator(added="1.1")
  691. """:external:ref:`core-metadata-classifier`"""
  692. requires_dist: _Validator[Optional[List[requirements.Requirement]]] = _Validator(
  693. added="1.2"
  694. )
  695. """:external:ref:`core-metadata-requires-dist`"""
  696. requires_python: _Validator[Optional[specifiers.SpecifierSet]] = _Validator(
  697. added="1.2"
  698. )
  699. """:external:ref:`core-metadata-requires-python`"""
  700. # Because `Requires-External` allows for non-PEP 440 version specifiers, we
  701. # don't do any processing on the values.
  702. requires_external: _Validator[Optional[List[str]]] = _Validator(added="1.2")
  703. """:external:ref:`core-metadata-requires-external`"""
  704. project_urls: _Validator[Optional[Dict[str, str]]] = _Validator(added="1.2")
  705. """:external:ref:`core-metadata-project-url`"""
  706. # PEP 685 lets us raise an error if an extra doesn't pass `Name` validation
  707. # regardless of metadata version.
  708. provides_extra: _Validator[Optional[List[utils.NormalizedName]]] = _Validator(
  709. added="2.1",
  710. )
  711. """:external:ref:`core-metadata-provides-extra`"""
  712. provides_dist: _Validator[Optional[List[str]]] = _Validator(added="1.2")
  713. """:external:ref:`core-metadata-provides-dist`"""
  714. obsoletes_dist: _Validator[Optional[List[str]]] = _Validator(added="1.2")
  715. """:external:ref:`core-metadata-obsoletes-dist`"""
  716. requires: _Validator[Optional[List[str]]] = _Validator(added="1.1")
  717. """``Requires`` (deprecated)"""
  718. provides: _Validator[Optional[List[str]]] = _Validator(added="1.1")
  719. """``Provides`` (deprecated)"""
  720. obsoletes: _Validator[Optional[List[str]]] = _Validator(added="1.1")
  721. """``Obsoletes`` (deprecated)"""