Skip to content

Commit

Permalink
Add docstrings and type hints to delegated driver.
Browse files Browse the repository at this point in the history
  • Loading branch information
Qalthos committed Nov 21, 2024
1 parent c0b6da2 commit 62c01f5
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 33 deletions.
8 changes: 0 additions & 8 deletions .config/pydoclint-baseline.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,6 @@ src/molecule/dependency/shell.py
DOC107: Method `Shell.__init__`: The option `--arg-type-hints-in-signature` is `True` but not all args in the signature have type hints
DOC103: Method `Shell.__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: ].
--------------------
src/molecule/driver/delegated.py
DOC601: Class `Delegated`: 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 `Delegated`: 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: [title: ]. (Please read https://jsh9.github.io/pydoclint/checking_class_attributes.html on how to correctly document class attributes.)
DOC101: Method `Delegated.__init__`: Docstring contains fewer arguments than in function signature.
DOC106: Method `Delegated.__init__`: The option `--arg-type-hints-in-signature` is `True` but there are no argument type hints in the signature
DOC107: Method `Delegated.__init__`: The option `--arg-type-hints-in-signature` is `True` but not all args in the signature have type hints
DOC103: Method `Delegated.__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: ].
--------------------
src/molecule/model/schema_v3.py
DOC101: Function `validate`: Docstring contains fewer arguments than in function signature.
DOC106: Function `validate`: The option `--arg-type-hints-in-signature` is `True` but there are no argument type hints in the signature
Expand Down
107 changes: 83 additions & 24 deletions src/molecule/driver/delegated.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,17 @@
import logging
import os

from typing import TYPE_CHECKING, Any

from molecule import util
from molecule.api import Driver
from molecule.data import __file__ as data_module


if TYPE_CHECKING:
from molecule.config import Config


LOG = logging.getLogger(__name__)


