From 3afea0c62952199f03f75fdb63d78e73baacf3f9 Mon Sep 17 00:00:00 2001 From: mathiasg Date: Thu, 27 Jul 2017 14:57:09 -0400 Subject: [PATCH 1/3] enh: selectdirs too --- nipype/interfaces/io.py | 22 ++++++++++++------- .../interfaces/tests/test_auto_SelectFiles.py | 2 ++ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/nipype/interfaces/io.py b/nipype/interfaces/io.py index 18e8047e1a..5dca91bdb0 100644 --- a/nipype/interfaces/io.py +++ b/nipype/interfaces/io.py @@ -1186,17 +1186,19 @@ def _list_outputs(self): class SelectFilesInputSpec(DynamicTraitedSpec, BaseInterfaceInputSpec): base_directory = Directory(exists=True, - desc="Root path common to templates.") + desc="Root path common to templates.") sort_filelist = traits.Bool(True, usedefault=True, - desc="When matching mutliple files, return them in sorted order.") + desc="When matching mutliple files, return them in sorted order.") raise_on_empty = traits.Bool(True, usedefault=True, - desc="Raise an exception if a template pattern matches no files.") + desc="Raise an exception if a template pattern matches no files.") force_lists = traits.Either(traits.Bool(), traits.List(Str()), - default=False, usedefault=True, - desc=("Whether to return outputs as a list even when only one file " - "matches the template. Either a boolean that applies to all " - "output fields or a list of output field names to coerce to " - " a list")) + default=False, usedefault=True, + desc=("Whether to return outputs as a list even when only one file " + "matches the template. Either a boolean that applies to all " + "output fields or a list of output field names to coerce to " + " a list")) + directory_mode = traits.Bool(False, usedefault=True, + desc="Return only directories.") class SelectFiles(IOBase): @@ -1303,6 +1305,10 @@ def _list_outputs(self): else: template = op.abspath(template) + if self.inputs.directory_mode: + # return only directories + template += os.sep + # Fill in the template and glob for files filled_template = template.format(**info) filelist = glob.glob(filled_template) diff --git a/nipype/interfaces/tests/test_auto_SelectFiles.py b/nipype/interfaces/tests/test_auto_SelectFiles.py index da119bfcf6..4b7aeb0fe3 100644 --- a/nipype/interfaces/tests/test_auto_SelectFiles.py +++ b/nipype/interfaces/tests/test_auto_SelectFiles.py @@ -5,6 +5,8 @@ def test_SelectFiles_inputs(): input_map = dict(base_directory=dict(), + directory_mode=dict(usedefault=True, + ), force_lists=dict(usedefault=True, ), ignore_exception=dict(nohash=True, From 992a81bc133e6cbd0de508821b8bd2e9eb3d8a14 Mon Sep 17 00:00:00 2001 From: mathiasg Date: Wed, 27 Sep 2017 16:17:27 -0400 Subject: [PATCH 2/3] bf: ensure template can end with os separator --- nipype/interfaces/io.py | 10 ++++++---- nipype/interfaces/tests/test_auto_SelectFiles.py | 2 -- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/nipype/interfaces/io.py b/nipype/interfaces/io.py index 155563a632..c03f0c9103 100644 --- a/nipype/interfaces/io.py +++ b/nipype/interfaces/io.py @@ -1197,8 +1197,6 @@ class SelectFilesInputSpec(DynamicTraitedSpec, BaseInterfaceInputSpec): "matches the template. Either a boolean that applies to all " "output fields or a list of output field names to coerce to " " a list")) - directory_mode = traits.Bool(False, usedefault=True, - desc="Return only directories.") class SelectFiles(IOBase): @@ -1298,6 +1296,10 @@ def _list_outputs(self): for field, template in list(self._templates.items()): + find_dirs = False + if template[-1] == os.sep: + find_dirs = True + # Build the full template path if isdefined(self.inputs.base_directory): template = op.abspath(op.join( @@ -1305,8 +1307,8 @@ def _list_outputs(self): else: template = op.abspath(template) - if self.inputs.directory_mode: - # return only directories + # re-add separator if searching exclusively for directories + if find_dirs: template += os.sep # Fill in the template and glob for files diff --git a/nipype/interfaces/tests/test_auto_SelectFiles.py b/nipype/interfaces/tests/test_auto_SelectFiles.py index 4b7aeb0fe3..da119bfcf6 100644 --- a/nipype/interfaces/tests/test_auto_SelectFiles.py +++ b/nipype/interfaces/tests/test_auto_SelectFiles.py @@ -5,8 +5,6 @@ def test_SelectFiles_inputs(): input_map = dict(base_directory=dict(), - directory_mode=dict(usedefault=True, - ), force_lists=dict(usedefault=True, ), ignore_exception=dict(nohash=True, From 06b45ee80ca8ddf4fd82f3d51418b347c4118f7a Mon Sep 17 00:00:00 2001 From: mathiasg Date: Wed, 27 Sep 2017 16:27:50 -0400 Subject: [PATCH 3/3] sty: cleanup --- nipype/interfaces/io.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/nipype/interfaces/io.py b/nipype/interfaces/io.py index c03f0c9103..4d3220b044 100644 --- a/nipype/interfaces/io.py +++ b/nipype/interfaces/io.py @@ -1186,17 +1186,20 @@ def _list_outputs(self): class SelectFilesInputSpec(DynamicTraitedSpec, BaseInterfaceInputSpec): base_directory = Directory(exists=True, - desc="Root path common to templates.") + desc="Root path common to templates.") sort_filelist = traits.Bool(True, usedefault=True, - desc="When matching mutliple files, return them in sorted order.") + desc="When matching mutliple files, return them" + " in sorted order.") raise_on_empty = traits.Bool(True, usedefault=True, - desc="Raise an exception if a template pattern matches no files.") + desc="Raise an exception if a template pattern " + "matches no files.") force_lists = traits.Either(traits.Bool(), traits.List(Str()), - default=False, usedefault=True, - desc=("Whether to return outputs as a list even when only one file " - "matches the template. Either a boolean that applies to all " - "output fields or a list of output field names to coerce to " - " a list")) + default=False, usedefault=True, + desc=("Whether to return outputs as a list even" + " when only one file matches the template. " + "Either a boolean that applies to all output " + "fields or a list of output field names to " + "coerce to a list")) class SelectFiles(IOBase): @@ -1296,9 +1299,7 @@ def _list_outputs(self): for field, template in list(self._templates.items()): - find_dirs = False - if template[-1] == os.sep: - find_dirs = True + find_dirs = template[-1] == os.sep # Build the full template path if isdefined(self.inputs.base_directory):