|
15 | 15 | from collections.abc import Callable, Iterable, Mapping, Sequence |
16 | 16 | from pathlib import Path |
17 | 17 | from textwrap import indent |
18 | | -from typing import Any, TypeVar |
| 18 | +from typing import Any, Literal, TypeAlias, TypeVar |
19 | 19 |
|
20 | 20 | import bioregistry |
21 | 21 | import click |
|
41 | 41 |
|
42 | 42 | __all__ = [ |
43 | 43 | "NoBuildError", |
| 44 | + "OntologyFormat", |
44 | 45 | "get_ontology", |
45 | 46 | ] |
46 | 47 |
|
@@ -193,23 +194,31 @@ def get_ontology( |
193 | 194 | return obo |
194 | 195 |
|
195 | 196 |
|
| 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 | + |
196 | 207 | def _ensure_ontology_path( |
197 | 208 | 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 |
213 | 222 | return None, None |
214 | 223 |
|
215 | 224 |
|
|
0 commit comments