From c72de48b6248433431ff784e27877ba38cf69b2f Mon Sep 17 00:00:00 2001 From: Sebastian Tobie Date: Sun, 23 Apr 2023 10:03:10 +0200 Subject: [PATCH] added an AnsibleParameter Type Alias and replaced all the old usage of _sdict with it --- plugins/module_utils/generic.py | 44 +++++++++++++++------------------ 1 file changed, 20 insertions(+), 24 deletions(-) diff --git a/plugins/module_utils/generic.py b/plugins/module_utils/generic.py index 646c8f5..c5bd9f9 100644 --- a/plugins/module_utils/generic.py +++ b/plugins/module_utils/generic.py @@ -8,6 +8,7 @@ __all__ = ( "SYSTEMD_NETWORK_CONFIG", "SYSTEMD_CONFIG_ROOT", "systemdbool", + "AnsibleParameter", ) @@ -15,10 +16,7 @@ SYSTEMD_CONFIG_ROOT = pathlib.Path("/etc/systemd") SYSTEMD_NETWORK_CONFIG = SYSTEMD_CONFIG_ROOT / "network" SYSTEMD_SERVICE_CONFIG = SYSTEMD_CONFIG_ROOT / "system" - -class _sdict(dict): - _help: Optional[str] - __name__: str +AnsibleParameter = Dict[str, Any] class _Type: @@ -41,12 +39,12 @@ class _Type: def list( self, - elements: Union[Type[object], str, dict], + elements: Union[Type[object], str, AnsibleParameter], required: bool = False, help: Optional[str] = None, - ) -> _sdict: - option = _sdict(type="list", required=required) - if not isinstance(elements, (str, _sdict)): + ) -> AnsibleParameter: + option: AnsibleParameter = dict(type="list", required=required) + if not isinstance(elements, (str, dict)): option["elements"] = elements.__name__ elif isinstance(elements, dict): option["elements"] = elements["type"] @@ -54,22 +52,19 @@ class _Type: option["options"] = dict() for name, value in elements["option"].items(): option["options"][name] = value - if isinstance(value, _sdict) and value._help is not None: - option["options"][name]["description"] = value._help - elif "description" in option["options"][name]: - pass - else: + if "description" not in option["options"][name]: warnings.warn( "helptext of option {} is unset. Ansible requires suboptions to have an documentation".format(name), ) - - option._help = help + if help is not None: + option["description"] = help.split("\n") return option - def dict(self, required: bool = False, help: Optional[str] = None, **options: dict) -> _sdict: - option = _sdict(type="dict", required=required) + def dict(self, required: bool = False, help: Optional[str] = None, **options: dict) -> AnsibleParameter: + option: AnsibleParameter = dict(type="dict", required=required) option["option"] = options - option._help = help + if help is not None: + option["description"] = help.split("\n") return option def __getattr__(self, name: str): @@ -78,15 +73,16 @@ class _Type: help: Optional[str] = None, choices: Optional[Sequence] = None, default: Optional[Any] = None, - ): + ) -> AnsibleParameter: """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: - output["choices"] = choices + option["choices"] = choices if default is not None: - output["default"] = default - output._help = help - return output + option["default"] = default + if help is not None: + option["description"] = help.split("\n") + return option argument.__name__ = name argument.__doc__ = argument.__doc__.format(name) # type: ignore