99''' Create filename pairs, triplets etc, with expected extensions '''
1010
1111import os
12- try :
13- basestring
14- except NameError :
15- basestring = str
12+ import pathlib
1613
1714
1815class TypesFilenamesError (Exception ):
1916 pass
2017
2118
19+ def _stringify_path (filepath_or_buffer ):
20+ """Attempt to convert a path-like object to a string.
21+
22+ Parameters
23+ ----------
24+ filepath_or_buffer : str or os.PathLike
25+
26+ Returns
27+ -------
28+ str_filepath_or_buffer : str
29+
30+ Notes
31+ -----
32+ Objects supporting the fspath protocol (python 3.6+) are coerced
33+ according to its __fspath__ method.
34+ For backwards compatibility with older pythons, pathlib.Path objects
35+ are specially coerced.
36+ Any other object is passed through unchanged, which includes bytes,
37+ strings, buffers, or anything else that's not even path-like.
38+
39+ Copied from:
40+ https://github.com/pandas-dev/pandas/blob/325dd686de1589c17731cf93b649ed5ccb5a99b4/pandas/io/common.py#L131-L160
41+ """
42+ if hasattr (filepath_or_buffer , "__fspath__" ):
43+ return filepath_or_buffer .__fspath__ ()
44+ elif isinstance (filepath_or_buffer , pathlib .Path ):
45+ return str (filepath_or_buffer )
46+ return filepath_or_buffer
47+
48+
2249def types_filenames (template_fname , types_exts ,
2350 trailing_suffixes = ('.gz' , '.bz2' ),
2451 enforce_extensions = True ,
@@ -31,7 +58,7 @@ def types_filenames(template_fname, types_exts,
3158
3259 Parameters
3360 ----------
34- template_fname : str
61+ template_fname : str or os.PathLike
3562 template filename from which to construct output dict of
3663 filenames, with given `types_exts` type to extension mapping. If
3764 ``self.enforce_extensions`` is True, then filename must have one
@@ -82,7 +109,8 @@ def types_filenames(template_fname, types_exts,
82109 >>> tfns == {'t1': '/path/test.funny', 't2': '/path/test.ext2'}
83110 True
84111 '''
85- if not isinstance (template_fname , basestring ):
112+ template_fname = _stringify_path (template_fname )
113+ if not isinstance (template_fname , str ):
86114 raise TypesFilenamesError ('Need file name as input '
87115 'to set_filenames' )
88116 if template_fname .endswith ('.' ):
@@ -151,7 +179,7 @@ def parse_filename(filename,
151179
152180 Parameters
153181 ----------
154- filename : str
182+ filename : str or os.PathLike
155183 filename in which to search for type extensions
156184 types_exts : sequence of sequences
157185 sequence of (name, extension) str sequences defining type to
@@ -190,6 +218,8 @@ def parse_filename(filename,
190218 >>> parse_filename('/path/fnameext2.gz', types_exts, ('.gz',))
191219 ('/path/fname', 'ext2', '.gz', 't2')
192220 '''
221+ filename = _stringify_path (filename )
222+
193223 ignored = None
194224 if match_case :
195225 endswith = _endswith
@@ -232,7 +262,7 @@ def splitext_addext(filename,
232262
233263 Parameters
234264 ----------
235- filename : str
265+ filename : str or os.PathLike
236266 filename that may end in any or none of `addexts`
237267 match_case : bool, optional
238268 If True, match case of `addexts` and `filename`, otherwise do
@@ -257,6 +287,8 @@ def splitext_addext(filename,
257287 >>> splitext_addext('fname.ext.foo', ('.foo', '.bar'))
258288 ('fname', '.ext', '.foo')
259289 '''
290+ filename = _stringify_path (filename )
291+
260292 if match_case :
261293 endswith = _endswith
262294 else :
0 commit comments