Skip to content

Commit 0af950f

Browse files
authored
Fix broken external_file paths when organizing on Windows (#1832)
* Fix windows path issue * fix tests * use walrus operation
1 parent fa33e8d commit 0af950f

3 files changed

Lines changed: 9 additions & 4 deletions

File tree

dandi/organize.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from enum import Enum
1919
import os
2020
import os.path as op
21+
import posixpath
2122
from pathlib import Path, PurePosixPath
2223
import re
2324
import traceback
@@ -275,7 +276,7 @@ def _create_external_file_names(metadata: list[dict]) -> list[dict]:
275276
renamed_path_list = []
276277
uuid_str = ext_file_dict.get("id", str(uuid.uuid4()))
277278
for no, ext_file in enumerate(ext_file_dict["external_files"]):
278-
renamed = op.join(
279+
renamed = posixpath.join(
279280
nwb_folder_name, f"{uuid_str}_external_file_{no}{ext_file.suffix}"
280281
)
281282
renamed_path_list.append(renamed)

dandi/pynwb_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import inspect
1818
import os
1919
import os.path as op
20-
from pathlib import Path
20+
from pathlib import Path, PurePosixPath
2121
import re
2222
from typing import IO, Any, TypeVar, cast
2323
import warnings
@@ -408,8 +408,8 @@ def _get_image_series(nwb: pynwb.NWBFile) -> list[dict]:
408408
if isinstance(ob, pynwb.image.ImageSeries) and ob.external_file is not None:
409409
out_dict = dict(id=ob.object_id, name=ob.name, external_files=[])
410410
for ext_file in ob.external_file:
411-
if Path(ext_file).suffix in VIDEO_FILE_EXTENSIONS:
412-
out_dict["external_files"].append(Path(ext_file))
411+
if (path := PurePosixPath(ext_file)).suffix in VIDEO_FILE_EXTENSIONS:
412+
out_dict["external_files"].append(path)
413413
else:
414414
lgr.warning(
415415
"external file %s should be one of: %s",

dandi/tests/test_organize.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,10 @@ def test_video_organize(
353353
for ext_file_ob in ext_file_objects:
354354
for no, name in enumerate(ext_file_ob["external_files"]):
355355
video_files_organized.append(name)
356+
# Regression: external_file paths are DANDI/S3 keys,
357+
# must use forward slashes even on Windows.
358+
# See https://github.com/catalystneuro/nwb-video-widgets/issues/33
359+
assert "\\" not in str(name)
356360
# check if external_file arguments are correctly named according to convention:
357361
filename = Path(
358362
f"{vid_folder.name}/{ext_file_ob['id']}_external_file_{no}"

0 commit comments

Comments
 (0)