Skip to content

Commit 53b202b

Browse files
Merge pull request #1297 from zm711/get-io-fix
update get_io to return ios for files that do not exist yet
2 parents f608309 + 3a170db commit 53b202b

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

neo/io/__init__.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,8 +451,23 @@ def list_candidate_ios(file_or_folder, ignore_patterns=['*.ini', 'README.txt', '
451451
# if only file prefix was provided, e.g /mydatafolder/session1-
452452
# to select all files sharing the `session1-` prefix
453453
elif file_or_folder.parent.exists():
454-
filenames = file_or_folder.parent.glob(file_or_folder.name + '*')
455-
454+
filenames = list(file_or_folder.parent.glob(file_or_folder.name + '*'))
455+
# if filenames empty and suffix is provided then non-existent file
456+
# may be written in current dir. So run check for io
457+
if len(filenames)==0 and file_or_folder.suffix:
458+
suffix = file_or_folder.suffix[1:].lower()
459+
if suffix not in io_by_extension:
460+
raise ValueError(f'{suffix} is not a supported format of any IO.')
461+
return io_by_extension[suffix]
462+
463+
# If non-existent file in non-existent dir is given check if this
464+
# structure could be created with an io writing the file
465+
elif file_or_folder.suffix:
466+
suffix = file_or_folder.suffix[1:].lower()
467+
if suffix not in io_by_extension:
468+
raise ValueError(f'{suffix} is not a supported format of any IO.')
469+
return io_by_extension[suffix]
470+
456471
else:
457472
raise ValueError(f'{file_or_folder} does not contain data files of a supported format')
458473

neo/test/iotest/test_get_io.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from pathlib import Path
2+
from tempfile import TemporaryDirectory
3+
from neo.io import get_io, list_candidate_ios, NixIO
4+
5+
6+
def test_list_candidate_ios_non_existant_file():
7+
# use plexon io suffix for testing here
8+
non_existant_file = Path('non_existant_folder/non_existant_file.plx')
9+
non_existant_file.unlink(missing_ok=True)
10+
ios = list_candidate_ios(non_existant_file)
11+
12+
assert ios
13+
14+
# cleanup
15+
non_existant_file.unlink(missing_ok=True)
16+
17+
18+
def test_list_candidate_ios_filename_stub():
19+
# create dummy folder with dummy files
20+
with TemporaryDirectory(prefix='filename_stub_test_') as test_folder:
21+
test_folder = Path(test_folder)
22+
test_filename = (test_folder / 'dummy_file.nix')
23+
test_filename.touch()
24+
filename_stub = test_filename.with_suffix('')
25+
26+
# check that io is found even though file suffix was not provided
27+
ios = list_candidate_ios(filename_stub)
28+
29+
assert NixIO in ios
30+
31+
32+
def test_get_io_non_existant_file_writable_io():
33+
# use nixio for testing with writable io
34+
non_existant_file = Path('non_existant_file.nix')
35+
non_existant_file.unlink(missing_ok=True)
36+
io = get_io(non_existant_file)
37+
38+
assert isinstance(io, NixIO)
39+
40+
# cleanup
41+
non_existant_file.unlink(missing_ok=True)

0 commit comments

Comments
 (0)