diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 26860b5..df17d65 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -5,6 +5,16 @@ Sebastian.Base Release Notes .. contents:: Topics +v0.4.0 +====== + +Release Summary +--------------- + +to prevent empty sections, the install and header methods return None if the method would just the scetion +header + + v0.3.1 ====== diff --git a/changelogs/.plugin-cache.yaml b/changelogs/.plugin-cache.yaml index cacc114..4a4f7e7 100644 --- a/changelogs/.plugin-cache.yaml +++ b/changelogs/.plugin-cache.yaml @@ -16,4 +16,4 @@ plugins: strategy: {} test: {} vars: {} -version: 0.2.0 +version: 0.4.0 diff --git a/changelogs/changelog.yaml b/changelogs/changelog.yaml index 7a58d8b..cdcc781 100644 --- a/changelogs/changelog.yaml +++ b/changelogs/changelog.yaml @@ -14,3 +14,9 @@ releases: changes: release_summary: removed forgotten print calls release_date: "2024-02-24" + 0.4.0: + release_date: "2024-03-08" + changes: + release_summary: | + to prevent empty sections, the install and header methods return None if the method would just the scetion + header diff --git a/galaxy.yml b/galaxy.yml index 151cbcb..deeb99c 100644 --- a/galaxy.yml +++ b/galaxy.yml @@ -1,7 +1,7 @@ --- namespace: sebastian name: base -version: 0.3.1 +version: 0.4.0 readme: README.md @@ -9,7 +9,7 @@ authors: - Sebastian Tobie description: > The base of my ansible collections. It provides the nessesary tools for my modules -license_file: LICENSE +license_file: LICENSE.txt tags: - linux - systemd diff --git a/plugins/module_utils/generic.py b/plugins/module_utils/generic.py index 64819ea..e434a05 100644 --- a/plugins/module_utils/generic.py +++ b/plugins/module_utils/generic.py @@ -89,7 +89,8 @@ class meta(type): class Types(metaclass=meta): - def list( + + def list( # type: ignore[misc] elements: Union[Type[object], str, AnsibleParameter], required: bool = False, help: Optional[str] = None, @@ -119,7 +120,7 @@ class Types(metaclass=meta): option["description"] = help.split("\n") return option - def dict(required: bool = False, help: Optional[str] = None, **options: AnsibleParameter) -> AnsibleParameter: + def dict(required: bool = False, help: Optional[str] = None, **options: AnsibleParameter) -> AnsibleParameter: # type: ignore[misc] """Wrapper for the Ansible dict type Args: diff --git a/plugins/module_utils/module.py b/plugins/module_utils/module.py index b98ebab..48e4326 100644 --- a/plugins/module_utils/module.py +++ b/plugins/module_utils/module.py @@ -1,7 +1,6 @@ import pathlib from copy import deepcopy -from typing import (Any, Callable, ClassVar, Dict, NoReturn, Optional, Type, - TypeVar, Union, overload) +from typing import Any, Callable, ClassVar, Dict, NoReturn, Optional, Type, TypeVar, Union, overload import ansible.module_utils.basic as basic @@ -229,30 +228,37 @@ class SystemdUnitModule(AnsibleModule): ), ) #: if defined it will be called after run has changed the unitfile - post: Optional[Callable[[], None]] + post: Optional[Callable[[], None]] = None #: generates the install section if the unit is installable - install: ClassVar[Optional[Callable[["SystemdUnitModule"], str]]] + install: ClassVar[Optional[Callable[["SystemdUnitModule"], str | None]]] = None def unit(self) -> str: # pragma: nocover raise NotImplementedError() - def header(self) -> str: - header = "[Unit]\n" - header += "".join( - self.map_param( - description="Description", - documentation="Documentation", - requires="Requires", - wants="Wants", - partof="PartOf", - before="Before", - after="After", - ) + def header(self) -> Optional[str]: + parts = self.map_param( + description="Description", + documentation="Documentation", + requires="Requires", + wants="Wants", + partof="PartOf", + before="Before", + after="After", ) + if len(parts) == 0: + return None + header = "[Unit]\n" + "".join(parts) return header - def map_param(self, **parammap: str): + def _unit(self, *parts: Optional[str]) -> str: + opart = [] + for part in parts: + if part is not None: + opart.append(part) + return "\n".join(opart) + + def map_param(self, **parammap: str) -> list[str]: """maps an dict with keys for an section with given params. The key of the dictionary is the parameter and the value is the key in the unitfile. If an parameter has multiple values it adds multiple entries""" output: list[str] = [] for param, key in parammap.items(): @@ -264,7 +270,7 @@ class SystemdUnitModule(AnsibleModule): output.append(f"{key}={systemdbool(params)}\n") return output - def unitfile_gen(self): # pragma: nocover + def unitfile_gen(self) -> None: # pragma: nocover path = self.tmpdir / "newunit" with open(path, "w") as unit: unit.write(self.unit()) @@ -272,7 +278,7 @@ class SystemdUnitModule(AnsibleModule): self.module.set_group_if_different(path.as_posix(), "root", False) self.module.set_mode_if_different(path.as_posix(), "0644", False) if self.unitfile.exists(): - diff = dict() + diff: dict[str, str] = dict() self.changed = self.changed | self.module.set_owner_if_different( self.unitfile.as_posix(), "root", @@ -343,13 +349,15 @@ def installable(_class: Type[SystemdUnitModule]): # pragma: nocover ) specs["argument_spec"].update(arguments) - def install(self: SystemdUnitModule) -> str: - output = "[Install]\n" + def install(self: SystemdUnitModule) -> Optional[str]: + parts = [] for argument, key in _INSTALL_MAPPING.items(): if self.get(argument, False): for unit in self.get(argument): # type: ignore - output += "{}={}\n".format(key, unit) - return output + parts.append("{}={}\n".format(key, unit)) + if len(parts) == 0: + return None + return "[Install]\n" + "".join(parts) _class.install = install _class.module_spec = specs diff --git a/pyproject.toml b/pyproject.toml index 75cac91..f4c2199 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -146,10 +146,11 @@ exclude_lines = [ ] [tool.mypy] +disable_error_code = "import-untyped" + python_version = "3.11" warn_return_any = true warn_unused_configs = true - [[tool.mypy.overrides]] module = ["ansible", "ansible.module_utils", "ansible.module_utils.basic"] ignore_missing_imports = true diff --git a/src/ansible_module/__init__.py b/src/ansible_module/__init__.py index 260c070..6a9beea 100644 --- a/src/ansible_module/__init__.py +++ b/src/ansible_module/__init__.py @@ -1 +1 @@ -__version__ = "0.3.1" +__version__ = "0.4.0"