Skip to content

Commit

Permalink
refactor: remove JSON output option
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
  • Loading branch information
henryiii committed Oct 24, 2024
1 parent ef4fd77 commit 664d675
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 75 deletions.
40 changes: 1 addition & 39 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 @@ -274,34 +273,6 @@ def _get_payload(msg: email.message.Message, source: bytes | str) -> str:
_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
class RFC822Policy(email.policy.EmailPolicy):
"""
Expand Down Expand Up @@ -883,16 +854,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
36 changes: 0 additions & 36 deletions tests/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -715,42 +715,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

0 comments on commit 664d675

Please # to comment.