mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-21 09:51:27 -07:00
add cmd_runner_fmt.stack decorator (#8415)
* add cmd_runner_fmt.stack decorator * fix sanity * fix typo * add changelog frag
This commit is contained in:
parent
e690317e3a
commit
7d72300c36
3 changed files with 30 additions and 2 deletions
|
@ -94,13 +94,23 @@ class _ArgFormat(object):
|
|||
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
|
||||
|
@ -184,6 +194,19 @@ class _Format(object):
|
|||
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):
|
||||
"""
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue