Skip to content

Commit

Permalink
fix: truncate author names at 70 characters in case one is very long
Browse files Browse the repository at this point in the history
this should not happen very often for people's names, but it is common that publishers give list on names in creator_seq with the incorrect separator, resulting in very long strings
  • Loading branch information
ManonMarchand committed Oct 4, 2023
1 parent 85fa8d6 commit 62efe9d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
13 changes: 8 additions & 5 deletions pyvo/registry/regtap.py
Original file line number Diff line number Diff line change
Expand Up @@ -855,13 +855,16 @@ def describe(self, verbose=False, width=78, file=None):
if verbose:
if self.source_value:
print(f"\nSource: {self.source_value}", file=file)
# don't print creators if its first value is overly long
if self.creators and len(self.creators[0]) < width:
if self.creators:
# if any creator has a name longer than 70 characters, we
# truncate it.
creators = [f"{creator[:70]}..." if len(creator) > 70
else creator for creator in self.creators]
nmax_authors = 5
if len(self.creators) <= nmax_authors:
print(f"Authors: {', '.join(self.creators)}", file=file)
if len(creators) <= nmax_authors:
print(f"Authors: {', '.join(creators)}", file=file)
else:
print(f"Authors: {', '.join(self.creators[:nmax_authors])} et al.\n"
print(f"Authors: {', '.join(creators[:nmax_authors])} et al.\n"
"See creators attribute for the complete list of authors.", file=file)
if self.alt_identifier:
print(f"Alternative identifier: {self.alt_identifier}", file=file)
Expand Down
21 changes: 18 additions & 3 deletions pyvo/registry/tests/test_regtap.py
Original file line number Diff line number Diff line change
Expand Up @@ -736,9 +736,7 @@ def test_describe_multi(self, flash_service):
assert "More info: http://dc.zah" in output

def test_describe_long_authors_list(self):
# generate a registry record with minimum
# information for the describe method and
# a long list of authors
"""Check that long list of authors use et al.."""
rsc = _makeRegistryRecord(
access_urls=[],
standard_ids=["ivo://pyvo/test"],
Expand All @@ -754,6 +752,23 @@ def test_describe_long_authors_list(self):
# output should cut at 5 authors
assert "Authors: a, a, a, a, a et al." in output

def test_describe_long_author_name(self):
"""Check that long author names are truncated."""
rsc = _makeRegistryRecord(
access_urls=[],
standard_ids=["ivo://pyvo/test"],
short_name=["name"],
intf_types=[],
intf_roles=[],
creator_seq=["a" * 71],
res_title=["title"]
)
out = io.StringIO()
rsc.describe(verbose=True, file=out)
output = out.getvalue()
# should cut the long author name at 70 characters
assert f"Authors: {'a'*70}..." in output

def test_no_access_url(self):
rsc = _makeRegistryRecord(
access_urls=[],
Expand Down

0 comments on commit 62efe9d

Please sign in to comment.