From bc2ab34933d246305b14485183dd4eb8e61d3b85 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Wed, 23 Oct 2024 23:47:31 -0400 Subject: [PATCH] refactor: remove JSON output option Signed-off-by: Henry Schreiner --- src/packaging/metadata.py | 41 +------------------------- tests/test_metadata.py | 60 ++++++++++++++++----------------------- 2 files changed, 25 insertions(+), 76 deletions(-) diff --git a/src/packaging/metadata.py b/src/packaging/metadata.py index 1e51393d..09912ee5 100644 --- a/src/packaging/metadata.py +++ b/src/packaging/metadata.py @@ -1,7 +1,6 @@ from __future__ import annotations import builtins -import dataclasses import email.feedparser import email.header import email.message @@ -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 @@ -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. """ diff --git a/tests/test_metadata.py b/tests/test_metadata.py index 50c50687..1b74fa65 100644 --- a/tests/test_metadata.py +++ b/tests/test_metadata.py @@ -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) @@ -855,42 +856,6 @@ def test_large(self): } ) - assert meta.as_json() == { - "author": "Example!", - "author_email": "Unknown ", - "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 ", - "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"), @@ -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