Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add cmd_runner_fmt.stack decorator #8415

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/8415-cmd-runner-stack.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- cmd_runner module utils - add decorator ``cmd_runner_fmt.stack`` (https://github.com/ansible-collections/community.general/pull/8415).
25 changes: 24 additions & 1 deletion plugins/module_utils/cmd_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,23 @@ def __init__(self, func, ignore_none=None, ignore_missing_value=False):
self.ignore_none = ignore_none
self.ignore_missing_value = ignore_missing_value

def __call__(self, value, ctx_ignore_none):
def __call__(self, value, ctx_ignore_none=True):
ignore_none = self.ignore_none if self.ignore_none is not None else ctx_ignore_none
if value is None and ignore_none:
return []
f = self.func
return [str(x) for x in f(value)]

def __str__(self):
return "<ArgFormat: func={0}, ignore_none={1}, ignore_missing_value={2}>".format(
self.func,
self.ignore_none,
self.ignore_missing_value,
)

def __repr__(self):
return str(self)


class _Format(object):
@staticmethod
Expand Down Expand Up @@ -184,6 +194,19 @@ def wrapper(v):
return func(**v)
return wrapper

@staticmethod
def stack(fmt):
@wraps(fmt)
def wrapper(*args, **kwargs):
new_func = fmt(ignore_none=True, *args, **kwargs)

def stacking(value):
stack = [new_func(v) for v in value if v]
stack = [x for args in stack for x in args]
return stack
return _ArgFormat(stacking, ignore_none=True)
return wrapper


class CmdRunner(object):
"""
Expand Down
5 changes: 4 additions & 1 deletion tests/unit/plugins/module_utils/test_cmd_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
simple_fixed_false=(partial(cmd_runner_fmt.as_fixed, ["--always-here", "--forever"]), False, ["--always-here", "--forever"], None),
simple_fixed_none=(partial(cmd_runner_fmt.as_fixed, ["--always-here", "--forever"]), None, ["--always-here", "--forever"], None),
simple_fixed_str=(partial(cmd_runner_fmt.as_fixed, ["--always-here", "--forever"]), "something", ["--always-here", "--forever"], None),
stack_optval__str=(partial(cmd_runner_fmt.stack(cmd_runner_fmt.as_optval), "-t"), ["potatoes", "bananas"], ["-tpotatoes", "-tbananas"], None),
stack_opt_val__str=(partial(cmd_runner_fmt.stack(cmd_runner_fmt.as_opt_val), "-t"), ["potatoes", "bananas"], ["-t", "potatoes", "-t", "bananas"], None),
stack_opt_eq_val__int=(partial(cmd_runner_fmt.stack(cmd_runner_fmt.as_opt_eq_val), "--answer"), [42, 17], ["--answer=42", "--answer=17"], None),
)
if tuple(version_info) >= (3, 1):
from collections import OrderedDict
Expand All @@ -67,7 +70,7 @@
def test_arg_format(func, value, expected, exception):
fmt_func = func()
try:
actual = fmt_func(value, ctx_ignore_none=True)
actual = fmt_func(value)
print("formatted string = {0}".format(actual))
assert actual == expected, "actual = {0}".format(actual)
except Exception as e:
Expand Down
Loading