-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add json_metadata property to BaseDistribution #11095
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# Extracted from https://github.com/pfmoore/pkg_metadata | ||
|
||
from email.header import Header, decode_header, make_header | ||
from email.message import Message | ||
from typing import Any, Dict, List, Union | ||
|
||
METADATA_FIELDS = [ | ||
# Name, Multiple-Use | ||
("Metadata-Version", False), | ||
("Name", False), | ||
("Version", False), | ||
("Dynamic", True), | ||
("Platform", True), | ||
("Supported-Platform", True), | ||
("Summary", False), | ||
("Description", False), | ||
("Description-Content-Type", False), | ||
("Keywords", False), | ||
("Home-page", False), | ||
("Download-URL", False), | ||
("Author", False), | ||
("Author-email", False), | ||
("Maintainer", False), | ||
("Maintainer-email", False), | ||
("License", False), | ||
("Classifier", True), | ||
("Requires-Dist", True), | ||
("Requires-Python", False), | ||
("Requires-External", True), | ||
("Project-URL", True), | ||
("Provides-Extra", True), | ||
("Provides-Dist", True), | ||
("Obsoletes-Dist", True), | ||
] | ||
|
||
|
||
def json_name(field: str) -> str: | ||
return field.lower().replace("-", "_") | ||
|
||
|
||
def msg_to_json(msg: Message) -> Dict[str, Any]: | ||
"""Convert a Message object into a JSON-compatible dictionary.""" | ||
|
||
def sanitise_header(h: Union[Header, str]) -> str: | ||
if isinstance(h, Header): | ||
chunks = [] | ||
for bytes, encoding in decode_header(h): | ||
if encoding == "unknown-8bit": | ||
try: | ||
# See if UTF-8 works | ||
bytes.decode("utf-8") | ||
encoding = "utf-8" | ||
except UnicodeDecodeError: | ||
# If not, latin1 at least won't fail | ||
encoding = "latin1" | ||
chunks.append((bytes, encoding)) | ||
return str(make_header(chunks)) | ||
return str(h) | ||
|
||
result = {} | ||
for field, multi in METADATA_FIELDS: | ||
if field not in msg: | ||
continue | ||
key = json_name(field) | ||
if multi: | ||
value: Union[str, List[str]] = [ | ||
sanitise_header(v) for v in msg.get_all(field) | ||
] | ||
else: | ||
value = sanitise_header(msg.get(field)) | ||
if key == "keywords": | ||
# Accept both comma-separated and space-separated | ||
# forms, for better compatibility with old data. | ||
if "," in value: | ||
value = [v.strip() for v in value.split(",")] | ||
else: | ||
value = value.split() | ||
result[key] = value | ||
|
||
payload = msg.get_payload() | ||
if payload: | ||
result["description"] = payload | ||
|
||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be cached, too. There's no real reason to recalculate it every time. Although it's probably not a performance bottleneck, so it's not a big deal either way.