Skip to content

Commit

Permalink
Deduplicate code in lookup CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
cthoyt committed Jan 21, 2025
1 parent d801c8a commit 54e39cd
Showing 1 changed file with 21 additions and 39 deletions.
60 changes: 21 additions & 39 deletions src/pyobo/cli/lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,52 +113,34 @@ def metadata(**kwargs: Unpack[LookupKwargs]) -> None:
def ids(**kwargs: Unpack[LookupKwargs]) -> None:
"""Page through the identifiers of entities in the given namespace."""
id_list = get_ids(**kwargs)
click.echo_via_pager("\n".join(id_list))
if not id_list:
click.secho("no data", fg="red")
else:
click.echo_via_pager("\n".join(id_list))


@lookup_annotate
@identifier_option
def names(identifier: str | None, **kwargs: Unpack[LookupKwargs]) -> None:
"""Page through the identifiers and names of entities in the given namespace."""
id_to_name = get_id_name_mapping(**kwargs)
if identifier is None:
_help_page_mapping(id_to_name)
else:
name = id_to_name.get(identifier)
if name is None:
click.secho(f"No name available for {identifier}", fg="red")
else:
click.echo(name)
_help_page_mapping(id_to_name, identifier=identifier)


@lookup_annotate
@identifier_option
def species(identifier: str | None, **kwargs: Unpack[LookupKwargs]) -> None:
"""Page through the identifiers and species of entities in the given namespace."""
id_to_species = get_id_species_mapping(**kwargs)
if identifier is None:
_help_page_mapping(id_to_species)
else:
species = id_to_species.get(identifier)
if species is None:
click.secho(f"No species available for {identifier}", fg="red")
else:
click.echo(species)
_help_page_mapping(id_to_species, identifier=identifier)


@lookup_annotate
@identifier_option
def definitions(identifier: str | None, **kwargs: Unpack[LookupKwargs]) -> None:
"""Page through the identifiers and definitions of entities in the given namespace."""
id_to_definition = get_id_definition_mapping(**kwargs)
if identifier is None:
_help_page_mapping(id_to_definition)
else:
definition = id_to_definition.get(identifier)
if definition is None:
click.secho(f"No definition available for {identifier}", fg="red")
else:
click.echo(definition)
_help_page_mapping(id_to_definition, identifier=identifier)


@lookup_annotate
Expand All @@ -168,9 +150,15 @@ def typedefs(**kwargs: Unpack[LookupKwargs]) -> None:
echo_df(df)


def _help_page_mapping(id_to_name: Mapping[str, str]) -> None:
def _help_page_mapping(id_to_name: Mapping[str, str], *, identifier: str | None = None) -> None:
if not id_to_name:
click.secho("no data", fg="red")
elif identifier:
value = id_to_name.get(identifier)
if value:
click.echo(value)
else:
click.secho(f"no data for {identifier}", fg="red")
else:
click.echo_via_pager("\n".join("\t".join(item) for item in id_to_name.items()))

Expand Down Expand Up @@ -248,7 +236,10 @@ def hierarchy(
include_has_member=include_has_member,
**kwargs,
)
click.echo_via_pager("\n".join("\t".join(row) for row in h.edges()))
if h.number_of_edges() == 0:
click.secho("no data", fg="red")
else:
click.echo_via_pager("\n".join("\t".join(row) for row in h.edges()))


@lookup_annotate
Expand All @@ -258,6 +249,7 @@ def ancestors(
**kwargs: Unpack[LookupKwargs],
) -> None:
"""Look up ancestors."""
# note, prefix is passed via kwargs
curies = get_ancestors(identifier=identifier, **kwargs)
for curie in sorted(curies or []):
click.echo(f"{curie}\t{get_name_by_curie(curie, version=kwargs['version'])}")
Expand All @@ -270,6 +262,7 @@ def descendants(
**kwargs: Unpack[LookupKwargs],
) -> None:
"""Look up descendants."""
# note, prefix is passed via kwargs
curies = get_descendants(identifier=identifier, **kwargs)
for curie in sorted(curies or []):
click.echo(f"{curie}\t{get_name_by_curie(curie, version=kwargs['version'])}")
Expand Down Expand Up @@ -297,15 +290,4 @@ def alts(
) -> None:
"""Page through alt ids in a namespace."""
id_to_alts = get_id_to_alts(**kwargs)
if identifier is None:
click.echo_via_pager(
"\n".join(
f"{identifier}\t{alt}" for identifier, alts in id_to_alts.items() for alt in alts
)
)
else:
_alts = id_to_alts.get(identifier)
if _alts is None:
click.secho(f"No alternate identifiers for {identifier}", fg="red")
else:
click.echo("\n".join(_alts))
_help_page_mapping(id_to_alts, identifier=identifier)

0 comments on commit 54e39cd

Please sign in to comment.