Skip to content

Commit 7f7f375

Browse files
Add utility to get kwave bin paths and URLs (#377)
1 parent 67750ec commit 7f7f375

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

src/openlifu/util/assets.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,16 @@ def stmt_calls_banned(stmt: ast.stmt) -> bool:
134134
def _import_kwave_inertly() -> ModuleType:
135135
"""Import kwave without allowing it to install binaries"""
136136
return _import_without_calls("kwave", banned_calls=["install_binaries"])
137+
138+
def get_kwave_paths() -> list[tuple[Path, str]]:
139+
"""Get a list of paths and urls to kwave binaries.
140+
141+
Each item in the list is a pair consisting of the install of a needed binary, followed by a download url for that binary.
142+
"""
143+
kwave = _import_kwave_inertly()
144+
paths : list[tuple[str, str]] = []
145+
for url_list in kwave.URL_DICT[kwave.PLATFORM].values():
146+
for url in url_list:
147+
_, filename = url.split("/")[-2:]
148+
paths.append((Path(kwave.BINARY_PATH) / filename, url))
149+
return paths

tests/test_assets.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
from __future__ import annotations
22

33
import shutil
4+
from pathlib import Path
45
from unittest.mock import MagicMock
56

67
import pytest
78
import requests
89

9-
from openlifu.util.assets import install_asset
10+
from openlifu.util.assets import get_kwave_paths, install_asset
1011

1112

1213
def test_destination_already_exists(tmp_path, mocker):
@@ -94,3 +95,14 @@ def test_raises_error_if_no_source_provided(tmp_path):
9495
destination = tmp_path / "asset.dat"
9596
with pytest.raises(ValueError, match="Either path_to_asset or url_to_asset must be provided."):
9697
install_asset(destination, path_to_asset=None, url_to_asset=None)
98+
99+
def test_get_kwave_paths():
100+
"""Check that get_kwave_paths returns a nonempty list of (Path, str) pairs.
101+
This may break if kwave changes how it represents download urls and binary paths."""
102+
paths = get_kwave_paths()
103+
assert isinstance(paths, list)
104+
assert len(paths) > 0
105+
for p, url in paths:
106+
assert isinstance(p, Path)
107+
assert isinstance(url, str)
108+
assert len(url) > 0

0 commit comments

Comments
 (0)