1
0
Fork 0

added an AnsibleParameter Type Alias and replaced all the old usage of _sdict with it

Dieser Commit ist enthalten in:
Sebastian Tobie 2023-04-23 10:03:10 +02:00
Ursprung 2e0ae0267f
Commit c72de48b62
1 geänderte Dateien mit 20 neuen und 24 gelöschten Zeilen

Datei anzeigen

@ -8,6 +8,7 @@ __all__ = (
"SYSTEMD_NETWORK_CONFIG", "SYSTEMD_NETWORK_CONFIG",
"SYSTEMD_CONFIG_ROOT", "SYSTEMD_CONFIG_ROOT",
"systemdbool", "systemdbool",
"AnsibleParameter",
) )
@ -15,10 +16,7 @@ SYSTEMD_CONFIG_ROOT = pathlib.Path("/etc/systemd")
SYSTEMD_NETWORK_CONFIG = SYSTEMD_CONFIG_ROOT / "network" SYSTEMD_NETWORK_CONFIG = SYSTEMD_CONFIG_ROOT / "network"
SYSTEMD_SERVICE_CONFIG = SYSTEMD_CONFIG_ROOT / "system" SYSTEMD_SERVICE_CONFIG = SYSTEMD_CONFIG_ROOT / "system"
AnsibleParameter = Dict[str, Any]
class _sdict(dict):
_help: Optional[str]
__name__: str
class _Type: class _Type:
@ -41,12 +39,12 @@ class _Type:
def list( def list(
self, self,
elements: Union[Type[object], str, dict], elements: Union[Type[object], str, AnsibleParameter],
required: bool = False, required: bool = False,
help: Optional[str] = None, help: Optional[str] = None,
) -> _sdict: ) -> AnsibleParameter:
option = _sdict(type="list", required=required) option: AnsibleParameter = dict(type="list", required=required)
if not isinstance(elements, (str, _sdict)): if not isinstance(elements, (str, dict)):
option["elements"] = elements.__name__ option["elements"] = elements.__name__
elif isinstance(elements, dict): elif isinstance(elements, dict):
option["elements"] = elements["type"] option["elements"] = elements["type"]
@ -54,22 +52,19 @@ class _Type:
option["options"] = dict() option["options"] = dict()
for name, value in elements["option"].items(): for name, value in elements["option"].items():
option["options"][name] = value option["options"][name] = value
if isinstance(value, _sdict) and value._help is not None: if "description" not in option["options"][name]:
option["options"][name]["description"] = value._help
elif "description" in option["options"][name]:
pass
else:
warnings.warn( warnings.warn(
"helptext of option {} is unset. Ansible requires suboptions to have an documentation".format(name), "helptext of option {} is unset. Ansible requires suboptions to have an documentation".format(name),
) )
if help is not None:
option._help = help option["description"] = help.split("\n")
return option return option
def dict(self, required: bool = False, help: Optional[str] = None, **options: dict) -> _sdict: def dict(self, required: bool = False, help: Optional[str] = None, **options: dict) -> AnsibleParameter:
option = _sdict(type="dict", required=required) option: AnsibleParameter = dict(type="dict", required=required)
option["option"] = options option["option"] = options
option._help = help if help is not None:
option["description"] = help.split("\n")
return option return option
def __getattr__(self, name: str): def __getattr__(self, name: str):
@ -78,15 +73,16 @@ class _Type:
help: Optional[str] = None, help: Optional[str] = None,
choices: Optional[Sequence] = None, choices: Optional[Sequence] = None,
default: Optional[Any] = None, default: Optional[Any] = None,
): ) -> AnsibleParameter:
"""Simple wrapper for Ansible {0} argument dict""" """Simple wrapper for Ansible {0} argument dict"""
output = _sdict(type=name, required=required) option: AnsibleParameter = dict(type=name, required=required)
if choices is not None: if choices is not None:
output["choices"] = choices option["choices"] = choices
if default is not None: if default is not None:
output["default"] = default option["default"] = default
output._help = help if help is not None:
return output option["description"] = help.split("\n")
return option
argument.__name__ = name argument.__name__ = name
argument.__doc__ = argument.__doc__.format(name) # type: ignore argument.__doc__ = argument.__doc__.format(name) # type: ignore