[PR #8415/7d72300c backport][stable-9] add cmd_runner_fmt.stack decorator (#8448)

add cmd_runner_fmt.stack decorator (#8415)

* add cmd_runner_fmt.stack decorator

* fix sanity

* fix typo

* add changelog frag

(cherry picked from commit 7d72300c36)

Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
This commit is contained in:
patchback[bot] 2024-06-01 22:54:14 +02:00 committed by GitHub
parent 1795a67b8e
commit 212871fcaf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 30 additions and 2 deletions

View file

@ -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):
"""