From 91761368eafa1c9dc093f307504227a1b5b0e941 Mon Sep 17 00:00:00 2001 From: Dorota Jarecka Date: Thu, 13 May 2021 19:06:46 -0400 Subject: [PATCH] fix to cmdline formatting for argstr with f-string --- pydra/engine/task.py | 5 +- .../engine/tests/test_shelltask_inputspec.py | 58 ++++++++++++++++++- 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/pydra/engine/task.py b/pydra/engine/task.py index 550bcf4f3a..ad4bc24ed6 100644 --- a/pydra/engine/task.py +++ b/pydra/engine/task.py @@ -433,7 +433,7 @@ def _command_pos_args(self, field, state_ind, index): argstr_f = argstr_formatting( argstr, self.inputs, value_updates={field.name: val} ) - argstr_formatted_l.append(argstr_f) + argstr_formatted_l.append(f" {argstr_f}") cmd_el_str = sep.join(argstr_formatted_l) else: # argstr has a simple form, e.g. "-f", or "--f" cmd_el_str = sep.join([f" {argstr} {val}" for val in value]) @@ -445,7 +445,8 @@ def _command_pos_args(self, field, state_ind, index): value = cmd_el_str # if argstr has a more complex form, with "{input_field}" if "{" in argstr and "}" in argstr: - cmd_el_str = argstr_formatting(argstr, self.inputs) + cmd_el_str = argstr.replace(f"{{{field.name}}}", str(value)) + cmd_el_str = argstr_formatting(cmd_el_str, self.inputs) else: # argstr has a simple form, e.g. "-f", or "--f" if value: cmd_el_str = f"{argstr} {value}" diff --git a/pydra/engine/tests/test_shelltask_inputspec.py b/pydra/engine/tests/test_shelltask_inputspec.py index daf7104c30..b423ece971 100644 --- a/pydra/engine/tests/test_shelltask_inputspec.py +++ b/pydra/engine/tests/test_shelltask_inputspec.py @@ -441,7 +441,35 @@ def test_shell_cmd_inputs_list_sep_2(): assert shelly.cmdline == "executable -v aaa,bbb,ccc" -def test_shell_cmd_inputs_sep_3(): +def test_shell_cmd_inputs_list_sep_2a(): + """providing list as an additional input:, sep, and argstr with f-string""" + my_input_spec = SpecInfo( + name="Input", + fields=[ + ( + "inpA", + attr.ib( + type=str, + metadata={ + "position": 1, + "help_string": "inpA", + "sep": ",", + "argstr": "-v {inpA}", + }, + ), + ) + ], + bases=(ShellSpec,), + ) + + shelly = ShellCommandTask( + executable="executable", inpA=["aaa", "bbb", "ccc"], input_spec=my_input_spec + ) + # a flag is used once + assert shelly.cmdline == "executable -v aaa,bbb,ccc" + + +def test_shell_cmd_inputs_list_sep_3(): """providing list as an additional input:, sep, argstr with ...""" my_input_spec = SpecInfo( name="Input", @@ -469,6 +497,34 @@ def test_shell_cmd_inputs_sep_3(): assert shelly.cmdline == "executable -v aaa, -v bbb, -v ccc" +def test_shell_cmd_inputs_list_sep_3a(): + """providing list as an additional input:, sep, argstr with ... and f-string""" + my_input_spec = SpecInfo( + name="Input", + fields=[ + ( + "inpA", + attr.ib( + type=str, + metadata={ + "position": 1, + "help_string": "inpA", + "sep": ",", + "argstr": "-v {inpA}...", + }, + ), + ) + ], + bases=(ShellSpec,), + ) + + shelly = ShellCommandTask( + executable="executable", inpA=["aaa", "bbb", "ccc"], input_spec=my_input_spec + ) + # a flag is repeated + assert shelly.cmdline == "executable -v aaa, -v bbb, -v ccc" + + def test_shell_cmd_inputs_sep_4(): """providing 1-el list as an additional input:, sep, argstr with ...,""" my_input_spec = SpecInfo(