Skip to content

Commit

Permalink
Add docstrings and type hints to ansiblegalaxybase
Browse files Browse the repository at this point in the history
  • Loading branch information
Qalthos committed Nov 22, 2024
1 parent 66d85c8 commit 9a94964
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 39 deletions.
12 changes: 0 additions & 12 deletions .config/pydoclint-baseline.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
src/molecule/dependency/ansible_galaxy/base.py
DOC601: Class `AnsibleGalaxyBase`: Class docstring contains fewer class attributes than actual class attributes. (Please read https://jsh9.github.io/pydoclint/checking_class_attributes.html on how to correctly document class attributes.)
DOC603: Class `AnsibleGalaxyBase`: Class docstring attributes are different from actual class attributes. (Or could be other formatting issues: https://jsh9.github.io/pydoclint/violation_codes.html#notes-on-doc103 ). Attributes in the class definition but not in the docstring: [__metaclass__: ]. (Please read https://jsh9.github.io/pydoclint/checking_class_attributes.html on how to correctly document class attributes.)
DOC101: Method `AnsibleGalaxyBase.__init__`: Docstring contains fewer arguments than in function signature.
DOC106: Method `AnsibleGalaxyBase.__init__`: The option `--arg-type-hints-in-signature` is `True` but there are no argument type hints in the signature
DOC107: Method `AnsibleGalaxyBase.__init__`: The option `--arg-type-hints-in-signature` is `True` but not all args in the signature have type hints
DOC103: Method `AnsibleGalaxyBase.__init__`: Docstring arguments are different from function arguments. (Or could be other formatting issues: https://jsh9.github.io/pydoclint/violation_codes.html#notes-on-doc103 ). Arguments in the function signature but not in the docstring: [config: ].
DOC101: Method `AnsibleGalaxyBase.filter_options`: Docstring contains fewer arguments than in function signature.
DOC106: Method `AnsibleGalaxyBase.filter_options`: The option `--arg-type-hints-in-signature` is `True` but there are no argument type hints in the signature
DOC107: Method `AnsibleGalaxyBase.filter_options`: The option `--arg-type-hints-in-signature` is `True` but not all args in the signature have type hints
DOC103: Method `AnsibleGalaxyBase.filter_options`: Docstring arguments are different from function arguments. (Or could be other formatting issues: https://jsh9.github.io/pydoclint/violation_codes.html#notes-on-doc103 ). Arguments in the function signature but not in the docstring: [keys: , opts: ].
--------------------
src/molecule/dependency/ansible_galaxy/collections.py
DOC601: Class `Collections`: Class docstring contains fewer class attributes than actual class attributes. (Please read https://jsh9.github.io/pydoclint/checking_class_attributes.html on how to correctly document class attributes.)
DOC603: Class `Collections`: Class docstring attributes are different from actual class attributes. (Or could be other formatting issues: https://jsh9.github.io/pydoclint/violation_codes.html#notes-on-doc103 ). Attributes in the class definition but not in the docstring: [COMMANDS: , FILTER_OPTS: ]. (Please read https://jsh9.github.io/pydoclint/checking_class_attributes.html on how to correctly document class attributes.)
Expand Down
2 changes: 1 addition & 1 deletion src/molecule/dependency/ansible_galaxy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def default_env(self) -> dict[str, str]:
return env

@property
def default_options(self) -> dict[str, str]:
def default_options(self) -> dict[str, str | bool]:
"""Default options across all invokers.
Returns:
Expand Down
85 changes: 63 additions & 22 deletions src/molecule/dependency/ansible_galaxy/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,16 @@
import logging
import os

from typing import TYPE_CHECKING

from molecule import util
from molecule.dependency import base


if TYPE_CHECKING:
from molecule.config import Config


LOG = logging.getLogger(__name__)


Expand All @@ -39,39 +45,58 @@ class AnsibleGalaxyBase(base.Base):
FILTER_OPTS: Keys to remove from the dictionary returned by options().
"""

__metaclass__ = abc.ABCMeta

FILTER_OPTS = ()

def __init__(self, config) -> None: # type: ignore[no-untyped-def] # noqa: ANN001
"""Construct AnsibleGalaxy."""
def __init__(self, config: Config) -> None:
"""Construct AnsibleGalaxy.
Args:
config: Molecule Config instance.
"""
super().__init__(config)
self._sh_command = None

self.command = "ansible-galaxy"

@property
@abc.abstractmethod
def requirements_file(self): # type: ignore[no-untyped-def] # cover # noqa: ANN201, D102
pass
def requirements_file(self) -> str: # cover
"""Path to requirements file.
Returns:
Path to the requirements file for this dependency.
"""

@property
def default_options(self): # type: ignore[no-untyped-def] # noqa: ANN201, D102
d = {
def default_options(self) -> dict[str, str | bool]:
"""Default options for this dependency.
Returns:
Default options for this dependency.
"""
d: dict[str, str | bool] = {
"force": False,
}
if self._config.debug:
d["vvv"] = True

return d

def filter_options(self, opts, keys): # type: ignore[no-untyped-def] # noqa: ANN001, ANN201
def filter_options(
self,
opts: dict[str, str | bool],
keys: tuple[str, ...],
) -> dict[str, str | bool]:
"""Filter certain keys from a dictionary.
Removes all the values of ``keys`` from the dictionary ``opts``, if
they are present. Returns the resulting dictionary. Does not modify the
existing one.
Args:
opts: Options dictionary.
keys: Key names to exclude from opts.
Returns:
A copy of ``opts`` without the value of keys
"""
Expand All @@ -84,21 +109,32 @@ def filter_options(self, opts, keys): # type: ignore[no-untyped-def] # noqa: A
# NOTE(retr0h): Override the base classes' options() to handle
# ``ansible-galaxy`` one-off.
@property
def options(self): # type: ignore[no-untyped-def] # noqa: ANN201, D102
def options(self) -> dict[str, str | bool]:
"""Computed options for this dependency.
Returns:
Merged and filtered options for this dependency.
"""
o = self._config.config["dependency"]["options"]
# NOTE(retr0h): Remove verbose options added by the user while in
# debug.
if self._config.debug:
o = util.filter_verbose_permutation(o)

o = util.merge_dicts(self.default_options, o)
return self.filter_options(o, self.FILTER_OPTS) # type: ignore[no-untyped-call]
return self.filter_options(o, self.FILTER_OPTS)

@property
def default_env(self): # type: ignore[no-untyped-def] # noqa: ANN201, D102
return util.merge_dicts(os.environ, self._config.env)
def default_env(self) -> dict[str, str]:
"""Default environment variables for this dependency.
def bake(self): # type: ignore[no-untyped-def] # noqa: ANN201
Returns:
Default environment variables for this dependency.
"""
env = dict(os.environ)
return util.merge_dicts(env, self._config.env) # type: ignore[type-var]

def bake(self) -> None:
"""Bake an ``ansible-galaxy`` command so it's ready to execute and returns None."""
options = self.options
verbose_flag = util.verbose_flag(options)
Expand All @@ -110,26 +146,31 @@ def bake(self): # type: ignore[no-untyped-def] # noqa: ANN201
*verbose_flag,
]

def execute(self, action_args=None): # type: ignore[no-untyped-def] # noqa: ANN001, ANN201, ARG002, D102
def execute(self, action_args: list[str] | None = None) -> None: # noqa: ARG002
"""Execute dependency.
Args:
action_args: Arguments for this dependency. Unused.
"""
if not self.enabled:
msg = "Skipping, dependency is disabled."
LOG.warning(msg)
return
super().execute() # type: ignore[no-untyped-call]
super().execute()

if not self._has_requirements_file(): # type: ignore[no-untyped-call]
if not self._has_requirements_file():
msg = "Skipping, missing the requirements file."
LOG.warning(msg)
return

if self._sh_command is None:
self.bake() # type: ignore[no-untyped-call]
self.bake()

self._setup() # type: ignore[no-untyped-call]
self.execute_with_retries() # type: ignore[no-untyped-call]
self._setup()
self.execute_with_retries()

def _setup(self): # type: ignore[no-untyped-def] # noqa: ANN202
def _setup(self) -> None:
"""Prepare the system for using ``ansible-galaxy`` and returns None."""

def _has_requirements_file(self): # type: ignore[no-untyped-def] # noqa: ANN202
def _has_requirements_file(self) -> bool:
return os.path.isfile(self.requirements_file) # noqa: PTH113
6 changes: 3 additions & 3 deletions src/molecule/dependency/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def __init__(self, config: Config) -> None:
config: An instance of a Molecule config.
"""
self._config = config
self._sh_command: str | None = None
self._sh_command: str | list[str] | None = None

def execute_with_retries(self) -> None:
"""Run dependency downloads with retry and timed back-off."""
Expand Down Expand Up @@ -107,7 +107,7 @@ def execute(

@property
@abc.abstractmethod
def default_options(self) -> dict[str, str]: # pragma: no cover
def default_options(self) -> dict[str, str | bool]: # pragma: no cover
"""Get default CLI arguments provided to ``cmd``.
Returns:
Expand Down Expand Up @@ -145,7 +145,7 @@ def enabled(self) -> bool:
return self._config.config["dependency"]["enabled"]

@property
def options(self) -> dict[str, str]:
def options(self) -> dict[str, str | bool]:
"""Computed dependency options.
Returns:
Expand Down
2 changes: 1 addition & 1 deletion src/molecule/dependency/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def command(self) -> str:
return self._config.config["dependency"]["command"] or ""

@property
def default_options(self) -> dict[str, str]:
def default_options(self) -> dict[str, str | bool]:
"""Get default options for shell dependencies (none).
Returns:
Expand Down

0 comments on commit 9a94964

Please sign in to comment.