Skip to content

Commit

Permalink
remove unnecessary handling of file:/// paths
Browse files Browse the repository at this point in the history
  • Loading branch information
Karl5766 committed Jul 22, 2024
1 parent f585e40 commit 76811d9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
24 changes: 13 additions & 11 deletions snakebids/core/input_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,25 +393,23 @@ def _all_custom_paths(config: InputsConfig):
return all(comp.get("custom_path") for comp in config.values())


def _is_url(path: Path | str):
"""Test if a path location is a url.
def _is_network_fs_path(path: Path | str):
"""Test if a path location is using a network file system e.g. "gs://" or "s3://".
Paths like "file://" "local://" "gs://" "s3://" etc. are urls while "/linux/path",
"C://Windows/path" or "relative/path" are not.
Paths like "file:///..." will not be recognized as network even if they may be
network locations mapped to a local location. Windows ("C://") and linux
("/path/to/file") paths and relative path ("path/to/file") are not network file
system paths.
Parameter
---------
path
A UPath, Path, or str object to be checked
Returns
-------
is_url : bool
True if the path is a url
True if the path belongs to a network file system
"""
path = str(path)
if path.startswith(("file://", "local://")):
return True
# StackOverflow reference: (by Kukanani)
# https://stackoverflow.com/questions/8357098/how-can-i-check-if-a-url-is-absolute-using-python
return bool(urlparse(path).netloc) # netloc="" for non-local paths
Expand Down Expand Up @@ -463,7 +461,7 @@ def _gen_bids_layout(
if not pybidsdb_dir:
pybidsdb_dir = None
# Otherwise check for relative path and update
elif not _is_url(pybidsdb_dir) and not Path(pybidsdb_dir).is_absolute():
elif not _is_network_fs_path(pybidsdb_dir) and not Path(pybidsdb_dir).is_absolute():
pybidsdb_dir = None
_logger.warning("Absolute path must be provided, database will not be used")

Expand Down Expand Up @@ -748,7 +746,11 @@ def _parse_bids_path(path: str, entities: Iterable[str]) -> tuple[str, dict[str,
# If path is relative, we need to get a slash in front of it to ensure parsing works
# correctly. So prepend "./" or ".\" and run function again, then strip before
# returning
if not _is_url(path) and not os.path.isabs(path) and get_first_dir(path) != ".":
if (
not _is_network_fs_path(path)
and not os.path.isabs(path)
and get_first_dir(path) != "."
):
path_, wildcard_values = _parse_bids_path(os.path.join(".", path), entities)
return str(Path(path_)), wildcard_values

Expand Down
20 changes: 9 additions & 11 deletions snakebids/tests/test_generate_inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
_all_custom_paths,
_gen_bids_layout,
_get_components,
_is_url,
_is_network_fs_path,
_normalize_database_args,
_parse_bids_path,
_parse_custom_path,
Expand Down Expand Up @@ -1603,7 +1603,7 @@ def test_all_custom_paths(count: int):
assert not _all_custom_paths(config)


def test_is_url():
def test_is_network_fs_path():
test_data = (
("file", "RELATIVE"),
("hello", "RELATIVE"),
Expand All @@ -1614,22 +1614,20 @@ def test_is_url():
(r"C:\some\file", "ABSOLUTE"),
(r"C:\\some\\file", "ABSOLUTE"),
("C:/some/file", "ABSOLUTE"),
("gs://some/google/cloud/bucket", "URL"),
("s3://some/s3/bucket", "URL"),
("file://some/file", "URL"),
("local://some/file", "URL"),
("gs://some/google/cloud/bucket", "NETWORK"),
("s3://some/s3/bucket", "NETWORK"),
)

for path, path_type in test_data:
# https://stackoverflow.com/questions/8357098/how-can-i-check-if-a-url-is-absolute-using-python
isurl = path_type == "URL"
isnet = path_type == "NETWORK"

# test the path itself, and the corresponding Path(path)
assert (
_is_url(path) == isurl
), f"Path {path} should have isurl={isurl} but got {_is_url(path)}"
if not isurl:
assert not _is_url(Path(path))
_is_network_fs_path(path) == isnet
), f"Path {path} should have isnet={isnet} but got {_is_network_fs_path(path)}"
if not isnet:
assert not _is_network_fs_path(Path(path))


@example_if(
Expand Down

0 comments on commit 76811d9

Please sign in to comment.