Skip to content

Commit e112eb5

Browse files
authored
Merge pull request #508 from bioimage-io/add_get_resolved_source_path
add get_resolved_source_path
2 parents f7e0eac + 00ab794 commit e112eb5

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

bioimageio/spec/shared/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
BIOIMAGEIO_COLLECTION_ERROR,
99
BIOIMAGEIO_SITE_CONFIG,
1010
BIOIMAGEIO_SITE_CONFIG_ERROR,
11-
RDF_NAMES,
1211
DownloadCancelled,
12+
RDF_NAMES,
1313
_resolve_json_from_url,
14+
get_resolved_source_path,
1415
resolve_local_source,
1516
resolve_rdf_source,
1617
resolve_rdf_source_and_type,

bioimageio/spec/shared/_resolve_source.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,22 @@ def _resolve_source_list(
332332
]
333333

334334

335+
def get_resolved_source_path(
336+
source: typing.Union[
337+
raw_nodes.URI, str, pathlib.Path, raw_nodes.ResolvedImportableSourceFile, raw_nodes.ImportableSourceFile
338+
],
339+
root_path: typing.Union[os.PathLike, URI],
340+
pbar=None,
341+
) -> pathlib.Path:
342+
resolved = resolve_source(source, root_path=root_path, pbar=pbar)
343+
if isinstance(resolved, os.PathLike):
344+
return pathlib.Path(resolved)
345+
elif isinstance(resolved, raw_nodes.ResolvedImportableSourceFile):
346+
return resolved.source_file
347+
else:
348+
raise NotImplementedError(type(resolved))
349+
350+
335351
def resolve_local_sources(
336352
sources: typing.Sequence[typing.Union[str, os.PathLike, raw_nodes.URI]],
337353
root_path: os.PathLike,
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import os
2+
from pathlib import Path
3+
from typing import Optional
4+
5+
import pytest
6+
7+
from bioimageio.spec.shared.raw_nodes import URI
8+
9+
10+
def mock_download(uri: URI, output: Optional[os.PathLike] = None, pbar=None):
11+
return Path(__file__).resolve()
12+
13+
14+
@pytest.mark.parametrize(
15+
"src",
16+
[
17+
"https://example.com/fake",
18+
Path(__file__),
19+
__file__,
20+
URI("https://example.com/fake"),
21+
],
22+
)
23+
def test_get_resolved_source_path(src):
24+
from bioimageio.spec.shared import get_resolved_source_path
25+
26+
import bioimageio.spec
27+
28+
bioimageio.spec.shared._resolve_source._download_url = mock_download
29+
res = get_resolved_source_path(src, root_path=Path(__file__).parent)
30+
assert isinstance(res, Path)
31+
assert res.exists()
32+
assert res == Path(__file__).resolve()

0 commit comments

Comments
 (0)