From f2f35803377a75900838b3ff86f74ddb16ad6919 Mon Sep 17 00:00:00 2001 From: Sebastian Tobie Date: Sun, 23 Apr 2023 09:17:41 +0200 Subject: [PATCH] moved the doc generation to an recursive function --- plugins/module_utils/module.py | 48 ++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/plugins/module_utils/module.py b/plugins/module_utils/module.py index d8fe8f4..e2d7594 100644 --- a/plugins/module_utils/module.py +++ b/plugins/module_utils/module.py @@ -9,15 +9,43 @@ try: except ImportError: from plugins.module_utils.generic import Types, _sdict + __all__ = ( "AnsibleModule", "SystemdUnitModule", + "installable", + "SystemdReloadMixin", ) T = TypeVar("T") +def docify(input: Union[dict, _sdict]) -> dict: + options = dict() + for name, help in input.items(): + options[name] = dict(type=help["type"]) + if hasattr(help, "_help") and help._help is not None: + options[name]["description"] = help._help + if "description" in help and isinstance(help["description"], str): + options[name]["description"] = options[name]["description"].split("\n") + if "required" in help and help["required"]: + options[name]["required"] = True + else: + options[name]["required"] = False + if help["type"] == "list": + options[name]["elements"] = help["elements"] + if not options[name]["required"]: + options[name]["default"] = [] + if "default" in help: + options[name]["default"] = help["default"] + if "options" in help: + options[name]["options"] = docify(help["options"]) + if "choices" in help: + options[name]["choices"] = tuple(help["choices"]) + return options + + class AnsibleModule(object): """Simple wrapper for the basic.AnsibleModule""" @@ -149,31 +177,13 @@ class AnsibleModule(object): doc = cls.__doc__ if doc is None: doc = "" - options = dict() help: _sdict specs = dict() if "argument_spec" in cls._common_args: specs.update(cls._common_args["argument_spec"]) if "argument_spec" in cls.module_spec: specs.update(cls.module_spec["argument_spec"]) - for option, help in specs.items(): - options[option] = dict( - type=help["type"], - ) - if hasattr(help, "_help") and help._help is not None: - options[option]["description"] = help._help.split("\n") - if "required" in help and help["required"]: - options[option]["required"] = True - else: - options[option]["required"] = False - if help["type"] == "list": - options[option]["elements"] = help["elements"] - if not options[option]["required"]: - options[option]["default"] = [] - if "default" in help: - options[option]["default"] = help["default"] - if "choices" in help: - options[option]["choices"] = tuple(help["choices"]) + options = docify(specs) docu = doc.split("\n") return str( yaml.safe_dump(