-
Notifications
You must be signed in to change notification settings - Fork 261
Improve parsing of author information #521
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
| import warnings | ||
|
|
||
| from contextlib import contextmanager | ||
| from email.utils import parseaddr | ||
| from pathlib import Path | ||
| from typing import Any | ||
| from typing import Iterator | ||
|
|
@@ -105,3 +106,26 @@ def readme_content_type(path: str | Path) -> str: | |
| return "text/markdown" | ||
| else: | ||
| return "text/plain" | ||
|
|
||
|
|
||
| def parse_author(address: str) -> tuple[str | None, str | None]: | ||
| """Parse name and address parts from an email address string. | ||
|
|
||
| >>> parse_author("John Doe <john.doe@example.com>") | ||
| ('John Doe', 'john.doe@example.com') | ||
|
|
||
| .. note:: | ||
|
|
||
| If the input string does not contain an ``@`` character, it is | ||
| assumed that it represents only a name without an email address. | ||
|
|
||
| :param address: the email address string to parse. | ||
| :return: a 2-tuple with the parsed name and email address. If a | ||
| part is missing, ``None`` will be returned in its place. | ||
| """ | ||
| if "@" not in address: | ||
| return address, None | ||
| name, email = parseaddr(address) | ||
| if not name and "@" not in email: | ||
| return email, None | ||
| return name or None, email or None | ||
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
| import pytest | ||
|
|
||
| from poetry.core.utils.helpers import combine_unicode | ||
| from poetry.core.utils.helpers import parse_author | ||
| from poetry.core.utils.helpers import parse_requires | ||
| from poetry.core.utils.helpers import readme_content_type | ||
| from poetry.core.utils.helpers import temporary_directory | ||
|
|
@@ -118,3 +119,60 @@ def test_utils_helpers_readme_content_type( | |
| readme: str | Path, content_type: str | ||
| ) -> None: | ||
| assert readme_content_type(readme) == content_type | ||
|
|
||
|
|
||
| def test_utils_helpers_parse_author(): | ||
|
||
| """Test the :func:`parse_author` function.""" | ||
|
|
||
| # Verify the (probable) default use case | ||
| name, email = parse_author("John Doe <john.doe@example.com>") | ||
| assert name == "John Doe" | ||
| assert email == "john.doe@example.com" | ||
|
|
||
| # Name only | ||
| name, email = parse_author("John Doe") | ||
| assert name == "John Doe" | ||
| assert email is None | ||
|
|
||
| # Name with a “special” character + email address | ||
| name, email = parse_author("R&D <researchanddevelopment@example.com>") | ||
| assert name == "R&D" | ||
| assert email == "researchanddevelopment@example.com" | ||
|
|
||
| # Name with a “special” character only | ||
| name, email = parse_author("R&D") | ||
| assert name == "R&D" | ||
| assert email is None | ||
|
|
||
| # Name with fancy unicode character + email address | ||
| name, email = parse_author("my·fancy corp <my-fancy-corp@example.com>") | ||
| assert name == "my·fancy corp" | ||
| assert email == "my-fancy-corp@example.com" | ||
|
|
||
| # Name with fancy unicode character only | ||
| name, email = parse_author("my·fancy corp") | ||
| assert name == "my·fancy corp" | ||
| assert email is None | ||
|
|
||
| # Email address only, wrapped in angular brackets | ||
| name, email = parse_author("<john.doe@example.com>") | ||
| assert name is None | ||
| assert email == "john.doe@example.com" | ||
|
|
||
| # Email address only | ||
| name, email = parse_author("john.doe@example.com") | ||
| assert name is None | ||
| assert email == "john.doe@example.com" | ||
|
|
||
| # Non-RFC-conform cases with unquoted commas | ||
| name, email = parse_author("asf,dfu@t.b") | ||
| assert name == "asf" | ||
| assert email is None | ||
|
|
||
| name, email = parse_author("asf,<dfu@t.b>") | ||
| assert name == "asf" | ||
| assert email is None | ||
|
|
||
| name, email = parse_author("asf, dfu@t.b") | ||
| assert name == "asf" | ||
| assert email is None | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you stack this upon #517, please?