9
9
''' Create filename pairs, triplets etc, with expected extensions '''
10
10
11
11
import os
12
- try :
13
- basestring
14
- except NameError :
15
- basestring = str
12
+ import pathlib
16
13
17
14
18
15
class TypesFilenamesError (Exception ):
19
16
pass
20
17
21
18
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
+
22
49
def types_filenames (template_fname , types_exts ,
23
50
trailing_suffixes = ('.gz' , '.bz2' ),
24
51
enforce_extensions = True ,
@@ -31,7 +58,7 @@ def types_filenames(template_fname, types_exts,
31
58
32
59
Parameters
33
60
----------
34
- template_fname : str
61
+ template_fname : str or os.PathLike
35
62
template filename from which to construct output dict of
36
63
filenames, with given `types_exts` type to extension mapping. If
37
64
``self.enforce_extensions`` is True, then filename must have one
@@ -82,7 +109,8 @@ def types_filenames(template_fname, types_exts,
82
109
>>> tfns == {'t1': '/path/test.funny', 't2': '/path/test.ext2'}
83
110
True
84
111
'''
85
- if not isinstance (template_fname , basestring ):
112
+ template_fname = _stringify_path (template_fname )
113
+ if not isinstance (template_fname , str ):
86
114
raise TypesFilenamesError ('Need file name as input '
87
115
'to set_filenames' )
88
116
if template_fname .endswith ('.' ):
@@ -151,7 +179,7 @@ def parse_filename(filename,
151
179
152
180
Parameters
153
181
----------
154
- filename : str
182
+ filename : str or os.PathLike
155
183
filename in which to search for type extensions
156
184
types_exts : sequence of sequences
157
185
sequence of (name, extension) str sequences defining type to
@@ -190,6 +218,8 @@ def parse_filename(filename,
190
218
>>> parse_filename('/path/fnameext2.gz', types_exts, ('.gz',))
191
219
('/path/fname', 'ext2', '.gz', 't2')
192
220
'''
221
+ filename = _stringify_path (filename )
222
+
193
223
ignored = None
194
224
if match_case :
195
225
endswith = _endswith
@@ -232,7 +262,7 @@ def splitext_addext(filename,
232
262
233
263
Parameters
234
264
----------
235
- filename : str
265
+ filename : str or os.PathLike
236
266
filename that may end in any or none of `addexts`
237
267
match_case : bool, optional
238
268
If True, match case of `addexts` and `filename`, otherwise do
@@ -257,6 +287,8 @@ def splitext_addext(filename,
257
287
>>> splitext_addext('fname.ext.foo', ('.foo', '.bar'))
258
288
('fname', '.ext', '.foo')
259
289
'''
290
+ filename = _stringify_path (filename )
291
+
260
292
if match_case :
261
293
endswith = _endswith
262
294
else :
0 commit comments