Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validate_paths data prefix #728

Merged
merged 4 commits into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions ark/utils/io_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import warnings


def validate_paths(paths, data_prefix=True):
def validate_paths(paths, data_prefix=False):
"""Verifys that paths exist and don't leave Docker's scope

Args:
Expand All @@ -22,20 +22,21 @@ def validate_paths(paths, data_prefix=True):
paths = [paths]

for path in paths:
# check data prefix
if data_prefix and not str(path).startswith('../data'):
raise ValueError(
f'The path, {path}, is not prefixed with \'../data\'.\n'
f'Be sure to add all images/files/data to the \'data\' folder, '
f'and to reference as \'../data/path_to_data/myfile.tif\'')

if not os.path.exists(path):
if str(path).startswith('../data') or not data_prefix:
for parent in reversed(pathlib.Path(path).parents):
if not os.path.exists(parent):
raise ValueError(
f'A bad path, {path}, was provided.\n'
f'The folder, {parent.name}, could not be found...')
raise ValueError(
f'The file/path, {pathlib.Path(path).name}, could not be found...')
else:
raise ValueError(
f'The path, {path}, is not prefixed with \'../data\'.\n'
f'Be sure to add all images/files/data to the \'data\' folder, '
f'and to reference as \'../data/path_to_data/myfile.tif\'')
for parent in reversed(pathlib.Path(path).parents):
if not os.path.exists(parent):
raise ValueError(
f'A bad path, {path}, was provided.\n'
f'The folder, {parent.name}, could not be found...')
raise ValueError(
f'The file/path, {pathlib.Path(path).name}, could not be found...')


def list_files(dir_name, substrs=None, exact_match=False, ignore_hidden=True):
Expand Down
15 changes: 10 additions & 5 deletions ark/utils/io_utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,31 @@ def test_validate_paths():
wrong_file = os.path.join(valid_path + '/real_subdirectory', 'not_a_real_file.tiff')

# test one valid path
iou.validate_paths(valid_path)
iou.validate_paths(valid_path, data_prefix=True)

# test multiple valid paths
iou.validate_paths([valid_path, '../data', valid_path + '/real_subdirectory'])
iou.validate_paths([valid_path, '../data', valid_path + '/real_subdirectory'],
data_prefix=True)

# test out-of-scope
with pytest.raises(ValueError, match=r".*not_a_real_directory.*prefixed.*"):
iou.validate_paths(starts_out_of_scope)
iou.validate_paths(starts_out_of_scope, data_prefix=True)

# test mid-directory existence
with pytest.raises(ValueError, match=r".*bad path.*not_a_real_subdirectory.*"):
iou.validate_paths(bad_middle_path)
iou.validate_paths(bad_middle_path, data_prefix=True)

# test file existence
with pytest.raises(ValueError, match=r".*The file/path.*not_a_real_file.*"):
iou.validate_paths(wrong_file)
iou.validate_paths(wrong_file, data_prefix=True)

# make tempdir for testing outside of docker
with tempfile.TemporaryDirectory() as valid_path:

# check valid path but no data prefix
with pytest.raises(ValueError, match=r".*is not prefixed with.*"):
iou.validate_paths(valid_path, data_prefix=True)

# make valid subdirectory
valid_parts = [p for p in pathlib.Path(valid_path).parts]
valid_parts[0] = 'not_a_real_directory'
Expand Down