diff --git a/fsl/data/image.py b/fsl/data/image.py index 152cfa133447d48784411e7b71f740c7fa5d65d7..2cc88b84040a5d4c85b6740bdc1efb974e39c621 100644 --- a/fsl/data/image.py +++ b/fsl/data/image.py @@ -1319,15 +1319,16 @@ def looksLikeImage(filename, allowedExts=None): return any([filename.endswith(ext) for ext in allowedExts]) -def addExt(prefix, mustExist=True): +def addExt(prefix, mustExist=True, unambiguous=True): """Adds a file extension to the given file ``prefix``. See :func:`~fsl.utils.path.addExt`. """ return fslpath.addExt(prefix, - ALLOWED_EXTENSIONS, - mustExist, - defaultExt(), - fileGroups=FILE_GROUPS) + allowedExts=ALLOWED_EXTENSIONS, + mustExist=mustExist, + defaultExt=defaultExt(), + fileGroups=FILE_GROUPS, + unambiguous=unambiguous) def splitExt(filename): diff --git a/fsl/utils/path.py b/fsl/utils/path.py index bccce3d27c063fea2725a839681a5c7d889d7210..ff1ad7b2c86822e03d67ff5b3881b06a76033c60 100644 --- a/fsl/utils/path.py +++ b/fsl/utils/path.py @@ -81,7 +81,8 @@ def addExt(prefix, allowedExts=None, mustExist=True, defaultExt=None, - fileGroups=None): + fileGroups=None, + unambiguous=True): """Adds a file extension to the given file ``prefix``. If ``mustExist`` is False, and the file does not already have a @@ -95,8 +96,8 @@ def addExt(prefix, - No files exist with the given prefix and a supported extension. - - ``fileGroups`` is ``None``, and more than one file exists with the - given prefix, and a supported extension. + - ``fileGroups is None`` and ``unambiguous is True``, and more than + one file exists with the given prefix, and a supported extension. Otherwise the full file name is returned. @@ -109,6 +110,11 @@ def addExt(prefix, :arg defaultExt: Default file extension to use. :arg fileGroups: Recognised file groups - see :func:`getFileGroup`. + + :arg unambiguous: If ``True`` (the default), and more than one file + exists with the specified ``prefix``, a + :exc:`PathError` is raised. Otherwise, a list + containing *all* matching files is returned. """ if allowedExts is None: allowedExts = [] @@ -151,8 +157,14 @@ def addExt(prefix, raise PathError('Could not find a supported file ' 'with prefix "{}"'.format(prefix)) - # Ambiguity! More than one supported - # file with the specified prefix. + # If ambiguity is ok, return + # all matching paths + elif not unambiguous: + return allPaths + + # Ambiguity is not ok! More than + # one supported file with the + # specified prefix. elif nexists > 1: # Remove non-existent paths from the