Skip to content

ENH: Allow ~/ in paths #1260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions nibabel/filename_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from __future__ import annotations

import os
import pathlib
import typing as ty

if ty.TYPE_CHECKING: # pragma: no cover
Expand Down Expand Up @@ -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 pathlib.Path(filepath_or_buffer).expanduser().as_posix()


def types_filenames(
Expand Down
3 changes: 2 additions & 1 deletion nibabel/freesurfer/tests/test_mghformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import io
import os
import pathlib

import numpy as np
import pytest
Expand Down Expand Up @@ -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()
Expand Down
5 changes: 3 additions & 2 deletions nibabel/tests/test_ecat.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##

import os
import pathlib
import warnings
from unittest import TestCase

Expand Down Expand Up @@ -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'
Expand Down
19 changes: 18 additions & 1 deletion nibabel/tests/test_filename_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -123,3 +124,19 @@ def test_splitext_addext():
assert res == ('..', '', '')
res = splitext_addext('...')
assert res == ('...', '', '')


def test__stringify_path():
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().as_posix()
res = _stringify_path(pathlib.Path('~/fname.ext.gz'))
assert res == f'{home}/fname.ext.gz'

res = _stringify_path(pathlib.Path('./fname.ext.gz'))
assert res == 'fname.ext.gz'
res = _stringify_path(pathlib.Path('../fname.ext.gz'))
assert res == '../fname.ext.gz'