Skip to content

Commit e1a2eab

Browse files
committed
Add linters.
1 parent bd2aaa7 commit e1a2eab

22 files changed

+82
-54
lines changed

routeros_api/__init__.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
from routeros_api.api import connect
2-
from routeros_api.api import RouterOsApiPool
3-
from routeros_api import query
41
from routeros_api import api_structure
2+
from routeros_api import query
3+
from routeros_api.api import RouterOsApiPool
4+
from routeros_api.api import connect
55

66
__all__ = ['connect', 'RouterOsApiPool', 'query', 'api_structure']

routeros_api/api.py

+14-8
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
1-
import hashlib
21
import binascii
2+
import hashlib
3+
34
from routeros_api import api_communicator
4-
from routeros_api import communication_exception_parsers
55
from routeros_api import api_socket
66
from routeros_api import api_structure
77
from routeros_api import base_api
8+
from routeros_api import communication_exception_parsers
89
from routeros_api import exceptions
910
from routeros_api import resource
1011

1112

12-
def connect(host, username='admin', password='', port=None, plaintext_login=False, use_ssl=False, ssl_verify=True, ssl_verify_hostname=True, ssl_context=None):
13-
return RouterOsApiPool(host, username, password, port, plaintext_login, use_ssl, ssl_verify, ssl_verify_hostname, ssl_context).get_api()
13+
def connect(host, username='admin', password='', port=None, plaintext_login=False, use_ssl=False, ssl_verify=True,
14+
ssl_verify_hostname=True, ssl_context=None):
15+
return RouterOsApiPool(
16+
host, username, password, port, plaintext_login, use_ssl, ssl_verify, ssl_verify_hostname, ssl_context,
17+
).get_api()
1418

1519

1620
class RouterOsApiPool(object):
1721
socket_timeout = 15.0
1822

