Skip to content

Commit 64e05e9

Browse files
committed
Refactor ontology path operations
1 parent 6fb6686 commit 64e05e9

1 file changed

Lines changed: 25 additions & 16 deletions

File tree

src/pyobo/getters.py

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from collections.abc import Callable, Iterable, Mapping, Sequence
1616
from pathlib import Path
1717
from textwrap import indent
18-
from typing import Any, TypeVar
18+
from typing import Any, Literal, TypeAlias, TypeVar
1919

2020
import bioregistry
2121
import click
@@ -41,6 +41,7 @@
4141

4242
__all__ = [
4343
"NoBuildError",
44+
"OntologyFormat",
4445
"get_ontology",
4546
]
4647

@@ -193,23 +194,31 @@ def get_ontology(
193194
return obo
194195

195196

197+
OntologyFormat: TypeAlias = Literal["obo", "owl", "json", "rdf"]
198+
# order matters in this list, since order implicitly defines priority
199+
_ONTOLOGY_GETTERS: list[tuple[OntologyFormat, Callable[[str], str | None]]] = [
200+
("obo", bioregistry.get_obo_download),
201+
("owl", bioregistry.get_owl_download),
202+
("json", bioregistry.get_json_download),
203+
("rdf", bioregistry.get_rdf_download),
204+
]
205+
206+
196207
def _ensure_ontology_path(
197208
prefix: str, force: bool, version: str | None
198-
) -> tuple[str, Path] | tuple[None, None]:
199-
for ontology_format, url in [
200-
("obo", bioregistry.get_obo_download(prefix)),
201-
("owl", bioregistry.get_owl_download(prefix)),
202-
("json", bioregistry.get_json_download(prefix)),
203-
]:
204-
if url is not None:
205-
try:
206-
path = ensure_path(prefix, url=url, force=force, version=version)
207-
except (urllib.error.HTTPError, pystow.utils.DownloadError):
208-
continue
209-
except pystow.utils.UnexpectedDirectoryError:
210-
continue # TODO report more info about the URL and the name it tried to make
211-
else:
212-
return ontology_format, path
209+
) -> tuple[OntologyFormat, Path] | tuple[None, None]:
210+
for ontology_format, getter in _ONTOLOGY_GETTERS:
211+
url = getter(prefix)
212+
if url is None:
213+
continue
214+
try:
215+
path = ensure_path(prefix, url=url, force=force, version=version)
216+
except (urllib.error.HTTPError, pystow.utils.DownloadError):
217+
continue
218+
except pystow.utils.UnexpectedDirectoryError:
219+
continue # TODO report more info about the URL and the name it tried to make
220+
else:
221+
return ontology_format, path
213222
return None, None
214223

215224

0 commit comments

Comments
 (0)