Skip to content

Commit

Permalink
Fix linting
Browse files Browse the repository at this point in the history
  • Loading branch information
luqasz committed Nov 30, 2024
1 parent 8d985c9 commit 333d883
Show file tree
Hide file tree
Showing 17 changed files with 66 additions and 51 deletions.
8 changes: 4 additions & 4 deletions librouteros/__init__.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
# -*- coding: UTF-8 -*-

from socket import create_connection
from collections import ChainMap
from socket import create_connection

from librouteros.api import Api
from librouteros.connections import SocketTransport
from librouteros.exceptions import (
ConnectionClosed,
FatalError,
)
from librouteros.connections import SocketTransport
from librouteros.protocol import ApiProtocol
from librouteros.login import (
plain,
token, # noqa: F401 BACK_COMP
)
from librouteros.api import Api
from librouteros.protocol import ApiProtocol

DEFAULTS = {
"timeout": 10,
Expand Down
10 changes: 5 additions & 5 deletions librouteros/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

import typing
from posixpath import join as pjoin
from librouteros.exceptions import TrapError, MultiTrapError

from librouteros import query
from librouteros.exceptions import MultiTrapError, TrapError
from librouteros.protocol import (
ApiProtocol,
compose_word,
parse_word,
ApiProtocol,
)
from librouteros import query

from librouteros.types import (
ReplyDict,
ResponseIter,
Response,
ResponseIter,
)


Expand Down
1 change: 1 addition & 0 deletions librouteros/connections.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: UTF-8 -*-
from socket import socket

from librouteros.exceptions import ConnectionClosed


Expand Down
2 changes: 1 addition & 1 deletion librouteros/login.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: UTF-8 -*-

import typing
from binascii import unhexlify, hexlify # pylint: disable=no-name-in-module
from binascii import hexlify, unhexlify # pylint: disable=no-name-in-module
from hashlib import md5


Expand Down
6 changes: 3 additions & 3 deletions librouteros/protocol.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# -*- coding: UTF-8 -*-

import typing
from logging import NullHandler, getLogger
from struct import pack, unpack
from logging import getLogger, NullHandler

from librouteros.connections import SocketTransport
from librouteros.exceptions import (
ProtocolError,
FatalError,
ProtocolError,
)
from librouteros.connections import SocketTransport

LOGGER = getLogger("librouteros")
LOGGER.addHandler(NullHandler())
Expand Down
1 change: 1 addition & 0 deletions librouteros/query.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: UTF-8 -*-
import typing
from itertools import chain

from librouteros.protocol import (
cast_to_api,
)
Expand Down
2 changes: 1 addition & 1 deletion librouteros/types.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# -*- coding: UTF-8 -*-

from typing import (
Any,
Dict,
Iterator,
Any,
List,
)

Expand Down
8 changes: 4 additions & 4 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from time import sleep
import platform
import socket
from os import (
devnull,
environ,
)
from pathlib import Path
from random import randint
from subprocess import Popen, check_call
from tempfile import NamedTemporaryFile
import socket
import platform
from pathlib import Path
from time import sleep

import pytest

Expand Down
6 changes: 4 additions & 2 deletions tests/integration/test_general.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import socket
import pytest
from time import sleep
from librouteros.query import Key

import pytest

from librouteros import connect
from librouteros.exceptions import LibRouterosError
from librouteros.query import Key


def test_login(routeros_login):
Expand Down
5 changes: 3 additions & 2 deletions tests/requirements/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest
from subprocess import check_output
import sys
from subprocess import check_output

import pytest


@pytest.fixture
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import pytest
from collections import namedtuple
from struct import pack

import pytest

WordLength = namedtuple("WordLength", ("integer", "encoded"))
WordPair = namedtuple("WordPair", ("word", "pair"))

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import pytest

from librouteros.protocol import (
parse_word,
compose_word,
parse_word,
)


Expand Down
9 changes: 6 additions & 3 deletions tests/unit/test_connections.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
# -*- coding: UTF-8 -*-

import pytest
from socket import error as SOCKET_ERROR, timeout as SOCKET_TIMEOUT, socket
from socket import error as SOCKET_ERROR
from socket import socket
from socket import timeout as SOCKET_TIMEOUT
from unittest.mock import MagicMock

import pytest

from librouteros.connections import SocketTransport
from librouteros.exceptions import (
ConnectionClosed,
)


class Test_SocketTransport:
class TestSocketTransport:
def setup_method(self):
self.transport = SocketTransport(sock=MagicMock(spec=socket))

Expand Down
6 changes: 4 additions & 2 deletions tests/unit/test_librouteros.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# -*- coding: UTF-8 -*-
import socket
import pytest
from unittest.mock import (
patch,
Mock,
patch,
)

import pytest

from librouteros import (
DEFAULTS,
Api,
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/test_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from unittest.mock import (
MagicMock,
)

from librouteros.api import (
Api,
Path,
Expand All @@ -17,7 +18,7 @@ def test_api_path_returns_Path():
assert isinstance(new, Path)


class Test_Path:
class TestPath:
def setup_method(self):
self.path = Path(
path="/interface",
Expand Down
33 changes: 17 additions & 16 deletions tests/unit/test_protocol.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
# -*- coding: UTF-8 -*-

import pytest
from unittest.mock import MagicMock, patch

from librouteros.protocol import (
Encoder,
Decoder,
ApiProtocol,
)
import pytest

from librouteros.connections import SocketTransport
from librouteros.exceptions import (
ProtocolError,
FatalError,
ProtocolError,
)
from librouteros.protocol import (
ApiProtocol,
Decoder,
Encoder,
)


class Test_Decoder:
class TestDecoder:
def setup_method(self):
self.decoder = Decoder()
self.decoder.encoding = "ASCII"
Expand Down Expand Up @@ -47,7 +48,7 @@ def test_decodeLength_raises(self, bad_length_bytes):
assert str(bad_length_bytes) in str(error.value)


class Test_Encoder:
class TestEncoder:
def setup_method(self):
self.encoder = Encoder()
self.encoder.encoding = "ASCII"
Expand Down Expand Up @@ -82,34 +83,34 @@ def test_utf_8_word_encoding(self, enc_len_mock):
enc_len_mock.assert_called_once_with(len(word.encode(self.encoder.encoding)))

@patch.object(Encoder, "encodeWord", return_value=b"")
def test_encodeSentence(self, encodeWord_mock):
def test_encodeSentence(self, encode_word_mock):
r"""
Assert that:
\x00 is appended to the sentence
encodeWord is called == len(sentence)
"""
encoded = self.encoder.encodeSentence("first", "second")
assert encodeWord_mock.call_count == 2
assert encode_word_mock.call_count == 2
assert encoded[-1:] == b"\x00"


class Test_ApiProtocol:
class TestApiProtocol:
def setup_method(self):
self.protocol = ApiProtocol(
transport=MagicMock(spec=SocketTransport),
encoding="utf-8",
)

@patch.object(Encoder, "encodeSentence")
def test_writeSentence_calls_encodeSentence(self, encodeSentence_mock):
def test_writeSentence_calls_encodeSentence(self, encode_sentence_mock):
self.protocol.writeSentence("/ip/address/print", "=key=value")
encodeSentence_mock.assert_called_once_with("/ip/address/print", "=key=value")
encode_sentence_mock.assert_called_once_with("/ip/address/print", "=key=value")

@patch.object(Encoder, "encodeSentence")
def test_writeSentence_calls_transport_write(self, encodeSentence_mock):
def test_writeSentence_calls_transport_write(self, encode_sentence_mock):
"""Assert that write is called with encoded sentence."""
self.protocol.writeSentence("/ip/address/print", "=key=value")
self.protocol.transport.write.assert_called_once_with(encodeSentence_mock.return_value)
self.protocol.transport.write.assert_called_once_with(encode_sentence_mock.return_value)

@patch("librouteros.protocol.iter", return_value=("!fatal", "reason"))
def test_readSentence_raises_FatalError(self, iter_mock):
Expand Down
12 changes: 7 additions & 5 deletions tests/unit/test_query.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
# -*- coding: UTF-8 -*-
import pytest
from unittest.mock import MagicMock, patch

import pytest

from librouteros.query import (
Query,
Key,
And,
Key,
Or,
Query,
)


class Test_Query:
class TestQuery:
def setup_method(self):
self.query = Query(
path=MagicMock(),
Expand Down Expand Up @@ -51,7 +53,7 @@ def test_iter_no_proplist(self, iter_mock):
)


class Test_Key:
class TestKey:
def setup_method(self):
self.key = Key(
name="key_name",
Expand Down

0 comments on commit 333d883

Please sign in to comment.