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:
Alexei Znamensky 2024-06-02 08:44:50 +12:00 committed by GitHub
parent e690317e3a
commit 7d72300c36
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):
"""