Skip to content

Commit

Permalink
Merge pull request #99 from Nautilus-Cyberneering/issue-98-error-vali…
Browse files Browse the repository at this point in the history
…dating-paths-for-renaming

Fix error validating path for renamed files
  • Loading branch information
josecelano authored Mar 2, 2022
2 parents dd064ba + d7d1a9b commit 06becf8
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 28 deletions.
47 changes: 32 additions & 15 deletions src/nautilus_librarian/mods/dvc/domain/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,6 @@ def extract_all_added_and_modified_and_renamed_files_from_dvc_diff(
return all_files


def extract_added_and_modified_and_renamed_files_from_dvc_diff(
dvc_diff_json, only_basename=True
):
all_files = extract_all_added_and_modified_and_renamed_files_from_dvc_diff(
dvc_diff_json, only_basename
)
files = filter_media_library_files(all_files)
return files


def extract_all_changed_files_from_dvc_diff(dvc_diff_json, only_basename=True):
"""
It gets a plain string list with the added, modified, deleted or renamed files from the dvc diff json.
Expand Down Expand Up @@ -166,13 +156,40 @@ def extract_renamed_files_from_dvc_diff(dvc_diff_json, only_basename=True):
return media_files


def extract_list_of_media_file_changes_from_dvc_diff_output(
dvc_diff, only_basename=True
):
return extract_added_and_modified_and_renamed_files_from_dvc_diff(
dvc_diff, only_basename
def extract_list_of_new_or_renamed_files_from_dvc_diff_output(dvc_diff_json):
dvc_diff = DvcDiffParser.from_json(dvc_diff_json)

added_files = dvc_diff.filter(
exclude_added=False,
exclude_modified=True,
exclude_deleted=True,
exclude_renamed=True,
only_basename=False,
)

renamed_files = dvc_diff.filter(
exclude_added=True,
exclude_modified=True,
exclude_deleted=True,
exclude_renamed=False,
only_basename=False,
)

# Renamed files are not plain filename list. They are a dict like this:
# {
# "new": "000002-32.600.2.tif",
# "old": "000001-32.600.2.tif",
# }
# That means the image was renamed from "000001-32.600.2.tif" to "000002-32.600.2.tif"

flat_renamed_files = map(lambda file: file["new"], renamed_files)

all_files = added_files + list(flat_renamed_files)

files = filter_media_library_files(all_files)

return files


def extract_added_files_from_dvc_diff(dvc_diff_json):
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from nautilus_librarian.domain.validate_filepaths import validate_filepath
from nautilus_librarian.mods.dvc.domain.utils import (
extract_list_of_media_file_changes_from_dvc_diff_output,
extract_list_of_new_or_renamed_files_from_dvc_diff_output,
get_new_filepath_if_is_a_renaming_dict,
)
from nautilus_librarian.typer.commands.workflows.actions.action_result import (
Expand All @@ -20,9 +20,7 @@ def validate_filepaths_action(dvc_diff):
ResultCode.EXIT, [Message("No media library file changes found")]
)

filepaths = extract_list_of_media_file_changes_from_dvc_diff_output(
dvc_diff, only_basename=False
)
filepaths = extract_list_of_new_or_renamed_files_from_dvc_diff_output(dvc_diff)

messages = []

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,38 @@
from nautilus_librarian.mods.dvc.domain.utils import (
extract_added_files_from_dvc_diff,
extract_deleted_files_from_dvc_diff,
extract_list_of_media_file_changes_from_dvc_diff_output,
extract_list_of_new_or_renamed_files_from_dvc_diff_output,
extract_modified_files_from_dvc_diff,
extract_renamed_files_from_dvc_diff,
get_new_filepath_if_is_a_renaming_dict,
)


def test_extract_list_of_media_file_changes_from_dvc_diff_output():
def test_extract_list_of_new_or_renamed_files_from_dvc_diff_output():
dvc_diff = {
"added": [
{"path": "data/000001/32/000001-32.600.2.tif"},
],
"deleted": [],
"modified": [],
"renamed": [],
"renamed": [
{
"path": {
"old": "data/000003/32/000003-32.600.2.tif",
"new": "data/000002/32/000002-32.600.2.tif",
}
}
],
}

filenames = extract_list_of_media_file_changes_from_dvc_diff_output(
filenames = extract_list_of_new_or_renamed_files_from_dvc_diff_output(
compact_json(dvc_diff)
)

assert filenames == ["000001-32.600.2.tif"]
assert filenames == [
"data/000001/32/000001-32.600.2.tif",
"data/000002/32/000002-32.600.2.tif", # We include only the new name of the file for renamed ones.
]


def test_extract_added_files_from_dvc_diff():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def given_a_dvc_diff_object_it_should_validate_the_filepath_of_the_new_media_fil
assert result.contains_text("000001-32.600.2.tif ✓")


def given_a_dvc_diff_object_it_should_validate_the_filepath_of_the_modified_media_files():
def given_a_dvc_diff_object_it_should_not_validate_the_filepath_of_the_modified_media_files():

dvc_diff_with_modified_image = {
"added": [],
Expand All @@ -37,7 +37,7 @@ def given_a_dvc_diff_object_it_should_validate_the_filepath_of_the_modified_medi
result = validate_filepaths_action(compact_json(dvc_diff_with_modified_image))

assert result.code == ResultCode.CONTINUE
assert result.contains_text("000002-32.600.2.tif ✓")
assert not result.contains_text("000002-32.600.2.tif ✓")


def given_a_dvc_diff_object_it_should_validate_the_filepath_of_the_renamed_media_files():
Expand All @@ -47,14 +47,20 @@ def given_a_dvc_diff_object_it_should_validate_the_filepath_of_the_renamed_media
"deleted": [],
"modified": [],
"renamed": [
{"path": "data/000003/32/000003-32.600.2.tif"},
{
"path": {
"old": "data/000001/32/000001-32.600.2.tif",
"new": "data/000002/32/000002-32.600.2.tif",
}
}
],
}

result = validate_filepaths_action(compact_json(dvc_diff_with_renamed_image))

assert result.code == ResultCode.CONTINUE
assert result.contains_text("000003-32.600.2.tif ✓")
assert result.contains_text("000002-32.600.2.tif ✓")
assert not result.contains_text("000001-32.600.2.tif ✓")


def given_a_wrong_media_filepath_it_should_show_an_error():
Expand Down

0 comments on commit 06becf8

Please sign in to comment.