Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Assume NPM person can be string #20

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
33 changes: 19 additions & 14 deletions hatch_nodejs_version/metadata_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
import os.path
import re
import urllib.parse
from typing import Any
from typing import Any, Union

from hatchling.metadata.plugin.interface import MetadataHookInterface

AUTHOR_PATTERN = r"^([^<(]+?)?[ \t]*(?:<([^>(]+?)>)?[ \t]*(?:\(([^)]+?)\)|$)"
AUTHOR_PATTERN = r"^(?P<name>[^<(]+?)?[ \t]*(?:<(?P<email>[^>(]+?)>)?[ \t]*(?:\((?P<url>[^)]+?)\)|$)"
REPOSITORY_PATTERN = r"^(?:(gist|bitbucket|gitlab|github):)?(.*?)$"
REPOSITORY_TABLE = {
"gitlab": "https://gitlab.com",
Expand Down Expand Up @@ -137,19 +137,24 @@ def _parse_bugs(self, bugs: str | dict[str, str]) -> str | None:
return bugs["url"]

def _parse_person(self, person: dict[str, str]) -> dict[str, str]:
if {"url", "email"} & person.keys():
result = {"name": person["name"]}
if "email" in person:
result["email"] = person["email"]
result = {}
if isinstance(person, dict):
if {"url", "email"} & person.keys():
result["name"] = person["name"]
if "email" in person:
result["email"] = person["email"]
return result
else:
author = person["name"]
else:
match = re.match(AUTHOR_PATTERN, person["name"])
if match is None:
raise ValueError(f"Invalid author name: {person['name']}")
name, email, _ = match.groups()
result = {"name": name}
if email is not None:
result["email"] = email

author = person
match = re.match(AUTHOR_PATTERN, author)
if match is None:
raise ValueError(f"Invalid author name: {author}")
name, email, _ = match.groups()
result = {"name": name}
if email is not None:
result["email"] = email
return result

def _parse_repository(self, repository: str | dict[str, str]) -> str:
Expand Down
17 changes: 17 additions & 0 deletions tests/test_metadata_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#
# SPDX-License-Identifier: MIT
import pytest
import json

from hatch_nodejs_version.metadata_source import NodeJSMetadataHook

Expand Down Expand Up @@ -165,3 +166,19 @@ def test_labels(self, project):
assert urls["the-repository"] == "https://github.com/some/code.git"
assert urls["the-bug-tracker"] == "https://www.send-help.com"
assert urls["the-homepage"] == "https://where-the-heart-is.com"

def test_authors_accepted_as_strings(self, project):
original_package_content = json.loads(TRIVIAL_PACKAGE_CONTENTS)
updated_package_content = original_package_content.copy()
author_as_string = f"{original_package_content['author']['name']} " \
f"<{original_package_content['author']['email']}>"
updated_package_content['author'] = author_as_string
(project / "pyproject.toml").write_text(TRIVIAL_PYPROJECT_CONTENTS)
(project / "package.json").write_text(json.dumps(updated_package_content))

config = {}
metadata = {}
metadata_source = NodeJSMetadataHook(project, config=config)
metadata_source.update(metadata)
assert metadata == TRIVIAL_EXPECTED_METADATA