Skip to content

Commit 8c05274

Browse files
committed
Improve descendants and ancestors CLIs
1 parent 29f325a commit 8c05274

1 file changed

Lines changed: 35 additions & 23 deletions

File tree

src/pyobo/cli/lookup.py

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
"""CLI for PyOBO lookups."""
22

3+
import inspect
34
import json
45
import sys
5-
from collections.abc import Mapping
6+
from collections.abc import Iterable, Mapping
7+
from typing import TYPE_CHECKING
68

79
import click
810
from more_click import verbose_option
@@ -17,7 +19,10 @@
1719
strict_option,
1820
version_option,
1921
)
20-
from ..constants import LookupKwargs
22+
from ..constants import GetOntologyKwargs, LookupKwargs
23+
24+
if TYPE_CHECKING:
25+
from curies import Reference
2126

2227
__all__ = [
2328
"lookup",
@@ -31,16 +36,26 @@ def lookup():
3136

3237
def lookup_annotate(f: Clickable) -> Clickable:
3338
"""Add appropriate decorators to lookup CLI functions."""
39+
signature = inspect.signature(f)
40+
param = signature.parameters["kwargs"]
41+
if param.kind is not inspect.Parameter.VAR_KEYWORD:
42+
raise ValueError("programmer error")
3443
for decorator in [
3544
lookup.command(),
36-
prefix_argument,
3745
verbose_option,
3846
force_option,
3947
force_process_option,
4048
strict_option,
4149
version_option,
4250
]:
4351
f = decorator(f)
52+
53+
if param.annotation == Unpack[LookupKwargs]:
54+
f = prefix_argument(f)
55+
elif param.annotation == Unpack[GetOntologyKwargs]:
56+
pass
57+
else:
58+
raise ValueError
4459
return f
4560

4661

@@ -249,33 +264,30 @@ def hierarchy(
249264

250265

251266
@lookup_annotate
252-
@click.argument("identifier")
253-
def ancestors(
254-
identifier: str,
255-
**kwargs: Unpack[LookupKwargs],
256-
) -> None:
267+
@click.argument("curie")
268+
def ancestors(curie: str, **kwargs: Unpack[GetOntologyKwargs]) -> None:
257269
"""Look up ancestors."""
258-
from ..api import get_ancestors, get_name
270+
from ..api import get_ancestors
259271

260-
# note, prefix is passed via kwargs
261-
ancestors = get_ancestors(identifier=identifier, **kwargs)
262-
for ancestor in sorted(ancestors or []):
263-
click.echo(f"{ancestor.curie}\t{get_name(ancestor, version=kwargs['version'])}")
272+
ancestors = get_ancestors(curie, **kwargs)
273+
_list_curies(ancestors, **kwargs)
264274

265275

266276
@lookup_annotate
267-
@click.argument("identifier")
268-
def descendants(
269-
identifier: str,
270-
**kwargs: Unpack[LookupKwargs],
271-
) -> None:
277+
@click.argument("curie")
278+
def descendants(curie: str, **kwargs: Unpack[GetOntologyKwargs]) -> None:
272279
"""Look up descendants."""
273-
from ..api import get_descendants, get_name
280+
from ..api import get_descendants
281+
282+
descendants = get_descendants(curie, **kwargs)
283+
_list_curies(descendants, **kwargs)
284+
285+
286+
def _list_curies(references: Iterable[Reference], **kwargs: Unpack[GetOntologyKwargs]) -> None:
287+
from ..api import get_name
274288

275-
# note, prefix is passed via kwargs
276-
descendants = get_descendants(identifier=identifier, **kwargs)
277-
for descendant in sorted(descendants or []):
278-
click.echo(f"{descendant.curie}\t{get_name(descendant, version=kwargs['version'])}")
289+
for reference in sorted(references or []):
290+
click.echo(f"{reference.curie}\t{get_name(reference, version=kwargs['version'])}")
279291

280292

281293
@lookup_annotate

0 commit comments

Comments
 (0)