Modified shell_args plugin for nested lists.

Shell args plugins is not supported for nested
list items.

Signed-off-by: Amol Kahat <amolkahat@gmail.com>
Change-Id: I51f6ae5f3d430e8f2c9e8cf47fbfbf5b50f84e3e
This commit is contained in:
Amol Kahat 2021-09-07 22:51:32 +05:30
parent 08560a8ae0
commit fae3c7d813
No known key found for this signature in database
GPG Key ID: FDD3BA6C832D7715
2 changed files with 26 additions and 6 deletions

View File

@ -26,6 +26,13 @@ class FilterModule(object):
'shell_arg_list': self.shell_arg_list '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): def shell_arg_list(self, arg, parameter=None):
# Nothing was passed into this, just return an empty string # Nothing was passed into this, just return an empty string
if not arg: if not arg:
@ -34,10 +41,11 @@ class FilterModule(object):
arg = [arg] arg = [arg]
return_value = [] return_value = []
for a in arg: for a in arg:
if a: if isinstance(a, str) and a.strip():
val = quote(a) self._add_to_list(return_value, a, parameter)
if parameter: elif isinstance(a, (list, tuple)):
return_value.append("{} {}".format(parameter, val)) # Deal with nested list items.
else: for item in a:
return_value.append(val) if item.strip():
self._add_to_list(return_value, item, parameter)
return ' '.join(return_value) return ' '.join(return_value)

View File

@ -59,3 +59,15 @@ class TestShellArgsFilters(tests_base.TestCase):
expected = '-p a' expected = '-p a'
self.assertEqual(expected, self.assertEqual(expected,
self.filter.shell_arg_list(arg, parameter='-p')) 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'))