From fae3c7d813fa44cf8df0cd2ea6d12178bd381fd9 Mon Sep 17 00:00:00 2001 From: Amol Kahat Date: Tue, 7 Sep 2021 22:51:32 +0530 Subject: [PATCH] Modified shell_args plugin for nested lists. Shell args plugins is not supported for nested list items. Signed-off-by: Amol Kahat Change-Id: I51f6ae5f3d430e8f2c9e8cf47fbfbf5b50f84e3e --- plugins/filter/shell_args.py | 20 ++++++++++++++------ tests/plugins/filter/test_shell_args.py | 12 ++++++++++++ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/plugins/filter/shell_args.py b/plugins/filter/shell_args.py index 4c063bd..7e357ac 100644 --- a/plugins/filter/shell_args.py +++ b/plugins/filter/shell_args.py @@ -26,6 +26,13 @@ class FilterModule(object): 'shell_arg_list': self.shell_arg_list } + def _add_to_list(self, list_item, arg, parameter): + val = quote(arg) + if parameter: + list_item.append("{} {}".format(parameter, val)) + else: + list_item.append(val) + def shell_arg_list(self, arg, parameter=None): # Nothing was passed into this, just return an empty string if not arg: @@ -34,10 +41,11 @@ class FilterModule(object): arg = [arg] return_value = [] for a in arg: - if a: - val = quote(a) - if parameter: - return_value.append("{} {}".format(parameter, val)) - else: - return_value.append(val) + if isinstance(a, str) and a.strip(): + self._add_to_list(return_value, a, parameter) + elif isinstance(a, (list, tuple)): + # Deal with nested list items. + for item in a: + if item.strip(): + self._add_to_list(return_value, item, parameter) return ' '.join(return_value) diff --git a/tests/plugins/filter/test_shell_args.py b/tests/plugins/filter/test_shell_args.py index 2b7fb61..f8a1db9 100644 --- a/tests/plugins/filter/test_shell_args.py +++ b/tests/plugins/filter/test_shell_args.py @@ -59,3 +59,15 @@ class TestShellArgsFilters(tests_base.TestCase): expected = '-p a' self.assertEqual(expected, self.filter.shell_arg_list(arg, parameter='-p')) + + def test_shell_arg_nested_list(self): + arg = ['a', ['b', 'c'], 'd'] + expected = '-p a -p b -p c -p d' + self.assertEqual(expected, + self.filter.shell_arg_list(arg, parameter='-p')) + + def test_shell_args_empty_item(self): + arg = ['a', ['b', 'c', ''], 'd', ''] + expected = '-p a -p b -p c -p d' + self.assertEqual(expected, + self.filter.shell_arg_list(arg, parameter='-p'))