Skip to content

Commit

Permalink
🌟 style: Organizing project and following conventions
Browse files Browse the repository at this point in the history
- Configuring tools: mypy, pydocstyle, radon, bandit
- Adding checks to github actions
  • Loading branch information
henriquesebastiao committed May 3, 2024
1 parent 880a164 commit dbddcab
Show file tree
Hide file tree
Showing 14 changed files with 301 additions and 207 deletions.
32 changes: 18 additions & 14 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,26 @@ on: [ push, pull_request ]
name: CI

jobs:
ruff:
name: Ruff
checks:
name: Checks
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: chartboost/ruff-action@v1
with:
version: "0.1.3"
strategy:
matrix:
python-version: [ 3.12 ]
check: [ ruff, blue, isort, pydocstyle, radon, mypy, bandit ]

isort:
name: Isort
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: isort/isort-action@v1
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
sort-paths: "netmikro"
requirements-files: "requirements.txt"
configuration: "--profile black -l 79"
python-version: ${{ matrix.python-version }}
- name: Install Dependencies
run: |
python -m pip install --upgrade pip
pip install poetry
poetry config virtualenvs.create false
poetry install
- name: Run checks
run: |
task ${{ matrix.check }}
6 changes: 3 additions & 3 deletions netmikro/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
class InvalidIpAddress(Exception):
class InvalidIpAddress(Exception): # noqa: D101
pass


class InvalidNtpMode(Exception):
class InvalidNtpMode(Exception): # noqa: D101
pass


class UndefinedBooleanValue(Exception):
class UndefinedBooleanValue(Exception): # noqa: D101
pass
70 changes: 45 additions & 25 deletions netmikro/modules/base.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
from typing import Optional

from netmiko.mikrotik.mikrotik_ssh import MikrotikRouterOsSSH

from ..utils.common import IpAddress


class Base:
"""Class that generates the connection with a MikroTik router.
Args:
host (str): IP address of the router you want to connect to.
username (str): Username to be used in the connection.
password (str): Password to be used in the connection.
ssh_port (int): SSH port to be used in the connection.
delay (float): Time delay between command executions on the router.
Attributes:
host (IpAddress): IP address of the router you want to connect to.
username (str): Username to be used in the connection.
password (str): Password to be used in the connection.
ssh_port (int): SSH port to be used in the connection.
delay (float): Time delay between command executions on the router.
"""

def __init__(
self,
host: str,
Expand All @@ -14,17 +29,7 @@ def __init__(
ssh_port: int = 22,
delay: float = 0,
):
"""
Class that generates the connection with a MikroTik router.
Parameters:
host (str): IP address of the router you want to connect to.
username (str): Username to be used in the connection.
password (str): Password to be used in the connection.
ssh_port (int): SSH port to be used in the connection.
delay (float): Time delay between command executions on the router.
"""
self.host = IpAddress(host)
self.host = host
self.username = username
self.password = password
self.ssh_port = ssh_port
Expand All @@ -39,28 +44,43 @@ def __init__(
}
self._connection = MikrotikRouterOsSSH(**_auth)

def _get(self, command: str) -> Optional[str]:
output = self._connection.send_command(
command_string=f'return [{command}]'
)
if output == '':
return None
def _get(self, command: str) -> str:
output = self._connection.send_command(f'return [{command}]').strip()
return output

def _get_number(self, command: str) -> int:
output = self._connection.send_command(f'return [{command}]').strip()
if output == '':
return 0
return int(output)

def _get_bool(self, command: str) -> bool:
output = self._connection.send_command(f'return [{command}]').strip()
if output == 'true':
return True
return False

def _get_list_ips(self, command: str) -> list[IpAddress]:
output = (
self._connection.send_command(f'return [{command}]')
.strip()
.split(';')
)
return [IpAddress(ip) for ip in output]

def disconnect(self):
"""Disconnects the connection with the router."""
return self._connection.disconnect()

def cmd(self, command: str) -> str:
"""
Runs a command in the router's terminal.
"""Runs a command in the router's terminal.
Parameters:
command: Command to be executed
Args:
command (str): Command to be executed
Returns:
Output of the command
str: Output of the command
"""

# The `expect_string` parameter is a regex (format: [admin@mikrotik])
# necessary in case the router's identity is changed,
# there is no ReadTimeout error due to the output format changing,
Expand Down
19 changes: 12 additions & 7 deletions netmikro/modules/ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,25 @@

from netmikro.modules.base import Base

from ..utils import boolean, validate_port
from ..utils import validate_port


@dataclass
class IpService:
"""Class for representing ip service on a MikroTik router."""

port: int
disabled: bool
available_from: None | list[str]
available_from: str


class Ip(Base):
"""Class that generates the connection with a MikroTik router.
Attributes:
service (dict): Dictionary with the services available on the router.
"""

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

Expand All @@ -30,17 +38,14 @@ def __init__(self, *args, **kwargs):
self.service = {
service: IpService(
port=int(self._get(f'/ip service get {service} port')),
disabled=boolean(
self._get(f'/ip service get {service} disabled')
),
disabled=self._get_bool(f'/ip service get {service} disabled'),
available_from=self._get(f'/ip service get {service} address'),
)
for service in _service_names
}

def ip_port_set(self, service_name: str, port: int) -> None:
"""
Set the API port number.
"""Set the API port number.
Args:
service_name: The service to be changed.
Expand Down
Loading

0 comments on commit dbddcab

Please sign in to comment.