Skip to content
Snippets Groups Projects
Commit 5c4cf4c8 authored by Paul McCarthy's avatar Paul McCarthy
Browse files

Feels ugly to me. getFileGroups has the option to raise an error if the

path looks like part of an incomplete file group.
parent ea619b7f
No related branches found
No related tags found
No related merge requests found
...@@ -118,8 +118,8 @@ def imcp(src, ...@@ -118,8 +118,8 @@ def imcp(src,
# match the destination file type, # match the destination file type,
# we need to perform a conversion. # we need to perform a conversion.
# #
# This is expensive in terms of io # This is more expensive in terms of
# and cpu, but programmatically # io and cpu, but programmatically
# very easy - nibabel does all the # very easy - nibabel does all the
# hard work. # hard work.
if srcExt != destExt: if srcExt != destExt:
...@@ -138,7 +138,7 @@ def imcp(src, ...@@ -138,7 +138,7 @@ def imcp(src,
# Otherwise we do a file copy. This # Otherwise we do a file copy. This
# is actually more complicated than # is actually more complicated than
# conveting the file type due to # converting the file type due to
# hdr/img pairs ... # hdr/img pairs ...
# #
# If the source is part of a file group, # If the source is part of a file group,
...@@ -154,19 +154,19 @@ def imcp(src, ...@@ -154,19 +154,19 @@ def imcp(src,
# (base, ext) tuples, so we don't # (base, ext) tuples, so we don't
# have to re-split when creating # have to re-split when creating
# destination paths. # destination paths.
#
# The unambiguous flag tells getFileGroup
# to raise an error if the source appears
# to be part of an incopmlete file group
# (e.g. file.hdr without an accompanying
# file.img).
copySrcs = fslpath.getFileGroup(src, copySrcs = fslpath.getFileGroup(src,
fslimage.ALLOWED_EXTENSIONS, fslimage.ALLOWED_EXTENSIONS,
fslimage.FILE_GROUPS, fslimage.FILE_GROUPS,
fullPaths=False) fullPaths=False,
unambiguous=True)
copySrcs = [(srcBase, e) for e in copySrcs] copySrcs = [(srcBase, e) for e in copySrcs]
# Note that these additional files
# do not have to exist, e.g.
# imcp('blah.img', ...) will still
# work if there is no blah.hdr
copySrcs = [(b, e) for (b, e) in copySrcs if op.exists(b + e)]
# Build a list of destinations for each # Build a list of destinations for each
# copy source - we build this list in # copy source - we build this list in
# advance, so we can fail if any of the # advance, so we can fail if any of the
......
...@@ -226,7 +226,11 @@ def splitExt(filename, allowedExts=None): ...@@ -226,7 +226,11 @@ def splitExt(filename, allowedExts=None):
return filename[:-extLen], filename[-extLen:] return filename[:-extLen], filename[-extLen:]
def getFileGroup(path, allowedExts=None, fileGroups=None, fullPaths=True): def getFileGroup(path,
allowedExts=None,
fileGroups=None,
fullPaths=True,
unambiguous=False):
"""If the given ``path`` is part of a ``fileGroup``, returns a list """If the given ``path`` is part of a ``fileGroup``, returns a list
containing the paths to all other files in the group (including the containing the paths to all other files in the group (including the
``path`` itself). ``path`` itself).
...@@ -273,6 +277,11 @@ def getFileGroup(path, allowedExts=None, fileGroups=None, fullPaths=True): ...@@ -273,6 +277,11 @@ def getFileGroup(path, allowedExts=None, fileGroups=None, fullPaths=True):
:arg fullPaths: If ``True`` (the default), full file paths (relative to :arg fullPaths: If ``True`` (the default), full file paths (relative to
the ``path``) are returned. Otherwise, only the file the ``path``) are returned. Otherwise, only the file
extensions in the group are returned. extensions in the group are returned.
:arg unambiguous: Defaults to ``False``. If ``True``, and the path
is not unambiguouosly part of one group, or part of
no groups, a :exc:`PathError` is raised.
Otherwise, the path is returned.
""" """
path = addExt(path, allowedExts, mustExist=True, fileGroups=fileGroups) path = addExt(path, allowedExts, mustExist=True, fileGroups=fileGroups)
...@@ -284,6 +293,8 @@ def getFileGroup(path, allowedExts=None, fileGroups=None, fullPaths=True): ...@@ -284,6 +293,8 @@ def getFileGroup(path, allowedExts=None, fileGroups=None, fullPaths=True):
matchedGroups = [] matchedGroups = []
matchedGroupFiles = [] matchedGroupFiles = []
fullMatches = 0
partialMatches = 0
for group in fileGroups: for group in fileGroups:
...@@ -291,29 +302,52 @@ def getFileGroup(path, allowedExts=None, fileGroups=None, fullPaths=True): ...@@ -291,29 +302,52 @@ def getFileGroup(path, allowedExts=None, fileGroups=None, fullPaths=True):
continue continue
groupFiles = [base + s for s in group] groupFiles = [base + s for s in group]
exist = [op.exists(f) for f in groupFiles]
if not all([op.exists(f) for f in groupFiles]): if any(exist): partialMatches += 1
continue if all(exist): fullMatches += 1
else: continue
matchedGroups .append(group) matchedGroups .append(group)
matchedGroupFiles.append(groupFiles) matchedGroupFiles.append(groupFiles)
if len(matchedGroupFiles) == 1:
if fullPaths: return matchedGroupFiles[0]
else: return matchedGroups[ 0]
# Path is not part of any group # Path is not part of any group
elif len(matchedGroupFiles) == 0: if partialMatches == 0:
if fullPaths: return [path] if fullPaths: return [path]
else: return [ext] else: return [ext]
# If the given path is part of more # If the given path is part of more
# than one existing file group, we # than one existing file group, we
# can't resolve this ambiguity. # can't resolve this ambiguity.
else: if fullMatches > 1:
raise PathError('Path is part of multiple ' raise PathError('Path is part of multiple '
'file groups: {}'.format(path)) 'file groups: {}'.format(path))
# If the unambiguous flag is not set,
# we don't care about partial matches
if not unambiguous:
partialMatches = 0
# The path is unambiguously part of a
# complete file group - resolve it to
# the first element of the group
if fullMatches == 1 and partialMatches == 0:
if fullPaths: return matchedGroupFiles[0]
else: return matchedGroups[ 0]
# The path appears to be part of
# an incomplete group - this is
# potentially ambiguuuos, so give
# up (but see the partialMatches
# clobber above).
elif partialMatches > 0:
raise PathError('Path is part of an incomplete '
'file group: {}'.format(path))
else:
if fullPaths: return [path]
else: return [ext]
def removeDuplicates(paths, allowedExts=None, fileGroups=None): def removeDuplicates(paths, allowedExts=None, fileGroups=None):
"""Reduces the list of ``paths`` down to those which are unique with """Reduces the list of ``paths`` down to those which are unique with
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment