Skip to content
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

feat: support writing metadata #846

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor: remove JSON output option
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
  • Loading branch information
henryiii committed Oct 25, 2024
commit bc2ab34933d246305b14485183dd4eb8e61d3b85
41 changes: 1 addition & 40 deletions src/packaging/metadata.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import annotations

import builtins
import dataclasses
import email.feedparser
import email.header
import email.message
Expand Down Expand Up @@ -280,35 +279,6 @@ def _get_payload(msg: email.message.Message, source: bytes | str) -> str:
"version": "version",
}
_RAW_TO_EMAIL_MAPPING = {raw: email for email, raw in _EMAIL_TO_RAW_MAPPING.items()}
_MULTI_FIELDS = {_RAW_TO_EMAIL_MAPPING[x] for x in _LIST_FIELDS | _DICT_FIELDS}


@dataclasses.dataclass
class _JSonMessageSetter:
"""
This provides an API to build a JSON message output in the same way as the
classic Message. Line breaks are preserved this way.
"""

data: dict[str, str | list[str]]

def __setitem__(self, name: str, value: str | None) -> None:
key = name.replace("-", "_")
if value is None:
return

if name == "keywords":
values = (x.strip() for x in value.split(","))
self.data[key] = [x for x in values if x]
elif name in _MULTI_FIELDS:
entry = self.data.setdefault(key, [])
assert isinstance(entry, list)
entry.append(value)
else:
self.data[key] = value

def set_payload(self, payload: str) -> None:
self["description"] = payload


# This class is for writing RFC822 messages
Expand Down Expand Up @@ -935,16 +905,7 @@ def as_rfc822(self) -> RFC822Message:
self._write_metadata(message)
return message

def as_json(self) -> dict[str, str | list[str]]:
"""
Return a JSON message with the metadata.
"""
message: dict[str, str | list[str]] = {}
smart_message = _JSonMessageSetter(message)
self._write_metadata(smart_message)
return message

def _write_metadata(self, message: RFC822Message | _JSonMessageSetter) -> None:
def _write_metadata(self, message: RFC822Message) -> None:
"""
Return an RFC822 message with the metadata.
"""
Expand Down
60 changes: 24 additions & 36 deletions tests/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,7 @@ def test_invalid_license_files(self, license_files):
with pytest.raises(metadata.InvalidMetadata):
meta.license_files # noqa: B018


class TestMetadataWriting:
def test_write_metadata(self):
meta = metadata.Metadata.from_raw(_RAW_EXAMPLE)
Expand Down Expand Up @@ -855,42 +856,6 @@ def test_large(self):
}
)

assert meta.as_json() == {
"author": "Example!",
"author_email": "Unknown <example@example.com>",
"classifier": [
"Development Status :: 4 - Beta",
"Programming Language :: Python",
],
"description": "some readme 👋\n",
"description_content_type": "text/markdown",
"keywords": ["trampolim", "is", "interesting"],
"license": "some license text",
"maintainer_email": "Other Example <other@example.com>",
"metadata_version": "2.1",
"name": "full_metadata",
"project_url": [
"homepage, example.com",
"documentation, readthedocs.org",
"repository, github.com/some/repo",
"changelog, github.com/some/repo/blob/master/CHANGELOG.rst",
],
"provides_extra": ["test"],
"requires_dist": [
"dependency1",
"dependency2>1.0.0",
"dependency3[extra]",
'dependency4; os_name != "nt"',
'dependency5[other-extra]>1.0; os_name == "nt"',
'test_dependency; extra == "test"',
'test_dependency[test_extra]; extra == "test"',
'test_dependency[test_extra2]>3.0; os_name == "nt" and extra == "test"',
],
"requires_python": ">=3.8",
"summary": "A package with all the metadata :)",
"version": "3.2.1",
}

core_metadata = meta.as_rfc822()
assert core_metadata.items() == [
("metadata-version", "2.1"),
Expand Down Expand Up @@ -928,3 +893,26 @@ def test_large(self):
]

assert core_metadata.get_payload() == "some readme 👋\n"

def test_modern_license(self):
meta = metadata.Metadata.from_raw(
{
"metadata_version": "2.4",
"name": "full_metadata",
"version": "3.2.1",
"license_expression": "MIT",
"license_files": ["LICENSE.txt", "LICENSE"],
}
)

core_metadata = meta.as_rfc822()
assert core_metadata.items() == [
("metadata-version", "2.4"),
("name", "full_metadata"),
("version", "3.2.1"),
("license-expression", "MIT"),
("license-file", "LICENSE.txt"),
("license-file", "LICENSE"),
]

assert core_metadata.get_payload() is None
Loading