Skip to content

Commit 508dd80

Browse files
authored
Merge pull request #1260 from ReinderVosDeWael/feature/expand-path
ENH: Allow ~/ in paths
2 parents 4e7ad07 + cca2b4a commit 508dd80

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

nibabel/filename_parser.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from __future__ import annotations
1111

1212
import os
13+
import pathlib
1314
import typing as ty
1415

1516
if ty.TYPE_CHECKING: # pragma: no cover
@@ -37,9 +38,7 @@ def _stringify_path(filepath_or_buffer: FileSpec) -> str:
3738
Adapted from:
3839
https://github.com/pandas-dev/pandas/blob/325dd68/pandas/io/common.py#L131-L160
3940
"""
40-
if isinstance(filepath_or_buffer, os.PathLike):
41-
return filepath_or_buffer.__fspath__()
42-
return filepath_or_buffer
41+
return pathlib.Path(filepath_or_buffer).expanduser().as_posix()
4342

4443

4544
def types_filenames(

nibabel/freesurfer/tests/test_mghformat.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import io
1212
import os
13+
import pathlib
1314

1415
import numpy as np
1516
import pytest
@@ -291,7 +292,7 @@ def test_mgh_load_fileobj():
291292
# pass the filename to the array proxy, please feel free to change this
292293
# test.
293294
img = MGHImage.load(MGZ_FNAME)
294-
assert img.dataobj.file_like == MGZ_FNAME
295+
assert pathlib.Path(img.dataobj.file_like) == pathlib.Path(MGZ_FNAME)
295296
# Check fileobj also passed into dataobj
296297
with ImageOpener(MGZ_FNAME) as fobj:
297298
contents = fobj.read()

nibabel/tests/test_ecat.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
99

1010
import os
11+
import pathlib
1112
import warnings
1213
from unittest import TestCase
1314

@@ -183,8 +184,8 @@ class TestEcatImage(TestCase):
183184
img = image_class.load(example_file)
184185

185186
def test_file(self):
186-
assert self.img.file_map['header'].filename == self.example_file
187-
assert self.img.file_map['image'].filename == self.example_file
187+
assert pathlib.Path(self.img.file_map['header'].filename) == pathlib.Path(self.example_file)
188+
assert pathlib.Path(self.img.file_map['image'].filename) == pathlib.Path(self.example_file)
188189

189190
def test_save(self):
190191
tmp_file = 'tinypet_tmp.v'

nibabel/tests/test_filename_parser.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
#
88
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
99
"""Tests for filename container"""
10+
import pathlib
1011

1112
import pytest
1213

13-
from ..filename_parser import TypesFilenamesError, parse_filename, splitext_addext, types_filenames
14+
from ..filename_parser import TypesFilenamesError, parse_filename, splitext_addext, types_filenames, _stringify_path
1415

1516

1617
def test_filenames():
@@ -123,3 +124,19 @@ def test_splitext_addext():
123124
assert res == ('..', '', '')
124125
res = splitext_addext('...')
125126
assert res == ('...', '', '')
127+
128+
129+
def test__stringify_path():
130+
res = _stringify_path('fname.ext.gz')
131+
assert res == 'fname.ext.gz'
132+
res = _stringify_path(pathlib.Path('fname.ext.gz'))
133+
assert res == 'fname.ext.gz'
134+
135+
home = pathlib.Path.home().as_posix()
136+
res = _stringify_path(pathlib.Path('~/fname.ext.gz'))
137+
assert res == f'{home}/fname.ext.gz'
138+
139+
res = _stringify_path(pathlib.Path('./fname.ext.gz'))
140+
assert res == 'fname.ext.gz'
141+
res = _stringify_path(pathlib.Path('../fname.ext.gz'))
142+
assert res == '../fname.ext.gz'

0 commit comments

Comments
 (0)