From 11b1f662f8f87367b5013ff7d26b97e72913319c Mon Sep 17 00:00:00 2001 From: Dominic Davis-Foster Date: Tue, 25 Apr 2023 08:39:17 +0100 Subject: [PATCH] Simplify metadata parser --- dist_meta/metadata.py | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/dist_meta/metadata.py b/dist_meta/metadata.py index 1627bd7..f5543e5 100644 --- a/dist_meta/metadata.py +++ b/dist_meta/metadata.py @@ -27,7 +27,6 @@ # # stdlib -import re import sys from typing import List @@ -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 @@ -94,7 +90,8 @@ 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) @@ -102,15 +99,13 @@ def loads(rawtext: str) -> MetadataMapping: 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 @@ -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: