Skip to content

Commit

Permalink
refactor: use imported aioeapi module
Browse files Browse the repository at this point in the history
  • Loading branch information
mtache committed May 15, 2024
1 parent 21fbf37 commit 4d0956d
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 18 deletions.
8 changes: 4 additions & 4 deletions aioeapi/config_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ async def push(self, content: list[str] | str, *, replace: bool = False) -> None
# prepare the initial set of command to enter the config session and
# rollback clean if the `replace` argument is True.

commands = [self._cli_config_session]
commands: list[str | dict[str, Any]] = [self._cli_config_session]
if replace:
commands.append(self.CLI_CFG_FACTORY_RESET)

Expand Down Expand Up @@ -244,7 +244,7 @@ async def diff(self) -> str:
----------
* https://www.gnu.org/software/diffutils/manual/diffutils.txt
"""
return await self._cli(f"show session-config named {self.name} diffs", ofmt="text")# type: ignore[return-value] # text outformat returns str
return await self._cli(f"show session-config named {self.name} diffs", ofmt="text") # type: ignore[return-value] # text outformat returns str

async def load_file(self, filename: str, *, replace: bool = False) -> None:
"""
Expand All @@ -268,12 +268,12 @@ async def load_file(self, filename: str, *, replace: bool = False) -> None:
If there are any issues with loading the configuration file then a
RuntimeError is raised with the error messages content.
"""
commands = [self._cli_config_session]
commands: list[str | dict[str, Any]] = [self._cli_config_session]
if replace:
commands.append(self.CLI_CFG_FACTORY_RESET)

commands.append(f"copy {filename} session-config")
res: list[dict[str, Any]] = await self._cli(commands=commands) # type: ignore[assignment] # JSON outformat of multiple commands returns list[dict[str, Any]]
res: list[dict[str, Any]] = await self._cli(commands=commands) # type: ignore[assignment] # JSON outformat of multiple commands returns list[dict[str, Any]]
checks_re = re.compile(r"error|abort|invalid", flags=re.I)
messages = res[-1]["messages"]

Expand Down
11 changes: 7 additions & 4 deletions aioeapi/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from __future__ import annotations

from socket import getservbyname
from typing import Any
from typing import TYPE_CHECKING, Any

# -----------------------------------------------------------------------------
# Public Imports
Expand All @@ -20,6 +20,9 @@
from .config_session import SessionConfig
from .errors import EapiCommandError

if TYPE_CHECKING:
from collections.abc import Sequence

# -----------------------------------------------------------------------------
# Exports
# -----------------------------------------------------------------------------
Expand Down Expand Up @@ -110,8 +113,8 @@ async def check_connection(self) -> bool:

async def cli( # noqa: PLR0913
self,
command: str | None = None,
commands: list[str] | None = None,
command: str | dict[str, Any] | None = None,
commands: Sequence[str | dict[str, Any]] | None = None,
ofmt: str | None = None,
version: int | str | None = "latest",
*,
Expand Down Expand Up @@ -184,7 +187,7 @@ async def cli( # noqa: PLR0913

def _jsonrpc_command( # noqa: PLR0913
self,
commands: list[str] | None = None,
commands: Sequence[str | dict[str, Any]] | None = None,
ofmt: str | None = None,
version: int | str | None = "latest",
*,
Expand Down
2 changes: 1 addition & 1 deletion anta/cli/exec/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
from pathlib import Path
from typing import TYPE_CHECKING, Literal

from aioeapi import EapiCommandError
from click.exceptions import UsageError
from httpx import ConnectError, HTTPError

from aioeapi import EapiCommandError
from anta.device import AntaDevice, AsyncEOSDevice
from anta.models import AntaCommand

Expand Down
9 changes: 5 additions & 4 deletions anta/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
from asyncssh import SSHClientConnection, SSHClientConnectionOptions
from httpx import ConnectError, HTTPError, TimeoutException

from anta import __DEBUG__, aioeapi
import aioeapi
from anta import __DEBUG__
from anta.logger import anta_log_exception, exc_to_str
from anta.models import AntaCommand

Expand Down Expand Up @@ -316,7 +317,7 @@ async def _collect(self, command: AntaCommand) -> None: # noqa: C901 function
----
command: the AntaCommand to collect.
"""
commands: list[dict[str, Any]] = []
commands: list[dict[str, str | int]] = []
if self.enable and self._enable_password is not None:
commands.append(
{
Expand All @@ -329,11 +330,11 @@ async def _collect(self, command: AntaCommand) -> None: # noqa: C901 function
commands.append({"cmd": "enable"})
commands += [{"cmd": command.command, "revision": command.revision}] if command.revision else [{"cmd": command.command}]
try:
response: list[dict[str, Any]] = await self._session.cli(
response: list[dict[str, Any] | str] = await self._session.cli(
commands=commands,
ofmt=command.ofmt,
version=command.version,
)
) # type: ignore[assignment] # multiple commands returns a list
# Do not keep response of 'enable' command
command.output = response[-1]
except aioeapi.EapiCommandError as e:
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import pytest
from click.testing import CliRunner, Result

from anta import aioeapi
import aioeapi
from anta.cli.console import console
from anta.device import AntaDevice, AsyncEOSDevice
from anta.inventory import AntaInventory
Expand Down
8 changes: 4 additions & 4 deletions tests/units/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from asyncssh import SSHClientConnection, SSHClientConnectionOptions
from rich import print as rprint

from anta import aioeapi
import aioeapi
from anta.device import AntaDevice, AsyncEOSDevice
from anta.models import AntaCommand
from tests.lib.fixture import COMMAND_OUTPUT
Expand Down Expand Up @@ -705,9 +705,9 @@ async def test_refresh(self, async_device: AsyncEOSDevice, patch_kwargs: list[di
"""Test AsyncEOSDevice.refresh()."""
with patch.object(async_device._session, "check_connection", **patch_kwargs[0]), patch.object(async_device._session, "cli", **patch_kwargs[1]):
await async_device.refresh()
async_device._session.check_connection.assert_called_once()
async_device._session.check_connection.assert_called_once() # type: ignore[attr-defined] # aioeapi.Device.check_connection is patched
if expected["is_online"]:
async_device._session.cli.assert_called_once()
async_device._session.cli.assert_called_once() # type: ignore[attr-defined] # aioeapi.Device.cli is patched
assert async_device.is_online == expected["is_online"]
assert async_device.established == expected["established"]
assert async_device.hw_model == expected["hw_model"]
Expand Down Expand Up @@ -740,7 +740,7 @@ async def test__collect(self, async_device: AsyncEOSDevice, command: dict[str, A
commands.append({"cmd": cmd.command, "revision": cmd.revision})
else:
commands.append({"cmd": cmd.command})
async_device._session.cli.assert_called_once_with(commands=commands, ofmt=cmd.ofmt, version=cmd.version)
async_device._session.cli.assert_called_once_with(commands=commands, ofmt=cmd.ofmt, version=cmd.version) # type: ignore[attr-defined] # aioeapi.Device.cli is patched # pylint: disable=line-too-long
assert cmd.output == expected["output"]
assert cmd.errors == expected["errors"]

Expand Down

0 comments on commit 4d0956d

Please sign in to comment.