Skip to content

Commit

Permalink
Simplify metadata parser
Browse files Browse the repository at this point in the history
  • Loading branch information
domdfcoding committed Apr 25, 2023
1 parent bc9d969 commit 11b1f66
Showing 1 changed file with 7 additions and 18 deletions.
25 changes: 7 additions & 18 deletions dist_meta/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#

# stdlib
import re
import sys
from typing import List

Expand All @@ -41,12 +40,9 @@

__all__ = ("dump", "dumps", "load", "loads", "MissingFieldError")

WSP = " \t"
DELIMITER = "\n\n"
NEWLINE_MARK = '\uf8ff'

_unfold_re = re.compile(rf"\n([{WSP}])")


def _clean_desc(lines: List[str], wsp: str) -> List[str]:
# Adapted from inspect.cleandoc
Expand Down Expand Up @@ -94,23 +90,22 @@ def loads(rawtext: str) -> MetadataMapping:
:returns: A mapping of the metadata fields, and the long description
"""
rawtext = rawtext.replace('\r', '')

rawtext = rawtext.replace("\r\n", '\n')

if DELIMITER in rawtext:
rawtext, body = rawtext.split(DELIMITER, maxsplit=1)
else:
body = ''

# unfold per RFC 5322 § 2.2.3
rawtext = _unfold_re.sub(fr"{NEWLINE_MARK}\1", rawtext)
rawtext = rawtext.replace("\n\t", f"{NEWLINE_MARK}\t").replace("\n ", f"{NEWLINE_MARK} ")

file_content: List[str] = rawtext.split('\n')
file_content.reverse()

fields: MetadataMapping = MetadataMapping()

while file_content:
line = file_content.pop()
for line in file_content:
if not line:
continue

Expand All @@ -127,15 +122,9 @@ def loads(rawtext: str) -> MetadataMapping:
description_lines = _clean_desc(description_lines, '|')
# pylint: enable=loop-global-usage

# Remove any trailing or leading blank lines.
while description_lines and not description_lines[-1]:
description_lines.pop()
while description_lines and not description_lines[0]:
description_lines.pop(0)

field_value = '\n'.join(description_lines).strip() + '\n'

fields["Description"] = field_value
# pylint: disable=loop-invariant-statement
fields["Description"] = '\n'.join(description_lines).strip() + '\n'
# pylint: enable=loop-invariant-statement

if body.strip():
if "Description" in fields:
Expand Down

0 comments on commit 11b1f66

Please sign in to comment.