Skip to content

Commit

Permalink
Allow to pass a list of paths
Browse files Browse the repository at this point in the history
  • Loading branch information
flferretti committed May 14, 2024
1 parent 9fa363d commit c1000b7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
21 changes: 13 additions & 8 deletions src/resolve_robotics_uri_py/resolve_robotics_uri_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ def pathlist_list_to_string(path_list: Iterable[str | pathlib.Path]) -> str:
# ===================


def resolve_robotics_uri(uri: str, extra_path: str | None = None) -> pathlib.Path:
def resolve_robotics_uri(
uri: str, package_dirs: list[str] | None = None
) -> pathlib.Path:
"""
Resolve a robotics URI to an absolute filename.
Expand All @@ -90,8 +92,7 @@ def resolve_robotics_uri(uri: str, extra_path: str | None = None) -> pathlib.Pat
Raises:
FileNotFoundError: If no file corresponding to the URI is found.
"""

extra_path = extra_path or ""
package_dirs = package_dirs if isinstance(package_dirs, list) else [package_dirs]

# If the URI has no scheme, use by default file:// which maps the resolved input
# path to a URI with empty authority
Expand Down Expand Up @@ -148,10 +149,14 @@ def resolve_robotics_uri(uri: str, extra_path: str | None = None) -> pathlib.Pat
model_filenames = []

# Search the resource in the path from the env variables
for folder in set(get_search_paths_from_envs(SupportedEnvVars | {extra_path})):
for folder in set(get_search_paths_from_envs(SupportedEnvVars)) | {
path
for directory in package_dirs
if directory and (path := pathlib.Path(directory)).exists()
}:

# Join the folder from environment variable and the URI path
candidate_file_name = pathlib.Path(folder) / uri_path
candidate_file_name = folder / uri_path

# Expand or resolve the file path (symlinks and ..)
candidate_file_name = candidate_file_name.resolve()
Expand Down Expand Up @@ -185,17 +190,17 @@ def main():
)
parser.add_argument("uri", metavar="URI", type=str, help="URI to resolve")
parser.add_argument(
"--extra_path",
"--package_dirs",
metavar="PATH",
type=str,
help="Additional environment variable to look for the file",
help="Additional paths to look for the file",
default=None,
)

args = parser.parse_args()

try:
result = resolve_robotics_uri(args.uri, args.extra_path)
result = resolve_robotics_uri(args.uri, args.package_dirs)
except FileNotFoundError as e:
print(e, file=sys.stderr)
sys.exit(1)
Expand Down
15 changes: 6 additions & 9 deletions test/test_resolve_robotics_uri_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,13 @@ def test_scheme_file():

def test_additional_search_path():

import tempfile
import pathlib
import resolve_robotics_uri_py

clear_env_vars()

uri = "model://my_model"
extra_path = "MY_SEARCH_PATH"

with tempfile.TemporaryDirectory() as temp_dir:

Expand All @@ -177,11 +180,5 @@ def test_additional_search_path():
top_level.touch(exist_ok=True)

# Test resolving a URI with an additional search path
with export_env_var(name=extra_path, value=str(temp_dir_path)):
result = resolve_robotics_uri_py.resolve_robotics_uri(uri, extra_path)
assert result == temp_dir_path / "my_model"

# Test resolving a URI an additional non-existing search path
with export_env_var(name=extra_path, value="/this/path/does/not/exist"):
with pytest.raises(FileNotFoundError):
resolve_robotics_uri_py.resolve_robotics_uri(uri, extra_path)
result = resolve_robotics_uri_py.resolve_robotics_uri(uri, temp_dir)
assert result == temp_dir_path / "my_model"

0 comments on commit c1000b7

Please sign in to comment.