| 1 |
# Copyright (c) 2024, Alexei Znamensky <[email protected]> |
| 2 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
|
| 5 |
# Note that this module util is **PRIVATE** to the collection. It can have breaking changes at any time. |
| 6 |
# Do not use this from other collections or standalone plugins/modules! |
| 7 |
|
| 8 |
from __future__ import annotations |
| 9 |
|
| 10 |
import typing as t |
| 11 |
from functools import wraps |
| 12 |
|
| 13 |
from ansible.module_utils.common.collections import is_sequence |
| 14 |
|
| 15 |
if t.TYPE_CHECKING: |
| 16 |
from collections.abc import Callable, Mapping, Sequence |
| 17 |
|
| 18 |
ArgFormatType = Callable[[t.Any], Sequence[t.Any]] |
| 19 |
_T = t.TypeVar("_T") |
| 20 |
|
| 21 |
|
| 22 |
def _ensure_list(value: _T | Sequence[_T]) -> list[_T]: |
| 23 |
return list(value) if is_sequence(value) else [value] # type: ignore # TODO need type assertion for is_sequence |
| 24 |
|
| 25 |
|
| 26 |
class _ArgFormat: |
| 27 |
def __init__( |
| 28 |
self, |
| 29 |
func: ArgFormatType, |
| 30 |
ignore_none: bool | None = True, |
| 31 |
ignore_missing_value: bool = False, |
| 32 |
) -> None: |
| 33 |
self.func = func |
| 34 |
self.ignore_none = ignore_none |
| 35 |
self.ignore_missing_value = ignore_missing_value |
| 36 |
|
| 37 |
def __call__(self, value: t.Any | None) -> list[str]: |
| 38 |
ignore_none = self.ignore_none if self.ignore_none is not None else True |
| 39 |
if value is None and ignore_none: |
| 40 |
return [] |
| 41 |
f = self.func |
| 42 |
return [str(x) for x in f(value)] |
| 43 |
|
| 44 |
def __str__(self) -> str: |
| 45 |
return f"<ArgFormat: func={self.func}, ignore_none={self.ignore_none}, ignore_missing_value={self.ignore_missing_value}>" |
| 46 |
|
| 47 |
def __repr__(self) -> str: |
| 48 |
return str(self) |
| 49 |
|
| 50 |
|
| 51 |
def as_bool( |
| 52 |
args_true: Sequence[t.Any] | t.Any, |
| 53 |
args_false: Sequence[t.Any] | t.Any | None = None, |
| 54 |
ignore_none: bool | None = None, |
| 55 |
) -> _ArgFormat: |
| 56 |
if args_false is not None: |
| 57 |
if ignore_none is None: |
| 58 |
ignore_none = False |
| 59 |
else: |
| 60 |
args_false = [] |
| 61 |
return _ArgFormat( |
| 62 |
lambda value: _ensure_list(args_true) if value else _ensure_list(args_false), ignore_none=ignore_none |
| 63 |
) |
| 64 |
|
| 65 |
|
| 66 |
def as_bool_not(args: Sequence[t.Any] | t.Any) -> _ArgFormat: |
| 67 |
return as_bool([], args, ignore_none=False) |
| 68 |
|
| 69 |
|
| 70 |
def as_optval(arg, ignore_none: bool | None = None) -> _ArgFormat: |
| 71 |
return _ArgFormat(lambda value: [f"{arg}{value}"], ignore_none=ignore_none) |
| 72 |
|
| 73 |
|
| 74 |
def as_opt_val(arg: str, ignore_none: bool | None = None) -> _ArgFormat: |
| 75 |
return _ArgFormat(lambda value: [arg, value], ignore_none=ignore_none) |
| 76 |
|
| 77 |
|
| 78 |
def as_opt_eq_val(arg: str, ignore_none: bool | None = None) -> _ArgFormat: |
| 79 |
return _ArgFormat(lambda value: [f"{arg}={value}"], ignore_none=ignore_none) |
| 80 |
|
| 81 |
|
| 82 |
def as_list(ignore_none: bool | None = None, min_len: int = 0, max_len: int | None = None) -> _ArgFormat: |
| 83 |
def func(value: t.Any) -> list[t.Any]: |
| 84 |
value = _ensure_list(value) |
| 85 |
if len(value) < min_len: |
| 86 |
raise ValueError(f"Parameter must have at least {min_len} element(s)") |
| 87 |
if max_len is not None and len(value) > max_len: |
| 88 |
raise ValueError(f"Parameter must have at most {max_len} element(s)") |
| 89 |
return value |
| 90 |
|
| 91 |
return _ArgFormat(func, ignore_none=ignore_none) |
| 92 |
|
| 93 |
|
| 94 |
def as_fixed(*args: t.Any) -> _ArgFormat: |
| 95 |
if len(args) == 1 and is_sequence(args[0]): |
| 96 |
args = args[0] |
| 97 |
return _ArgFormat(lambda value: _ensure_list(args), ignore_none=False, ignore_missing_value=True) |
| 98 |
|
| 99 |
|
| 100 |
def as_func(func: ArgFormatType, ignore_none: bool | None = None) -> _ArgFormat: |
| 101 |
return _ArgFormat(func, ignore_none=ignore_none) |
| 102 |
|
| 103 |
|
| 104 |
def as_map( |
| 105 |
_map: Mapping[t.Any, Sequence[t.Any] | t.Any], |
| 106 |
default: Sequence[t.Any] | t.Any | None = None, |
| 107 |
ignore_none: bool | None = None, |
| 108 |
) -> _ArgFormat: |
| 109 |
if default is None: |
| 110 |
default = [] |
| 111 |
return _ArgFormat(lambda value: _ensure_list(_map.get(value, default)), ignore_none=ignore_none) |
| 112 |
|
| 113 |
|
| 114 |
def unpack_args(func): |
| 115 |
@wraps(func) |
| 116 |
def wrapper(v): |
| 117 |
return func(*v) |
| 118 |
|
| 119 |
return wrapper |
| 120 |
|
| 121 |
|
| 122 |
def unpack_kwargs(func): |
| 123 |
@wraps(func) |
| 124 |
def wrapper(v): |
| 125 |
return func(**v) |
| 126 |
|
| 127 |
return wrapper |
| 128 |
|
| 129 |
|
| 130 |
def stack(fmt): |
| 131 |
@wraps(fmt) |
| 132 |
def wrapper(*args, **kwargs): |
| 133 |
new_func = fmt(ignore_none=True, *args, **kwargs) |
| 134 |
|
| 135 |
def stacking(value): |
| 136 |
stack = [new_func(v) for v in value if v] |
| 137 |
stack = [x for args in stack for x in args] |
| 138 |
return stack |
| 139 |
|
| 140 |
return _ArgFormat(stacking, ignore_none=True) |
| 141 |
|
| 142 |
return wrapper |
| 143 |
|
| 144 |
|
| 145 |
def is_argformat(fmt: object) -> t.TypeGuard[_ArgFormat]: |
| 146 |
return isinstance(fmt, _ArgFormat) |