Skip to content

Commit 185f82c

Browse files
committed
Refactor _stringify_path to filename_parser.py
1 parent aaf1a19 commit 185f82c

File tree

3 files changed

+30
-30
lines changed

3 files changed

+30
-30
lines changed

nibabel/filebasedimages.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@
1212
from copy import deepcopy
1313
from .fileholders import FileHolder
1414
from .filename_parser import (types_filenames, TypesFilenamesError,
15-
splitext_addext)
15+
splitext_addext, _stringify_path)
1616
from .openers import ImageOpener
1717
from .deprecated import deprecate_with_version
18-
from .loadsave import _stringify_path
1918

2019

2120
class ImageFileError(Exception):

nibabel/filename_parser.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,38 @@
1414
except NameError:
1515
basestring = str
1616

17-
from .loadsave import _stringify_path
17+
import pathlib
1818

1919
class TypesFilenamesError(Exception):
2020
pass
2121

22+
def _stringify_path(filepath_or_buffer):
23+
"""Attempt to convert a path-like object to a string.
24+
25+
Parameters
26+
----------
27+
filepath_or_buffer : object to be converted
28+
Returns
29+
-------
30+
str_filepath_or_buffer : maybe a string version of the object
31+
Notes
32+
-----
33+
Objects supporting the fspath protocol (python 3.6+) are coerced
34+
according to its __fspath__ method.
35+
For backwards compatibility with older pythons, pathlib.Path and
36+
py.path objects are specially coerced.
37+
Any other object is passed through unchanged, which includes bytes,
38+
strings, buffers, or anything else that's not even path-like.
39+
40+
Copied from:
41+
https://github.com/pandas-dev/pandas/blob/325dd686de1589c17731cf93b649ed5ccb5a99b4/pandas/io/common.py#L131-L160
42+
"""
43+
if hasattr(filepath_or_buffer, "__fspath__"):
44+
return filepath_or_buffer.__fspath__()
45+
elif isinstance(filepath_or_buffer, pathlib.Path):
46+
return str(filepath_or_buffer)
47+
return filepath_or_buffer
48+
2249

2350
def types_filenames(template_fname, types_exts,
2451
trailing_suffixes=('.gz', '.bz2'),

nibabel/loadsave.py

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,41 +11,15 @@
1111

1212
import os
1313
import numpy as np
14-
import pathlib
1514

16-
from .filename_parser import splitext_addext
15+
from .filename_parser import splitext_addext, _stringify_path
1716
from .openers import ImageOpener
1817
from .filebasedimages import ImageFileError
1918
from .imageclasses import all_image_classes
2019
from .arrayproxy import is_proxy
2120
from .deprecated import deprecate_with_version
2221

23-
def _stringify_path(filepath_or_buffer):
24-
"""Attempt to convert a path-like object to a string.
2522

26-
Parameters
27-
----------
28-
filepath_or_buffer : object to be converted
29-
Returns
30-
-------
31-
str_filepath_or_buffer : maybe a string version of the object
32-
Notes
33-
-----
34-
Objects supporting the fspath protocol (python 3.6+) are coerced
35-
according to its __fspath__ method.
36-
For backwards compatibility with older pythons, pathlib.Path and
37-
py.path objects are specially coerced.
38-
Any other object is passed through unchanged, which includes bytes,
39-
strings, buffers, or anything else that's not even path-like.
40-
41-
Copied from:
42-
https://github.com/pandas-dev/pandas/blob/325dd686de1589c17731cf93b649ed5ccb5a99b4/pandas/io/common.py#L131-L160
43-
"""
44-
if hasattr(filepath_or_buffer, "__fspath__"):
45-
return filepath_or_buffer.__fspath__()
46-
elif isinstance(filepath_or_buffer, pathlib.Path):
47-
return str(filepath_or_buffer)
48-
return filepath_or_buffer
4923

5024

5125
def load(filename, **kwargs):

0 commit comments

Comments
 (0)