|
7 | 7 |
|
8 | 8 | Functions: |
9 | 9 |
|
10 | | -.. autofunction:: neo.rawio.get_rawio_class |
| 10 | +.. autofunction:: neo.rawio.get_rawio |
11 | 11 |
|
12 | 12 |
|
13 | 13 | Classes: |
|
176 | 176 |
|
177 | 177 | """ |
178 | 178 |
|
179 | | -import os |
| 179 | +from pathlib import Path |
| 180 | +from collections import Counter |
180 | 181 |
|
181 | 182 | from neo.rawio.alphaomegarawio import AlphaOmegaRawIO |
182 | 183 | from neo.rawio.axographrawio import AxographRawIO |
@@ -255,23 +256,58 @@ def get_rawio_class(filename_or_dirname): |
255 | 256 |
|
256 | 257 | import warnings |
257 | 258 |
|
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 | + ) |
259 | 262 |
|
260 | 263 | return get_rawio(filename_or_dirname) |
261 | 264 |
|
262 | 265 |
|
263 | | -def get_rawio(filename_or_dirname): |
| 266 | +def get_rawio(filename_or_dirname, exclusive_rawio: bool = True): |
264 | 267 | """ |
265 | 268 | 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. |
266 | 289 | """ |
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 | + |
269 | 300 | 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) |
273 | 305 |
|
274 | | - if len(possibles) == 1: |
| 306 | + if len(possibles) == 1 and exclusive_rawio: |
275 | 307 | return possibles[0] |
276 | | - else: |
| 308 | + elif exclusive_rawio: |
277 | 309 | return None |
| 310 | + else: |
| 311 | + possibles = [io[0] for io in Counter(possibles).most_common()] |
| 312 | + return possibles |
| 313 | + |
0 commit comments