diff --git a/src/resolve_robotics_uri_py/resolve_robotics_uri_py.py b/src/resolve_robotics_uri_py/resolve_robotics_uri_py.py index 043eb90..4c7aef1 100644 --- a/src/resolve_robotics_uri_py/resolve_robotics_uri_py.py +++ b/src/resolve_robotics_uri_py/resolve_robotics_uri_py.py @@ -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. @@ -91,7 +93,11 @@ def resolve_robotics_uri(uri: str, extra_path: str | None = None) -> pathlib.Pat FileNotFoundError: If no file corresponding to the URI is found. """ - extra_path = extra_path or "" + package_dirs = ( + package_dirs + if package_dirs and 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 @@ -148,10 +154,12 @@ 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 (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() @@ -185,17 +193,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) diff --git a/test/test_resolve_robotics_uri_py.py b/test/test_resolve_robotics_uri_py.py index f858df2..31e7902 100644 --- a/test/test_resolve_robotics_uri_py.py +++ b/test/test_resolve_robotics_uri_py.py @@ -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: @@ -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"