Skip to content

Commit

Permalink
added error_if_empty parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
astewartau committed Oct 6, 2021
1 parent 8253d38 commit 97408d6
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions interfaces/nipype_interface_selectfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class SelectFiles(IOBase):
output_spec = DynamicTraitedSpec
_always_run = True

def __init__(self, templates, num_files=None, **kwargs):
def __init__(self, templates, num_files=None, error_if_empty=True, **kwargs):
"""Create an instance with specific input fields.
Parameters
Expand Down Expand Up @@ -115,6 +115,7 @@ def __init__(self, templates, num_files=None, **kwargs):
self._outfields = list(templates)
self._templates = templates
self._num_files = num_files
self._error_if_empty = error_if_empty

# Add the dynamic input fields
undefined_traits = {}
Expand Down Expand Up @@ -173,7 +174,7 @@ def _list_outputs(self):
filelist = glob.glob(filled_template)

# Handle the case where nothing matched
if not filelist:
if not filelist and self._error_if_empty:
msg = "No files were found matching %s template: %s" % (
field,
filled_template,
Expand All @@ -182,24 +183,26 @@ def _list_outputs(self):
raise IOError(msg)
else:
warn(msg)

# Possibly sort the list
if self.inputs.sort_filelist:
filelist = human_order_sorted(filelist)

# Handle whether this must be a list or not
if field not in force_lists:
filelist = simplify_list(filelist)

# Limit the number of files matched
if isinstance(filelist, list):
if self._num_files:
filelist = filelist[:self._num_files]
outputs['num_files'] = len(filelist)
if len(filelist) == 1:
filelist = filelist[0]
elif filelist:
outputs['num_files'] = 1
# Possibly sort the list
if self.inputs.sort_filelist:
filelist = human_order_sorted(filelist)

# Handle whether this must be a list or not
if field not in force_lists:
filelist = simplify_list(filelist)

# Limit the number of files matched
if isinstance(filelist, list):
if self._num_files:
filelist = filelist[:self._num_files]
outputs['num_files'] = len(filelist)
if len(filelist) == 1:
filelist = filelist[0]
elif filelist:
outputs['num_files'] = 1
else:
outputs['num_files'] = 0

outputs[field] = filelist

Expand Down

0 comments on commit 97408d6

Please sign in to comment.