From f1f99014b8d0af47d8afcaf972107992644d43ef Mon Sep 17 00:00:00 2001 From: Reinder Vos de Wael Date: Fri, 29 Sep 2023 10:05:55 -0400 Subject: [PATCH 1/9] Allow relative and home paths --- nibabel/filename_parser.py | 5 ++--- nibabel/tests/test_filename_parser.py | 26 +++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/nibabel/filename_parser.py b/nibabel/filename_parser.py index 45c50d6830..b3b4f90ff2 100644 --- a/nibabel/filename_parser.py +++ b/nibabel/filename_parser.py @@ -10,6 +10,7 @@ from __future__ import annotations import os +import pathlib import typing as ty if ty.TYPE_CHECKING: # pragma: no cover @@ -37,9 +38,7 @@ def _stringify_path(filepath_or_buffer: FileSpec) -> str: Adapted from: https://github.com/pandas-dev/pandas/blob/325dd68/pandas/io/common.py#L131-L160 """ - if isinstance(filepath_or_buffer, os.PathLike): - return filepath_or_buffer.__fspath__() - return filepath_or_buffer + return str(pathlib.Path(filepath_or_buffer).expanduser().resolve()) def types_filenames( diff --git a/nibabel/tests/test_filename_parser.py b/nibabel/tests/test_filename_parser.py index 29da7b6f61..7d2d45eb25 100644 --- a/nibabel/tests/test_filename_parser.py +++ b/nibabel/tests/test_filename_parser.py @@ -7,10 +7,11 @@ # ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ## """Tests for filename container""" +import pathlib import pytest -from ..filename_parser import TypesFilenamesError, parse_filename, splitext_addext, types_filenames +from ..filename_parser import TypesFilenamesError, parse_filename, splitext_addext, types_filenames, _stringify_path def test_filenames(): @@ -123,3 +124,26 @@ def test_splitext_addext(): assert res == ('..', '', '') res = splitext_addext('...') assert res == ('...', '', '') + + +def test__stringify_path(): + current_directory = pathlib.Path.cwd() + res = _stringify_path('') + assert res == str(current_directory) + res = _stringify_path('fname.ext.gz') + assert res == str(current_directory / 'fname.ext.gz') + res = _stringify_path(pathlib.Path('fname.ext.gz')) + assert res == str(current_directory / 'fname.ext.gz') + + home = pathlib.Path.home() + res = _stringify_path(pathlib.Path('~/fname.ext.gz')) + assert res == str(home) + '/fname.ext.gz' + + res = _stringify_path(pathlib.Path('./fname.ext.gz')) + assert res == str(current_directory / 'fname.ext.gz') + res = _stringify_path(pathlib.Path('../fname.ext.gz')) + assert res == str(current_directory.parent / 'fname.ext.gz') + + + + From 891e462469d823b11426de77f92553f1fad6a0a5 Mon Sep 17 00:00:00 2001 From: Reinder Vos de Wael Date: Fri, 29 Sep 2023 10:17:12 -0400 Subject: [PATCH 2/9] Maintain relative path behavior --- nibabel/filename_parser.py | 2 +- nibabel/tests/test_filename_parser.py | 10 ++++------ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/nibabel/filename_parser.py b/nibabel/filename_parser.py index b3b4f90ff2..71e55854eb 100644 --- a/nibabel/filename_parser.py +++ b/nibabel/filename_parser.py @@ -38,7 +38,7 @@ def _stringify_path(filepath_or_buffer: FileSpec) -> str: Adapted from: https://github.com/pandas-dev/pandas/blob/325dd68/pandas/io/common.py#L131-L160 """ - return str(pathlib.Path(filepath_or_buffer).expanduser().resolve()) + return str(pathlib.Path(filepath_or_buffer).expanduser()) def types_filenames( diff --git a/nibabel/tests/test_filename_parser.py b/nibabel/tests/test_filename_parser.py index 7d2d45eb25..fe5249a8ab 100644 --- a/nibabel/tests/test_filename_parser.py +++ b/nibabel/tests/test_filename_parser.py @@ -128,21 +128,19 @@ def test_splitext_addext(): def test__stringify_path(): current_directory = pathlib.Path.cwd() - res = _stringify_path('') - assert res == str(current_directory) res = _stringify_path('fname.ext.gz') - assert res == str(current_directory / 'fname.ext.gz') + assert res == 'fname.ext.gz' res = _stringify_path(pathlib.Path('fname.ext.gz')) - assert res == str(current_directory / 'fname.ext.gz') + assert res == 'fname.ext.gz' home = pathlib.Path.home() res = _stringify_path(pathlib.Path('~/fname.ext.gz')) assert res == str(home) + '/fname.ext.gz' res = _stringify_path(pathlib.Path('./fname.ext.gz')) - assert res == str(current_directory / 'fname.ext.gz') + assert res == 'fname.ext.gz' res = _stringify_path(pathlib.Path('../fname.ext.gz')) - assert res == str(current_directory.parent / 'fname.ext.gz') + assert res == '../fname.ext.gz' From b70ce832a115af47bedbb661227ae9648aad5643 Mon Sep 17 00:00:00 2001 From: Reinder Vos de Wael Date: Fri, 29 Sep 2023 10:19:00 -0400 Subject: [PATCH 3/9] Push to trigger CI --- nibabel/filename_parser.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/nibabel/filename_parser.py b/nibabel/filename_parser.py index 71e55854eb..e25ea9e1d3 100644 --- a/nibabel/filename_parser.py +++ b/nibabel/filename_parser.py @@ -38,7 +38,8 @@ def _stringify_path(filepath_or_buffer: FileSpec) -> str: Adapted from: https://github.com/pandas-dev/pandas/blob/325dd68/pandas/io/common.py#L131-L160 """ - return str(pathlib.Path(filepath_or_buffer).expanduser()) + full_path = pathlib.Path(filepath_or_buffer).expanduser() + return str(full_path) def types_filenames( From e6e8d40c64c8ce978d9b7201efb2b1ce8755fe53 Mon Sep 17 00:00:00 2001 From: Reinder Vos de Wael Date: Fri, 29 Sep 2023 11:05:18 -0400 Subject: [PATCH 4/9] Restore forward slash behavior --- nibabel/filename_parser.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/nibabel/filename_parser.py b/nibabel/filename_parser.py index e25ea9e1d3..92a2f4b1f5 100644 --- a/nibabel/filename_parser.py +++ b/nibabel/filename_parser.py @@ -38,8 +38,7 @@ def _stringify_path(filepath_or_buffer: FileSpec) -> str: Adapted from: https://github.com/pandas-dev/pandas/blob/325dd68/pandas/io/common.py#L131-L160 """ - full_path = pathlib.Path(filepath_or_buffer).expanduser() - return str(full_path) + return pathlib.Path(filepath_or_buffer).expanduser().as_posix() def types_filenames( From 47f4dccdfd29150a1fedc4179d91506e92752935 Mon Sep 17 00:00:00 2001 From: Reinder Vos de Wael Date: Fri, 29 Sep 2023 11:48:47 -0400 Subject: [PATCH 5/9] Ensure posix test strings --- nibabel/tests/test_filename_parser.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nibabel/tests/test_filename_parser.py b/nibabel/tests/test_filename_parser.py index fe5249a8ab..963f7cc624 100644 --- a/nibabel/tests/test_filename_parser.py +++ b/nibabel/tests/test_filename_parser.py @@ -127,13 +127,13 @@ def test_splitext_addext(): def test__stringify_path(): - current_directory = pathlib.Path.cwd() + current_directory = pathlib.Path.cwd().as_posix() res = _stringify_path('fname.ext.gz') assert res == 'fname.ext.gz' res = _stringify_path(pathlib.Path('fname.ext.gz')) assert res == 'fname.ext.gz' - home = pathlib.Path.home() + home = pathlib.Path.home().as_posix() res = _stringify_path(pathlib.Path('~/fname.ext.gz')) assert res == str(home) + '/fname.ext.gz' From 54ad8596b39777cda2d62f6d5a4233d0314c5953 Mon Sep 17 00:00:00 2001 From: Reinder Vos de Wael Date: Fri, 29 Sep 2023 12:38:24 -0400 Subject: [PATCH 6/9] Update nibabel/tests/test_filename_parser.py Co-authored-by: Chris Markiewicz --- nibabel/tests/test_filename_parser.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/nibabel/tests/test_filename_parser.py b/nibabel/tests/test_filename_parser.py index 963f7cc624..f37b3713b8 100644 --- a/nibabel/tests/test_filename_parser.py +++ b/nibabel/tests/test_filename_parser.py @@ -141,7 +141,3 @@ def test__stringify_path(): assert res == 'fname.ext.gz' res = _stringify_path(pathlib.Path('../fname.ext.gz')) assert res == '../fname.ext.gz' - - - - From 6bf8c890cbd2eb7afc1c68d4fa64bfbcbcc23859 Mon Sep 17 00:00:00 2001 From: Reinder Vos de Wael Date: Tue, 3 Oct 2023 15:23:08 -0400 Subject: [PATCH 7/9] Assert equal pathlib.Path instead of str --- nibabel/freesurfer/tests/test_mghformat.py | 3 ++- nibabel/tests/test_ecat.py | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/nibabel/freesurfer/tests/test_mghformat.py b/nibabel/freesurfer/tests/test_mghformat.py index ded1aca8a2..5a400119ba 100644 --- a/nibabel/freesurfer/tests/test_mghformat.py +++ b/nibabel/freesurfer/tests/test_mghformat.py @@ -10,6 +10,7 @@ import io import os +import pathlib import numpy as np import pytest @@ -291,7 +292,7 @@ def test_mgh_load_fileobj(): # pass the filename to the array proxy, please feel free to change this # test. img = MGHImage.load(MGZ_FNAME) - assert img.dataobj.file_like == MGZ_FNAME + assert pathlib.Path(img.dataobj.file_like) == pathlib.Path(MGZ_FNAME) # Check fileobj also passed into dataobj with ImageOpener(MGZ_FNAME) as fobj: contents = fobj.read() diff --git a/nibabel/tests/test_ecat.py b/nibabel/tests/test_ecat.py index ff74b7b084..c8de98c2d1 100644 --- a/nibabel/tests/test_ecat.py +++ b/nibabel/tests/test_ecat.py @@ -8,6 +8,7 @@ ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ## import os +import pathlib import warnings from unittest import TestCase @@ -183,8 +184,8 @@ class TestEcatImage(TestCase): img = image_class.load(example_file) def test_file(self): - assert self.img.file_map['header'].filename == self.example_file - assert self.img.file_map['image'].filename == self.example_file + assert pathlib.Path(self.img.file_map['header'].filename) == pathlib.Path(self.example_file) + assert pathlib.Path(self.img.file_map['image'].filename) == pathlib.Path(self.example_file) def test_save(self): tmp_file = 'tinypet_tmp.v' From 77aca5f6c2345ea6c3cbbcd9f0c4a137271a4676 Mon Sep 17 00:00:00 2001 From: Reinder Vos de Wael Date: Wed, 4 Oct 2023 12:34:17 -0400 Subject: [PATCH 8/9] Update nibabel/tests/test_filename_parser.py Co-authored-by: Chris Markiewicz --- nibabel/tests/test_filename_parser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nibabel/tests/test_filename_parser.py b/nibabel/tests/test_filename_parser.py index f37b3713b8..735529e713 100644 --- a/nibabel/tests/test_filename_parser.py +++ b/nibabel/tests/test_filename_parser.py @@ -135,7 +135,7 @@ def test__stringify_path(): home = pathlib.Path.home().as_posix() res = _stringify_path(pathlib.Path('~/fname.ext.gz')) - assert res == str(home) + '/fname.ext.gz' + assert res == f'{home}/fname.ext.gz' res = _stringify_path(pathlib.Path('./fname.ext.gz')) assert res == 'fname.ext.gz' From cca2b4acad5166e21abd18100300dfc053096647 Mon Sep 17 00:00:00 2001 From: Reinder Vos de Wael Date: Wed, 4 Oct 2023 12:34:39 -0400 Subject: [PATCH 9/9] Update nibabel/tests/test_filename_parser.py Co-authored-by: Chris Markiewicz --- nibabel/tests/test_filename_parser.py | 1 - 1 file changed, 1 deletion(-) diff --git a/nibabel/tests/test_filename_parser.py b/nibabel/tests/test_filename_parser.py index 735529e713..736994e0da 100644 --- a/nibabel/tests/test_filename_parser.py +++ b/nibabel/tests/test_filename_parser.py @@ -127,7 +127,6 @@ def test_splitext_addext(): def test__stringify_path(): - current_directory = pathlib.Path.cwd().as_posix() res = _stringify_path('fname.ext.gz') assert res == 'fname.ext.gz' res = _stringify_path(pathlib.Path('fname.ext.gz'))