diff --git a/config.toml b/config.toml index 77a0dcc..ad1e328 100644 --- a/config.toml +++ b/config.toml @@ -31,6 +31,9 @@ CLEANUP_IMAGES = false TESTING = true CLEANUP_IMAGES = false +[default.typer] +no_args_is_help = true + [default.snoop.install] snoop = "ss" out = "snoop.log" diff --git a/pyproject.toml b/pyproject.toml index 82e53fd..278dccc 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -22,7 +22,6 @@ fsspec = "^2023.10.0" mkdocs-git-revision-date-localized-plugin = "^1.2.1" mkdocs-material = {extras = ["all"], version = "^9.4.8"} - [tool.poetry.group.dev.dependencies] ruff = "^0.1.1" black = "^23.10.1" diff --git a/src/node_deployer/create_disk.py b/src/node_deployer/create_disk.py index 2ce700b..5a451c5 100644 --- a/src/node_deployer/create_disk.py +++ b/src/node_deployer/create_disk.py @@ -1,5 +1,4 @@ from fnmatch import fnmatch -import ipaddress from typing import Annotated, Optional from docker.types import Mount @@ -9,14 +8,10 @@ from .cli import cli_spinner from .config import config from .create_img import create_img from .debug import debug_guard +from .ip_interface import IPAddress from .utils import ensure_build_dir -# When PEP695 is supported this line should be: -# type IPAddress = ipaddress.IPv4Address | ipaddress.IPv6Address -IPAddress = ipaddress._IPAddressBase - - def filter_validation_response(response: str) -> str: """Filters out erroneous warnings from the validation response @@ -136,7 +131,7 @@ def create_ignition_disk( "-ip", help="IP address of the switch to connect to", prompt=True, - parser=ipaddress.ip_address, + parser=IPAddress, ), ] = None, switch_port: Annotated[ diff --git a/src/node_deployer/create_img.py b/src/node_deployer/create_img.py index b9df1b9..2fbc564 100644 --- a/src/node_deployer/create_img.py +++ b/src/node_deployer/create_img.py @@ -1,4 +1,3 @@ -import ipaddress import json from pathlib import Path from typing import Annotated, Optional @@ -9,12 +8,9 @@ from .autoignition import json_to_img from .cli import cli_spinner from .config import config from .debug import debug_guard +from .ip_interface import IPAddress from .utils import ensure_build_dir -# When PEP695 is supported this line should be: -# type IPAddress = ipaddress.IPv4Address | ipaddress.IPv6Address -IPAddress = ipaddress._IPAddressBase - def load_template() -> dict: """Loads the default template for the ignition configuration @@ -121,7 +117,7 @@ def create_img( "-ip", help="IP address of the switch to connect to", prompt=True, - parser=ipaddress.ip_address, + parser=IPAddress, ), ] = None, switch_port: Annotated[ diff --git a/src/node_deployer/debug.py b/src/node_deployer/debug.py index 5785414..8980b0c 100644 --- a/src/node_deployer/debug.py +++ b/src/node_deployer/debug.py @@ -44,7 +44,7 @@ def debug_guard(f: Callable) -> Callable: debug_f = get_debug_f(f) if kwargs.get("debug", False): # Snoop depth is set to compensate for wrapper stack frames - return debug_f(*args, **kwargs) # noqa: F821 #* ss is installed in debug_mode + return debug_f(*args, **kwargs) else: return f(*args, **kwargs) diff --git a/src/node_deployer/ip_interface.py b/src/node_deployer/ip_interface.py new file mode 100644 index 0000000..dc8ec6e --- /dev/null +++ b/src/node_deployer/ip_interface.py @@ -0,0 +1,46 @@ +from ipaddress import IPv4Address, IPv6Address, ip_address + + +class IPAddress: + def __init__(self, *args, **kwargs) -> None: + self.obj: IPv4Address | IPv6Address = ip_address(*args, **kwargs) + to_passthrough = ( + "compressed", + "exploded", + "is_global", + "is_link_local", + "is_loopback", + "is_multicast", + "is_private", + "is_reserved", + "is_unspecified", + "max_prefixlen", + "packed", + "reverse_pointer", + "version", + ) + for attrname in to_passthrough: + self._passthrough(attrname) + + def _passthrough(self, attrname: str) -> None: + """Passes through an attribute from the underlying IPv4Address or IPv6Address object + + Args: + attrname (str): The name of the attribute to pass through + + Raises: + AttributeError: If the attribute is a method + """ + attr = getattr(self.obj, attrname) + if callable(attr): + raise AttributeError(f"Passthrough is unavailable for methods ({attrname})") + setattr(self, attrname, attr) + + def __str__(self) -> str: + return str(self.obj) + + def __repr__(self) -> str: + return repr(self.obj) + + def __bool__(self) -> bool: + return not self.obj.is_unspecified diff --git a/src/node_deployer/node_deployer.py b/src/node_deployer/node_deployer.py index ef54495..e424d66 100644 --- a/src/node_deployer/node_deployer.py +++ b/src/node_deployer/node_deployer.py @@ -10,9 +10,7 @@ from .create_disk import create_ignition_disk from .create_img import create_img -cmd_params: Dict[Any, Any] = { - "no_args_is_help": True, -} +cmd_params: Dict[Any, Any] = config.typer app = typer.Typer( help="A tool for creating ignition images for automated deployment to a swarm",