Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

metadata.py 16KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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 Dict, List, Optional, Tuple, Union, cast
  9. if sys.version_info >= (3, 8): # pragma: no cover
  10. from typing import TypedDict
  11. else: # pragma: no cover
  12. if typing.TYPE_CHECKING:
  13. from typing_extensions import TypedDict
  14. else:
  15. try:
  16. from typing_extensions import TypedDict
  17. except ImportError:
  18. class TypedDict:
  19. def __init_subclass__(*_args, **_kwargs):
  20. pass
  21. # The RawMetadata class attempts to make as few assumptions about the underlying
  22. # serialization formats as possible. The idea is that as long as a serialization
  23. # formats offer some very basic primitives in *some* way then we can support
  24. # serializing to and from that format.
  25. class RawMetadata(TypedDict, total=False):
  26. """A dictionary of raw core metadata.
  27. Each field in core metadata maps to a key of this dictionary (when data is
  28. provided). The key is lower-case and underscores are used instead of dashes
  29. compared to the equivalent core metadata field. Any core metadata field that
  30. can be specified multiple times or can hold multiple values in a single
  31. field have a key with a plural name.
  32. Core metadata fields that can be specified multiple times are stored as a
  33. list or dict depending on which is appropriate for the field. Any fields
  34. which hold multiple values in a single field are stored as a list.
  35. """
  36. # Metadata 1.0 - PEP 241
  37. metadata_version: str
  38. name: str
  39. version: str
  40. platforms: List[str]
  41. summary: str
  42. description: str
  43. keywords: List[str]
  44. home_page: str
  45. author: str
  46. author_email: str
  47. license: str
  48. # Metadata 1.1 - PEP 314
  49. supported_platforms: List[str]
  50. download_url: str
  51. classifiers: List[str]
  52. requires: List[str]
  53. provides: List[str]
  54. obsoletes: List[str]
  55. # Metadata 1.2 - PEP 345
  56. maintainer: str
  57. maintainer_email: str
  58. requires_dist: List[str]
  59. provides_dist: List[str]
  60. obsoletes_dist: List[str]
  61. requires_python: str
  62. requires_external: List[str]
  63. project_urls: Dict[str, str]
  64. # Metadata 2.0
  65. # PEP 426 attempted to completely revamp the metadata format
  66. # but got stuck without ever being able to build consensus on
  67. # it and ultimately ended up withdrawn.
  68. #
  69. # However, a number of tools had started emiting METADATA with
  70. # `2.0` Metadata-Version, so for historical reasons, this version
  71. # was skipped.
  72. # Metadata 2.1 - PEP 566
  73. description_content_type: str
  74. provides_extra: List[str]
  75. # Metadata 2.2 - PEP 643
  76. dynamic: List[str]
  77. # Metadata 2.3 - PEP 685
  78. # No new fields were added in PEP 685, just some edge case were
  79. # tightened up to provide better interoptability.
  80. _STRING_FIELDS = {
  81. "author",
  82. "author_email",
  83. "description",
  84. "description_content_type",
  85. "download_url",
  86. "home_page",
  87. "license",
  88. "maintainer",
  89. "maintainer_email",
  90. "metadata_version",
  91. "name",
  92. "requires_python",
  93. "summary",
  94. "version",
  95. }
  96. _LIST_STRING_FIELDS = {
  97. "classifiers",
  98. "dynamic",
  99. "obsoletes",
  100. "obsoletes_dist",
  101. "platforms",
  102. "provides",
  103. "provides_dist",
  104. "provides_extra",
  105. "requires",
  106. "requires_dist",
  107. "requires_external",
  108. "supported_platforms",
  109. }
  110. def _parse_keywords(data: str) -> List[str]:
  111. """Split a string of comma-separate keyboards into a list of keywords."""
  112. return [k.strip() for k in data.split(",")]
  113. def _parse_project_urls(data: List[str]) -> Dict[str, str]:
  114. """Parse a list of label/URL string pairings separated by a comma."""
  115. urls = {}
  116. for pair in data:
  117. # Our logic is slightly tricky here as we want to try and do
  118. # *something* reasonable with malformed data.
  119. #
  120. # The main thing that we have to worry about, is data that does
  121. # not have a ',' at all to split the label from the Value. There
  122. # isn't a singular right answer here, and we will fail validation
  123. # later on (if the caller is validating) so it doesn't *really*
  124. # matter, but since the missing value has to be an empty str
  125. # and our return value is dict[str, str], if we let the key
  126. # be the missing value, then they'd have multiple '' values that
  127. # overwrite each other in a accumulating dict.
  128. #
  129. # The other potentional issue is that it's possible to have the
  130. # same label multiple times in the metadata, with no solid "right"
  131. # answer with what to do in that case. As such, we'll do the only
  132. # thing we can, which is treat the field as unparseable and add it
  133. # to our list of unparsed fields.
  134. parts = [p.strip() for p in pair.split(",", 1)]
  135. parts.extend([""] * (max(0, 2 - len(parts)))) # Ensure 2 items
  136. # TODO: The spec doesn't say anything about if the keys should be
  137. # considered case sensitive or not... logically they should
  138. # be case-preserving and case-insensitive, but doing that
  139. # would open up more cases where we might have duplicate
  140. # entries.
  141. label, url = parts
  142. if label in urls:
  143. # The label already exists in our set of urls, so this field
  144. # is unparseable, and we can just add the whole thing to our
  145. # unparseable data and stop processing it.
  146. raise KeyError("duplicate labels in project urls")
  147. urls[label] = url
  148. return urls
  149. def _get_payload(msg: email.message.Message, source: Union[bytes, str]) -> str:
  150. """Get the body of the message."""
  151. # If our source is a str, then our caller has managed encodings for us,
  152. # and we don't need to deal with it.
  153. if isinstance(source, str):
  154. payload: str = msg.get_payload()
  155. return payload
  156. # If our source is a bytes, then we're managing the encoding and we need
  157. # to deal with it.
  158. else:
  159. bpayload: bytes = msg.get_payload(decode=True)
  160. try:
  161. return bpayload.decode("utf8", "strict")
  162. except UnicodeDecodeError:
  163. raise ValueError("payload in an invalid encoding")
  164. # The various parse_FORMAT functions here are intended to be as lenient as
  165. # possible in their parsing, while still returning a correctly typed
  166. # RawMetadata.
  167. #
  168. # To aid in this, we also generally want to do as little touching of the
  169. # data as possible, except where there are possibly some historic holdovers
  170. # that make valid data awkward to work with.
  171. #
  172. # While this is a lower level, intermediate format than our ``Metadata``
  173. # class, some light touch ups can make a massive difference in usability.
  174. # Map METADATA fields to RawMetadata.
  175. _EMAIL_TO_RAW_MAPPING = {
  176. "author": "author",
  177. "author-email": "author_email",
  178. "classifier": "classifiers",
  179. "description": "description",
  180. "description-content-type": "description_content_type",
  181. "download-url": "download_url",
  182. "dynamic": "dynamic",
  183. "home-page": "home_page",
  184. "keywords": "keywords",
  185. "license": "license",
  186. "maintainer": "maintainer",
  187. "maintainer-email": "maintainer_email",
  188. "metadata-version": "metadata_version",
  189. "name": "name",
  190. "obsoletes": "obsoletes",
  191. "obsoletes-dist": "obsoletes_dist",
  192. "platform": "platforms",
  193. "project-url": "project_urls",
  194. "provides": "provides",
  195. "provides-dist": "provides_dist",
  196. "provides-extra": "provides_extra",
  197. "requires": "requires",
  198. "requires-dist": "requires_dist",
  199. "requires-external": "requires_external",
  200. "requires-python": "requires_python",
  201. "summary": "summary",
  202. "supported-platform": "supported_platforms",
  203. "version": "version",
  204. }
  205. def parse_email(data: Union[bytes, str]) -> Tuple[RawMetadata, Dict[str, List[str]]]:
  206. """Parse a distribution's metadata.
  207. This function returns a two-item tuple of dicts. The first dict is of
  208. recognized fields from the core metadata specification. Fields that can be
  209. parsed and translated into Python's built-in types are converted
  210. appropriately. All other fields are left as-is. Fields that are allowed to
  211. appear multiple times are stored as lists.
  212. The second dict contains all other fields from the metadata. This includes
  213. any unrecognized fields. It also includes any fields which are expected to
  214. be parsed into a built-in type but were not formatted appropriately. Finally,
  215. any fields that are expected to appear only once but are repeated are
  216. included in this dict.
  217. """
  218. raw: Dict[str, Union[str, List[str], Dict[str, str]]] = {}
  219. unparsed: Dict[str, List[str]] = {}
  220. if isinstance(data, str):
  221. parsed = email.parser.Parser(policy=email.policy.compat32).parsestr(data)
  222. else:
  223. parsed = email.parser.BytesParser(policy=email.policy.compat32).parsebytes(data)
  224. # We have to wrap parsed.keys() in a set, because in the case of multiple
  225. # values for a key (a list), the key will appear multiple times in the
  226. # list of keys, but we're avoiding that by using get_all().
  227. for name in frozenset(parsed.keys()):
  228. # Header names in RFC are case insensitive, so we'll normalize to all
  229. # lower case to make comparisons easier.
  230. name = name.lower()
  231. # We use get_all() here, even for fields that aren't multiple use,
  232. # because otherwise someone could have e.g. two Name fields, and we
  233. # would just silently ignore it rather than doing something about it.
  234. headers = parsed.get_all(name)
  235. # The way the email module works when parsing bytes is that it
  236. # unconditionally decodes the bytes as ascii using the surrogateescape
  237. # handler. When you pull that data back out (such as with get_all() ),
  238. # it looks to see if the str has any surrogate escapes, and if it does
  239. # it wraps it in a Header object instead of returning the string.
  240. #
  241. # As such, we'll look for those Header objects, and fix up the encoding.
  242. value = []
  243. # Flag if we have run into any issues processing the headers, thus
  244. # signalling that the data belongs in 'unparsed'.
  245. valid_encoding = True
  246. for h in headers:
  247. # It's unclear if this can return more types than just a Header or
  248. # a str, so we'll just assert here to make sure.
  249. assert isinstance(h, (email.header.Header, str))
  250. # If it's a header object, we need to do our little dance to get
  251. # the real data out of it. In cases where there is invalid data
  252. # we're going to end up with mojibake, but there's no obvious, good
  253. # way around that without reimplementing parts of the Header object
  254. # ourselves.
  255. #
  256. # That should be fine since, if mojibacked happens, this key is
  257. # going into the unparsed dict anyways.
  258. if isinstance(h, email.header.Header):
  259. # The Header object stores it's data as chunks, and each chunk
  260. # can be independently encoded, so we'll need to check each
  261. # of them.
  262. chunks: List[Tuple[bytes, Optional[str]]] = []
  263. for bin, encoding in email.header.decode_header(h):
  264. try:
  265. bin.decode("utf8", "strict")
  266. except UnicodeDecodeError:
  267. # Enable mojibake.
  268. encoding = "latin1"
  269. valid_encoding = False
  270. else:
  271. encoding = "utf8"
  272. chunks.append((bin, encoding))
  273. # Turn our chunks back into a Header object, then let that
  274. # Header object do the right thing to turn them into a
  275. # string for us.
  276. value.append(str(email.header.make_header(chunks)))
  277. # This is already a string, so just add it.
  278. else:
  279. value.append(h)
  280. # We've processed all of our values to get them into a list of str,
  281. # but we may have mojibake data, in which case this is an unparsed
  282. # field.
  283. if not valid_encoding:
  284. unparsed[name] = value
  285. continue
  286. raw_name = _EMAIL_TO_RAW_MAPPING.get(name)
  287. if raw_name is None:
  288. # This is a bit of a weird situation, we've encountered a key that
  289. # we don't know what it means, so we don't know whether it's meant
  290. # to be a list or not.
  291. #
  292. # Since we can't really tell one way or another, we'll just leave it
  293. # as a list, even though it may be a single item list, because that's
  294. # what makes the most sense for email headers.
  295. unparsed[name] = value
  296. continue
  297. # If this is one of our string fields, then we'll check to see if our
  298. # value is a list of a single item. If it is then we'll assume that
  299. # it was emitted as a single string, and unwrap the str from inside
  300. # the list.
  301. #
  302. # If it's any other kind of data, then we haven't the faintest clue
  303. # what we should parse it as, and we have to just add it to our list
  304. # of unparsed stuff.
  305. if raw_name in _STRING_FIELDS and len(value) == 1:
  306. raw[raw_name] = value[0]
  307. # If this is one of our list of string fields, then we can just assign
  308. # the value, since email *only* has strings, and our get_all() call
  309. # above ensures that this is a list.
  310. elif raw_name in _LIST_STRING_FIELDS:
  311. raw[raw_name] = value
  312. # Special Case: Keywords
  313. # The keywords field is implemented in the metadata spec as a str,
  314. # but it conceptually is a list of strings, and is serialized using
  315. # ", ".join(keywords), so we'll do some light data massaging to turn
  316. # this into what it logically is.
  317. elif raw_name == "keywords" and len(value) == 1:
  318. raw[raw_name] = _parse_keywords(value[0])
  319. # Special Case: Project-URL
  320. # The project urls is implemented in the metadata spec as a list of
  321. # specially-formatted strings that represent a key and a value, which
  322. # is fundamentally a mapping, however the email format doesn't support
  323. # mappings in a sane way, so it was crammed into a list of strings
  324. # instead.
  325. #
  326. # We will do a little light data massaging to turn this into a map as
  327. # it logically should be.
  328. elif raw_name == "project_urls":
  329. try:
  330. raw[raw_name] = _parse_project_urls(value)
  331. except KeyError:
  332. unparsed[name] = value
  333. # Nothing that we've done has managed to parse this, so it'll just
  334. # throw it in our unparseable data and move on.
  335. else:
  336. unparsed[name] = value
  337. # We need to support getting the Description from the message payload in
  338. # addition to getting it from the the headers. This does mean, though, there
  339. # is the possibility of it being set both ways, in which case we put both
  340. # in 'unparsed' since we don't know which is right.
  341. try:
  342. payload = _get_payload(parsed, data)
  343. except ValueError:
  344. unparsed.setdefault("description", []).append(
  345. parsed.get_payload(decode=isinstance(data, bytes))
  346. )
  347. else:
  348. if payload:
  349. # Check to see if we've already got a description, if so then both
  350. # it, and this body move to unparseable.
  351. if "description" in raw:
  352. description_header = cast(str, raw.pop("description"))
  353. unparsed.setdefault("description", []).extend(
  354. [description_header, payload]
  355. )
  356. elif "description" in unparsed:
  357. unparsed["description"].append(payload)
  358. else:
  359. raw["description"] = payload
  360. # We need to cast our `raw` to a metadata, because a TypedDict only support
  361. # literal key names, but we're computing our key names on purpose, but the
  362. # way this function is implemented, our `TypedDict` can only have valid key
  363. # names.
  364. return cast(RawMetadata, raw), unparsed