Expand Down Expand Up @@ -138,25 +144,47 @@ class Delegated(Driver):
ansible_connection_options:
ansible_connection: local
```
Attributes:
title: Short description of the driver.
"""

title = "Default driver, user is expected to manage provisioning of test resources."

def __init__(self, config=None) -> None: # type: ignore[no-untyped-def] # noqa: ANN001
"""Construct Delegated."""
def __init__(self, config: Config) -> None:
"""Construct Delegated.
Args:
config: An instance of a Molecule config.
"""
super().__init__(config)
self._name = "default"

@property
def name(self): # type: ignore[no-untyped-def] # noqa: ANN201, D102
def name(self) -> str:
"""Name of the driver.
Returns:
Name of the driver.
"""
return self._name

@name.setter
def name(self, value): # type: ignore[no-untyped-def] # noqa: ANN001, ANN202
def name(self, value: str) -> None:
"""Driver name setter.
Args:
value: New name of the driver.
"""
self._name = value

@property
def login_cmd_template(self): # type: ignore[no-untyped-def] # noqa: ANN201, D102
def login_cmd_template(self) -> str:
"""Get the login command template to be populated by ``login_options`` as a string.
Returns:
The login command template, if any.
"""
if "login_cmd_template" in self.options:
return self.options["login_cmd_template"]

Expand All @@ -170,36 +198,62 @@ def login_cmd_template(self): # type: ignore[no-untyped-def] # noqa: ANN201, D
"-i {identity_file} "
f"{connection_options}"
)
return None
return ""

Check warning on line 201 in src/molecule/driver/delegated.py

View check run for this annotation

Codecov / codecov/patch

src/molecule/driver/delegated.py#L201

Added line #L201 was not covered by tests

@property
def default_safe_files(self): # type: ignore[no-untyped-def] # noqa: ANN201, D102
def default_safe_files(self) -> list[str]:
"""Generate files to be preserved.
Returns:
List of files to be preserved.
"""
return []

@property
def default_ssh_connection_options(self): # type: ignore[no-untyped-def] # noqa: ANN201, D102
def default_ssh_connection_options(self) -> list[str]:
"""SSH client options.
Returns:
List of SSH connection options.
"""
if self.managed:
ssh_connopts = self._get_ssh_connection_options() # type: ignore[no-untyped-call]
if self.options.get("ansible_connection_options", {}).get(
ssh_connopts = self._get_ssh_connection_options()
if config_connopts := self.options.get("ansible_connection_options", {}).get(
"ansible_ssh_common_args",
None,
):
ssh_connopts.append(
self.options.get("ansible_connection_options").get(
"ansible_ssh_common_args",
),
config_connopts,
)
return ssh_connopts
return []

def login_options(self, instance_name): # type: ignore[no-untyped-def] # noqa: ANN001, ANN201, D102
def login_options(self, instance_name: str) -> dict[str, str]:
"""Login options.
Args:
instance_name: The name of the instance to look up login options for.
Returns:
Dictionary of options related to logging into the instance.
"""
if self.managed:
d = {"instance": instance_name}

return util.merge_dicts(d, self._get_instance_config(instance_name)) # type: ignore[no-untyped-call]
return util.merge_dicts(d, self._get_instance_config(instance_name))
return {"instance": instance_name}

def ansible_connection_options(self, instance_name): # type: ignore[no-untyped-def] # noqa: ANN001, ANN201, D102
def ansible_connection_options(
self,
instance_name: str,
) -> dict[str, str]:
"""Ansible connection options.
Args:
instance_name: The name of the instance to look up Ansible connection options for.
Returns:
Dictionary of options related to ansible connection to the instance.
"""
# list of tuples describing mappable instance params and default values
instance_params = [
("become_pass", None),
Expand All @@ -213,7 +267,7 @@ def ansible_connection_options(self, instance_name): # type: ignore[no-untyped-
]
if self.managed:
try:
d = self._get_instance_config(instance_name) # type: ignore[no-untyped-call]
d = self._get_instance_config(instance_name)
conn_dict = {}
# Check if optional mappable params are in the instance config
for i in instance_params:
Expand Down Expand Up @@ -246,19 +300,24 @@ def ansible_connection_options(self, instance_name): # type: ignore[no-untyped-
return {}
return self.options.get("ansible_connection_options", {})

def _created(self): # type: ignore[no-untyped-def] # noqa: ANN202
def _created(self) -> str:
if self.managed:
return super()._created() # type: ignore[no-untyped-call]
return super()._created()
return "unknown"

def _get_instance_config(self, instance_name): # type: ignore[no-untyped-def] # noqa: ANN001, ANN202
def _get_instance_config(self, instance_name: str) -> dict[str, Any]:
instance_config_dict = util.safe_load_file(self._config.driver.instance_config)

return next(item for item in instance_config_dict if item["instance"] == instance_name)

def sanity_checks(self): # type: ignore[no-untyped-def] # noqa: ANN201, D102
def sanity_checks(self) -> None:
"""Run sanity checks."""
# Note(decentral1se): Cannot implement driver specifics are unknown
pass

def schema_file(self): # type: ignore[no-untyped-def] # noqa: ANN201, D102
def schema_file(self) -> str:
"""Return schema file path.
Returns:
Path to schema file.
"""
return os.path.join(os.path.dirname(data_module), "driver.json") # noqa: PTH118, PTH120
6 changes: 5 additions & 1 deletion src/molecule/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@ class DependencyData(TypedDict, total=False):
env: dict[str, Any]


class DriverOptions(TypedDict):
class DriverOptions(TypedDict, total=False):
"""Config options for molecule drivers.
Attributes:
ansible_connection_options: Options to use with ansible connection plugin.
login_cmd_template: Template with which to generate login commands.
managed: Whether the driver is managed.
"""

ansible_connection_options: dict[str, str]
login_cmd_template: str
managed: bool


Expand Down

0 comments on commit 62c01f5

Please sign in to comment.