19-
def __init__(self, host, username='admin', password='', port=None, plaintext_login=False, use_ssl=False, ssl_verify=True, ssl_verify_hostname=True, ssl_context=None):
23+
def __init__(self, host, username='admin', password='', port=None, plaintext_login=False, use_ssl=False,
24+
ssl_verify=True, ssl_verify_hostname=True, ssl_context=None):
2025
self.host = host
2126
self.username = username
2227
self.password = password
@@ -41,8 +46,9 @@ def __init__(self, host, username='admin', password='', port=None, plaintext_log
4146

4247
def get_api(self):
4348
if not self.connected:
44-
self.socket = api_socket.get_socket(self.host, self.port,
45-
timeout=self.socket_timeout, use_ssl=self.use_ssl, ssl_verify=self.ssl_verify, ssl_verify_hostname=self.ssl_verify_hostname, ssl_context=self.ssl_context)
49+
self.socket = api_socket.get_socket(
50+
self.host, self.port, timeout=self.socket_timeout, use_ssl=self.use_ssl, ssl_verify=self.ssl_verify,
51+
ssl_verify_hostname=self.ssl_verify_hostname, ssl_context=self.ssl_context)
4652
base = base_api.Connection(self.socket)
4753
communicator = api_communicator.ApiCommunicator(base)
4854
self.api = RouterOsApi(communicator)
@@ -83,7 +89,7 @@ def login(self, login, password, plaintext_login):
8389
login = login.encode()
8490
if isinstance(password, str):
8591
password = password.encode()
86-
response = self.get_binary_resource('/').call('login',{ 'name': login, 'password': password })
92+
response = self.get_binary_resource('/').call('login', {'name': login, 'password': password})
8793
else:
8894
response = self.get_binary_resource('/').call('login')
8995
if 'ret' in response.done_message:

routeros_api/api_communicator/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
from routeros_api.api_communicator import base
21
from routeros_api.api_communicator import async_decorator
3-
from routeros_api.api_communicator import exception_decorator
2+
from routeros_api.api_communicator import base
43
from routeros_api.api_communicator import encoding_decorator
4+
from routeros_api.api_communicator import exception_decorator
55
from routeros_api.api_communicator import key_cleaner_decorator
66

77

routeros_api/api_communicator/async_decorator.py

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ def call(self, *args, **kwargs):
66
tag = self.inner.send(*args, **kwargs)
77
return ResponsePromise(self.inner, tag)
88

9+
910
class ResponsePromise(object):
1011
def __init__(self, receiver, tag):
1112
self.receiver = receiver

routeros_api/api_communicator/base.py

+7-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from routeros_api import exceptions
2-
from routeros_api import sentence
32
from routeros_api import query
3+
from routeros_api import sentence
44

55

66
class ApiCommunicatorBase(object):
@@ -9,17 +9,14 @@ def __init__(self, base):
99
self.tag = 0
1010
self.response_buffor = {}
1111

12-
def send(self, path, command, arguments=None, queries=None,
13-
additional_queries=()):
12+
def send(self, path, command, arguments=None, queries=None, additional_queries=()):
1413
tag = self._get_next_tag()
15-
command = self.get_command(path, command, arguments, queries, tag=tag,
16-
additional_queries=additional_queries)
14+
command = self.get_command(path, command, arguments, queries, tag=tag, additional_queries=additional_queries)
1715
self.send_command(command)
1816
self.response_buffor[tag] = AsynchronousResponse(command=command)
1917
return tag
2018

21-
def get_command(self, path, command, arguments=None, queries=None,
22-
tag=None, additional_queries=()):
19+
def get_command(self, path, command, arguments=None, queries=None, tag=None, additional_queries=()):
2320
arguments = arguments or {}
2421
queries = queries or {}
2522
command = sentence.CommandSentence(path, command, tag=tag)
@@ -40,7 +37,7 @@ def _get_next_tag(self):
4037

4138
def receive(self, tag):
4239
response_buffor_manager = AsynchronousResponseBufforManager(self, tag)
43-
while(not response_buffor_manager.done):
40+
while not response_buffor_manager.done:
4441
response_buffor_manager.step_to_finish_response()
4542
response_buffor_manager.clean()
4643
response = response_buffor_manager.response
@@ -84,7 +81,7 @@ def save_to_buffor(self, buffor):
8481
elif self.response.type == b'trap':
8582
asynchronous_response.error = self.response.attributes[b'message']
8683
elif self.response.type == b'fatal':
87-
del(buffor[self.response.tag])
84+
del (buffor[self.response.tag])
8885
message = "Fatal error executing command {command}".format(
8986
command=asynchronous_response.command)
9087
raise exceptions.RouterOsApiFatalCommunicationError(message)
@@ -132,7 +129,7 @@ def done(self):
132129
return self.response.done
133130

134131
def clean(self):
135-
del(self.receiver.response_buffor[self.tag])
132+
del (self.receiver.response_buffor[self.tag])
136133

137134

138135
class AsynchronousResponse(list):
@@ -154,7 +151,6 @@ def error_as_exception(self):
154151
else:
155152
return None
156153

157-
158154
def map(self, function):
159155
result = type(self)(map(function, self), command=self.command)
160156
result.done_message = function(self.done_message)

routeros_api/api_communicator/encoding_decorator.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ class EncodingApiCommunicator(object):
22
def __init__(self, inner):
33
self.inner = inner
44

5-
def call(self, path, command, arguments=None, queries=None,
6-
additional_queries=()):
5+
def call(self, path, command, arguments=None, queries=None, additional_queries=()):
76
path = path.encode()
87
command = command.encode()
98
arguments = self.transform_dictionary(arguments or {})

routeros_api/api_communicator/key_cleaner_decorator.py

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def encode_key(key):
3131
else:
3232
return key
3333

34+
3435
def decode_dictionary(dictionary):
3536
return dict([(decode_key(key), value) for key, value in
3637
dictionary.items()])

routeros_api/api_socket.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
import socket
22
import ssl
3+
34
from routeros_api import exceptions
5+
46
try:
57
import errno
68
except ImportError:
79
errno = None
810

911
EINTR = getattr(errno, 'EINTR', 4)
1012

11-
def get_socket(hostname, port, use_ssl=False, ssl_verify=True, ssl_verify_hostname=True, ssl_context=None, timeout=15.0):
13+
14+
def get_socket(hostname, port, use_ssl=False, ssl_verify=True, ssl_verify_hostname=True, ssl_context=None,
15+
timeout=15.0):
1216
while True:
1317
try:
1418
api_socket = socket.create_connection((hostname, port), timeout=timeout)
@@ -28,9 +32,10 @@ def get_socket(hostname, port, use_ssl=False, ssl_verify=True, ssl_verify_hostna
2832
ssl_context.check_hostname = False
2933
ssl_context.verify_mode = ssl.CERT_NONE
3034
if ssl_context is not None:
31-
api_socket = ssl_context.wrap_socket(api_socket,server_hostname=hostname)
35+
api_socket = ssl_context.wrap_socket(api_socket, server_hostname=hostname)
3236
return SocketWrapper(api_socket)
3337

38+
3439
# http://stackoverflow.com/a/14855726
3540
def set_keepalive(sock, after_idle_sec=1, interval_sec=3, max_fails=5):
3641
"""Set TCP keepalive on an open socket.

routeros_api/base_api.py

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from routeros_api import exceptions
44

5-
65
LENGTH_MATRIX = [
76
(0x80, 0x0),
87
(0x40, 0x80),

routeros_api/communication_exception_parsers.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import re
2+
23
from routeros_api import exceptions
34

45

routeros_api/exceptions.py

+6
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
11
class RouterOsApiError(Exception):
22
pass
33

4+
45
class RouterOsApiConnectionError(RouterOsApiError):
56
pass
67

8+
79
class FatalRouterOsApiError(RouterOsApiError):
810
pass
911

12+
1013
class RouterOsApiParsingError(RouterOsApiError):
1114
pass
1215

16+
1317
class RouterOsApiCommunicationError(RouterOsApiError):
1418
def __init__(self, message, original_message):
1519
super(RouterOsApiCommunicationError, self).__init__(message, original_message)
1620
self.original_message = original_message
1721

22+
1823
class RouterOsApiFatalCommunicationError(RouterOsApiError):
1924
pass
2025

26+
2127
class RouterOsApiConnectionClosedError(RouterOsApiConnectionError):
2228
pass

routeros_api/query.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from routeros_api import utils
22

3+
34
class BasicQuery(object):
45
operator = None
56

routeros_api/resource.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ def remove_async(self, **kwargs):
3535

3636
def call(self, command, arguments=None, queries=None,
3737
additional_queries=()):
38-
return self.call_async(command, arguments=arguments, queries=queries,
39-
additional_queries=additional_queries).get()
38+
return self.call_async(
39+
command, arguments=arguments, queries=queries, additional_queries=additional_queries,
40+
).get()
4041

41-
def call_async(self, command, arguments=None, queries=None,
42-
additional_queries=()):
42+
def call_async(self, command, arguments=None, queries=None, additional_queries=()):
4343
return self.communicator.call(
4444
self.path, command, arguments=arguments, queries=queries,
4545
additional_queries=additional_queries)
@@ -53,8 +53,7 @@ def __init__(self, communicator, path, structure):
5353
self.structure = structure
5454
super(RouterOsResource, self).__init__(communicator, path)
5555

56-
def call_async(self, command, arguments=None, queries=None,
57-
additional_queries=()):
56+
def call_async(self, command, arguments=None, queries=None, additional_queries=()):
5857
arguments = self.transform_dictionary(arguments or {})
5958
queries = self.transform_dictionary(queries or {})
6059
promise = self.communicator.call(

routeros_api/sentence.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from routeros_api import exceptions
44
from routeros_api import query
55

6-
76
response_re = re.compile(rb'^!(re|trap|fatal|done)$')
87
attribute_re = re.compile(rb'^=([^=]+)=(.*)$', re.DOTALL)
98
tag_re = re.compile(rb'^\.tag=(.*)$')
@@ -26,7 +25,6 @@ def parse(cls, sentence):
2625
sentence)
2726
return response
2827

29-
3028
def parse_attributes(self, serialized_attributes):
3129
for serialized in serialized_attributes:
3230
attribute_match = attribute_re.match(serialized)
@@ -57,8 +55,8 @@ def get_api_format(self):
5755
formated = [self.path + self.command]
5856
for key, value in self.attributes.items():
5957
formated.append(b'=' + key + b'=' + value)
60-
for query in self.queries:
61-
formated.extend(query.get_api_format())
58+
for _query in self.queries:
59+
formated.extend(_query.get_api_format())
6260
if self.tag is not None:
6361
formated.append(b'.tag=' + self.tag)
6462
return formated

setup.cfg

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
[flake8]
2+
max-line-length = 120
3+
4+
[isort]
5+
force_single_line = true
6+
line_length = 120
7+
lines_between_types = 1
8+
19
[zest.releaser]
210
create-wheel = yes
311

setup.py

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import sys
2-
31
from setuptools import find_packages
42
from setuptools import setup
53

tests/test_api_communicator.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
except ImportError:
66
import mock
77

8-
from routeros_api import exceptions
98
from routeros_api import api_communicator
9+
from routeros_api import exceptions
10+
1011

1112
class TestCommunicator(TestCase):
1213
def test_login_call(self):

tests/test_base_api.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,14 @@ def test_multiple_bytes(self):
9393

9494

9595
class TestConnection(TestCase):
96-
def test_sending(self, ):
96+
def test_sending(self):
9797
socket = mock.Mock()
9898
connection = base_api.Connection(socket)
9999
connection.send_sentence([b'foo', b'bar'])
100100
expected = [
101101
mock.call(b'\x03foo'),
102102
mock.call(b'\x03bar'),
103-
mock.call(b'\x00')
103+
mock.call(b'\x00'),
104104
]
105105
self.assertEqual(expected, socket.send.mock_calls)
106106

tests/test_resource.py

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from routeros_api import resource
1010
from routeros_api.api_communicator import base
1111

12-
1312
BYTES_STRUCTURE = {'bytes': structure.BytesField()}
1413
BOOLEAN_STRUCTURE = {'boolean': structure.BooleanField()}
1514

tests/test_sentence.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
from unittest import TestCase
22

3-
try:
4-
from unittest import mock
5-
except ImportError:
6-
import mock
7-
83
from routeros_api import exceptions
94
from routeros_api import sentence
105

6+
117
class TestResponseSentence(TestCase):
128
def test_done(self):
139
response = sentence.ResponseSentence.parse([b'!done'])
@@ -34,6 +30,7 @@ def test_trap(self):
3430
self.assertEqual(response.type, b'trap')
3531
self.assertEqual(response.attributes[b'message'], b'b')
3632

33+
3734
class TestCommandSentence(TestCase):
3835
def test_login_sentence(self):
3936
command = sentence.CommandSentence(b'/', b'login')
@@ -51,4 +48,4 @@ def test_query_sentence_with_tag(self):
5148
command = sentence.CommandSentence(b'/interface/', b'print', tag=b'0')
5249
command.filter(name=b'wlan0')
5350
self.assertEqual(command.get_api_format(),
54-
[b'/interface/print', b'?name=wlan0', b'.tag=0'])
51+
[b'/interface/print', b'?name=wlan0', b'.tag=0'])

0 commit comments

Comments
 (0)