Skip to content

Commit 7856ad1

Browse files
authored
Merge pull request #1498 from zm711/tiny-doc-fix
Update `get_rawio` function with additional functionality
2 parents 908945a + 860472e commit 7856ad1

File tree

1 file changed

+47
-11
lines changed

1 file changed

+47
-11
lines changed

neo/rawio/__init__.py

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
88
Functions:
99
10-
.. autofunction:: neo.rawio.get_rawio_class
10+
.. autofunction:: neo.rawio.get_rawio
1111
1212
1313
Classes:
@@ -176,7 +176,8 @@
176176
177177
"""
178178

179-
import os
179+
from pathlib import Path
180+
from collections import Counter
180181

181182
from neo.rawio.alphaomegarawio import AlphaOmegaRawIO
182183
from neo.rawio.axographrawio import AxographRawIO
@@ -255,23 +256,58 @@ def get_rawio_class(filename_or_dirname):
255256

256257
import warnings
257258

258-
warnings.warn("get_rawio_class is deprecated. In the future please use get_rawio")
259+
warnings.warn(
260+
"get_rawio_class is deprecated and will be removed in 0.15.0. " "In the future please use `get_rawio`"
261+
)
259262

260263
return get_rawio(filename_or_dirname)
261264

262265

263-
def get_rawio(filename_or_dirname):
266+
def get_rawio(filename_or_dirname, exclusive_rawio: bool = True):
264267
"""
265268
Return a neo.rawio class guess from file extension.
269+
270+
Parameters
271+
----------
272+
filename_or_dirname : str | Path
273+
The filename or directory name to check for file suffixes that
274+
can be read by Neo. This can also be used to check whether a
275+
rawio could read a not-yet written file
276+
exclusive_rawio: bool, default: True
277+
Whether to return a rawio if there is only one rawio capable of
278+
reading the file. If this doesn't exist will return None.
279+
If set to False it will return all possible rawios organized
280+
by the most likely rawio.
281+
282+
Returns
283+
-------
284+
possibles: neo.RawIO | None | list[neo.RawIO]
285+
If exclusive_rawio is True, returns the single RawIO that
286+
can read a file/set of files or None. If exclusive_rawio is
287+
False it will return all possible RawIOs (organized by most likely)
288+
that could read the file or files.
266289
"""
267-
_, ext = os.path.splitext(filename_or_dirname)
268-
ext = ext[1:]
290+
filename_or_dirname = Path(filename_or_dirname)
291+
292+
# if filename_or_dirname doesn't exist then user may just be checking if
293+
# neo can read their file or they give a real file
294+
if not filename_or_dirname.exists() or filename_or_dirname.is_file():
295+
ext = Path(filename_or_dirname).suffix
296+
ext_list = [ext[1:]]
297+
else:
298+
ext_list = list({filename.suffix[1:] for filename in filename_or_dirname.glob('*') if filename.is_file()})
299+
269300
possibles = []
270-
for rawio in rawiolist:
271-
if any(ext.lower() == ext2.lower() for ext2 in rawio.extensions):
272-
possibles.append(rawio)
301+
for ext in ext_list:
302+
for rawio in rawiolist:
303+
if any(ext.lower() == ext2.lower() for ext2 in rawio.extensions):
304+
possibles.append(rawio)
273305

274-
if len(possibles) == 1:
306+
if len(possibles) == 1 and exclusive_rawio:
275307
return possibles[0]
276-
else:
308+
elif exclusive_rawio:
277309
return None
310+
else:
311+
possibles = [io[0] for io in Counter(possibles).most_common()]
312+
return possibles
313+

0 commit comments

Comments
 (0)