Skip to content

Commit

Permalink
✨ Switch to using author_names to get the author names
Browse files Browse the repository at this point in the history
Closes #29.
  • Loading branch information
davep committed Feb 4, 2025
1 parent fbc22be commit 59cd33b
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 40 deletions.
2 changes: 2 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

- When saving a PEP's source a default filename is provided.
([#24](https://github.com/davep/peplum/pull/24))
- Updated the PEP loading code to use the [newly-added `author_names`
property in the API](https://github.com/python/peps/issues/4211).

## v0.3.0

Expand Down
42 changes: 6 additions & 36 deletions src/peplum/app/data/pep.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,10 @@ class PEP:
"""The number of the PEP."""
title: str
"""The title of the PEP."""
authors: tuple[str, ...]
# authors: str
"""The authors of the PEP."""
author_names: tuple[str, ...]
"""The names of the authors of the PEP."""
sponsor: str | None
"""The sponsor of the PEP."""
delegate: str | None
Expand Down Expand Up @@ -179,14 +181,6 @@ class PEP:
notes: str = ""
"""The user's notes associated with this PEP."""

_AUTHOR_SPLIT = compile(r",(?! +Jr)")
"""The regular expression for splitting up authors.
Notes:
This is 'good enough' but not ideal. It might need updating and
improvement later on.
"""

def annotate(self, *, notes: str | None = None) -> PEP:
"""Annotate the PEP.
Expand All @@ -210,7 +204,7 @@ def __contains__(self, search_text: str) -> bool:
return (
search_text in str(self.number)
or search_text in self.title.casefold()
or search_text in " ".join(self.authors).casefold()
or search_text in " ".join(self.author_names).casefold()
or search_text in (self.sponsor or "").casefold()
or search_text in (self.delegate or "").casefold()
or search_text in self.status.casefold()
Expand All @@ -226,31 +220,6 @@ def __contains__(self, search_text: str) -> bool:
or search_text in self.notes.casefold()
)

@classmethod
def _authors(cls, authors: str) -> tuple[str, ...]:
"""Get the authors from a string.
Args:
authors: The authors to parse.
Returns:
A tuple of the authors found.
Notes:
The authors in the PEPs are a comma-separated string of author
names. The problem is, as of the time of writing, there's at
least one author who has a comma in their name. So this method
makes an effort to split up the authors while also keeping such
a name intact.
Yes, this is going to be brittle.
Yes, the code here is a bit of a hack.
https://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/
"""
return tuple(author.strip() for author in cls._AUTHOR_SPLIT.split(authors))

@classmethod
def _parse(cls, data: dict[str, Any]) -> dict[str, Any]:
"""Parse the content of a PEP from the API data.
Expand All @@ -270,7 +239,8 @@ def get_ints(field: str) -> tuple[int, ...]:
return dict(
number=data.get("number", -1),
title=data.get("title", ""),
authors=cls._authors(data.get("authors", "")),
# authors=data.get("authors"),
author_names=tuple(data.get("author_names", [])),
sponsor=data.get("sponsor"),
delegate=data.get("delegate"),
discussions_to=data.get("discussions_to"),
Expand Down
6 changes: 4 additions & 2 deletions src/peplum/app/data/peps.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def __init__(self, author: str) -> None:
"""The folded version of the author for case-insensitive lookup."""

def __rand__(self, pep: PEP) -> bool:
return self._folded_author in (author.casefold() for author in pep.authors)
return self._folded_author in (author.casefold() for author in pep.author_names)

def __str__(self) -> str:
return str(self._author)
Expand Down Expand Up @@ -347,7 +347,9 @@ def authors(self) -> tuple[AuthorCount, ...]:
"""The authors and their counts as found in the PEPs."""
return tuple(
AuthorCount(author, count)
for author, count in Counter(chain(*(pep.authors for pep in self))).items()
for author, count in Counter(
chain(*(pep.author_names for pep in self))
).items()
)

def _describe(self, name: str, filter_type: type[Filter]) -> str | None:
Expand Down
6 changes: 6 additions & 0 deletions src/peplum/app/screens/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,12 @@ async def download_pep_data(self) -> None:
@on(Loaded)
def load_fresh_peps(self, message: Loaded) -> None:
"""React to a fresh set of PEPs being made available."""
if len(message.peps.authors) == 0:
self.notify(
"You likely have a cached copy of the older version of the PEP data; a redownload is recommended.",
severity="warning",
timeout=8,
)
self.all_peps = message.peps.sorted_by(load_configuration().peps_sort_order)

def on_mount(self) -> None:
Expand Down
2 changes: 1 addition & 1 deletion src/peplum/app/widgets/pep_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ def watch_pep(self) -> None:
else:
self.query_one("#title", Value).show(self.pep.title)
self.query_one("#author", ClickableValue).show(
AuthorItem(author) for author in self.pep.authors
AuthorItem(author) for author in self.pep.author_names
)
self.query_one("#sponsor", Value).show(self.pep.sponsor)
self.query_one("#delegate", Value).show(self.pep.delegate)
Expand Down
4 changes: 3 additions & 1 deletion src/peplum/app/widgets/peps_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ def __init__(self, pep: PEP) -> None:
info.add_column(width=6)
info.add_column(ratio=1)
info.add_column(width=11, justify="right")
info.add_row("", f"[dim]{', '.join(pep.authors)}[/]", f"[dim]{pep.created}[/]")
info.add_row(
"", f"[dim]{', '.join(pep.author_names)}[/]", f"[dim]{pep.created}[/]"
)

self._pep = pep
"""The PEP that this option is showing."""
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/test_peps.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
"number": 1,
"title": "PEP Purpose and Guidelines",
"authors": "Author 1, Author 2, Author 3",
"author_names": [
"Author 1",
"Author 2",
"Author 3",
],
"discussions_to": None,
"status": "Active",
"type": "Process",
Expand All @@ -47,6 +52,7 @@
"number": 458,
"title": "Secure PyPI downloads with signed repository metadata",
"authors": "Author 1",
"author_names": ["Author 1"],
"discussions_to": "https://discuss.python.org/t/pep-458-secure-pypi-downloads-with-package-signing/2648",
"status": "Accepted",
"type": "Standards Track",
Expand All @@ -66,6 +72,7 @@
"number": 467,
"title": "Minor API improvements for binary sequences",
"authors": "Author 1, Jr.",
"author_names": ["Author 1, Jr."],
"discussions_to": "https://discuss.python.org/t/42001",
"status": "Draft",
"type": "Informational",
Expand All @@ -85,6 +92,7 @@
"number": 639,
"title": "Improving License Clarity with Better Package Metadata",
"authors": "Author 1, Author 1, Jr.",
"author_names": ["Author 1", "Author 1, Jr."],
"discussions_to": "https://discuss.python.org/t/53020",
"status": "Provisional",
"type": "Process",
Expand All @@ -104,6 +112,7 @@
"number": 213,
"title": "Attribute Access Handlers",
"authors": "Author 1, Jr., Author 1",
"author_names": ["Author 1, Jr.", "Author 1"],
"discussions_to": None,
"status": "Deferred",
"type": "Standards Track",
Expand All @@ -123,6 +132,7 @@
"number": 204,
"title": "Range Literals",
"authors": "Author 2",
"author_names": ["Author 2"],
"discussions_to": None,
"status": "Rejected",
"type": "Informational",
Expand All @@ -142,6 +152,7 @@
"number": 3,
"title": "Guidelines for Handling Bug Reports",
"authors": "Author 1",
"author_names": ["Author 1"],
"discussions_to": None,
"status": "Withdrawn",
"type": "Process",
Expand All @@ -161,6 +172,7 @@
"number": 100,
"title": "Python Unicode Integration",
"authors": "Author 1",
"author_names": ["Author 1"],
"discussions_to": None,
"status": "Final",
"type": "Standards Track",
Expand All @@ -180,6 +192,7 @@
"number": 5,
"title": "Guidelines for Language Evolution",
"authors": "Author 3",
"author_names": ["Author 3"],
"discussions_to": None,
"status": "Superseded",
"type": "Informational",
Expand Down

0 comments on commit 59cd33b

Please sign in to comment.