]>
jfr.im git - dlqueue.git/blob - venv/lib/python3.11/site-packages/pip/_internal/metadata/_json.py
1 # Extracted from https://github.com/pfmoore/pkg_metadata
3 from email
.header
import Header
, decode_header
, make_header
4 from email
.message
import Message
5 from typing
import Any
, Dict
, List
, Union
9 ("Metadata-Version", False),
14 ("Supported-Platform", True),
16 ("Description", False),
17 ("Description-Content-Type", False),
20 ("Download-URL", False),
22 ("Author-email", False),
23 ("Maintainer", False),
24 ("Maintainer-email", False),
27 ("Requires-Dist", True),
28 ("Requires-Python", False),
29 ("Requires-External", True),
30 ("Project-URL", True),
31 ("Provides-Extra", True),
32 ("Provides-Dist", True),
33 ("Obsoletes-Dist", True),
37 def json_name(field
: str) -> str:
38 return field
.lower().replace("-", "_")
41 def msg_to_json(msg
: Message
) -> Dict
[str, Any
]:
42 """Convert a Message object into a JSON-compatible dictionary."""
44 def sanitise_header(h
: Union
[Header
, str]) -> str:
45 if isinstance(h
, Header
):
47 for bytes, encoding
in decode_header(h
):
48 if encoding
== "unknown-8bit":
53 except UnicodeDecodeError:
54 # If not, latin1 at least won't fail
56 chunks
.append((bytes, encoding
))
57 return str(make_header(chunks
))
61 for field
, multi
in METADATA_FIELDS
:
64 key
= json_name(field
)
66 value
: Union
[str, List
[str]] = [
67 sanitise_header(v
) for v
in msg
.get_all(field
)
70 value
= sanitise_header(msg
.get(field
))
72 # Accept both comma-separated and space-separated
73 # forms, for better compatibility with old data.
75 value
= [v
.strip() for v
in value
.split(",")]
80 payload
= msg
.get_payload()
82 result
["description"] = payload