Skip to content

Commit edae65f

Browse files
committed
Add pre commit hooks to validate the commits
It also fixes all validation errors.
1 parent 957dc5e commit edae65f

20 files changed

+572
-379
lines changed

.pre-commit-config.yaml

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
repos:
2+
- repo: https://github.com/psf/black
3+
rev: 20.8b1
4+
hooks:
5+
- id: black
6+
args:
7+
- --safe
8+
- --quiet
9+
files: ^((maxcube|sample|tests)/.+)?[^/]+\.py$
10+
- repo: https://github.com/codespell-project/codespell
11+
rev: v2.0.0
12+
hooks:
13+
- id: codespell
14+
args:
15+
- --ignore-words-list=maxcube
16+
- --skip="./.*"
17+
- --quiet-level=2
18+
exclude_types: [csv, json]
19+
exclude: ^tests/fixtures/
20+
- repo: https://gitlab.com/pycqa/flake8
21+
rev: 3.9.0
22+
hooks:
23+
- id: flake8
24+
additional_dependencies:
25+
- pycodestyle==2.7.0
26+
- pyflakes==2.3.1
27+
- pydocstyle==6.0.0
28+
- flake8-comprehensions==3.4.0
29+
- flake8-noqa==1.1.0
30+
files: ^(maxcube|sample|tests)/.+\.py$
31+
- repo: https://github.com/PyCQA/isort
32+
rev: 5.7.0
33+
hooks:
34+
- id: isort
35+
- repo: https://github.com/pre-commit/pre-commit-hooks
36+
rev: v3.2.0
37+
hooks:
38+
- id: check-executables-have-shebangs
39+
stages: [manual]
40+
- id: no-commit-to-branch
41+
args:
42+
- --branch=master
43+
- repo: https://github.com/adrienverge/yamllint.git
44+
rev: v1.24.2
45+
hooks:
46+
- id: yamllint
47+
- repo: https://github.com/pre-commit/mirrors-prettier
48+
rev: v2.2.1
49+
hooks:
50+
- id: prettier
51+
stages: [manual]
52+
- repo: https://github.com/cdce8p/python-typing-update
53+
rev: v0.3.2
54+
hooks:
55+
# Run `python-typing-update` hook manually from time to time
56+
# to update python typing syntax.
57+
# Will require manual work, before submitting changes!
58+
- id: python-typing-update
59+
stages: [manual]
60+
args:
61+
- --py38-plus
62+
- --force
63+
- --keep-updates
64+
files: ^(homeassistant|tests|script)/.+\.py$

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ python:
55
- 3.9
66

77
install:
8-
- pip install coveralls
8+
- pip install coveralls
99

1010
script: coverage run setup.py test
1111

maxcube/commander.py

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import base64
22
import logging
3-
import socket
4-
53
from time import sleep
64
from typing import List
75

@@ -11,15 +9,15 @@
119

1210
logger = logging.getLogger(__name__)
1311

14-
QUIT_MSG = Message('q')
15-
L_MSG = Message('l')
12+
QUIT_MSG = Message("q")
13+
L_MSG = Message("l")
1614
L_REPLY_CMD = L_MSG.reply_cmd()
1715

18-
UPDATE_TIMEOUT = Timeout('update', 3.0)
19-
CONNECT_TIMEOUT = Timeout('connect', 3.0)
20-
FLUSH_INPUT_TIMEOUT = Timeout('flush-input', 0)
21-
SEND_RADIO_MSG_TIMEOUT = Timeout('send-radio-msg', 30.0)
22-
CMD_REPLY_TIMEOUT = Timeout('cmd-reply', 2.0)
16+
UPDATE_TIMEOUT = Timeout("update", 3.0)
17+
CONNECT_TIMEOUT = Timeout("connect", 3.0)
18+
FLUSH_INPUT_TIMEOUT = Timeout("flush-input", 0)
19+
SEND_RADIO_MSG_TIMEOUT = Timeout("send-radio-msg", 30.0)
20+
CMD_REPLY_TIMEOUT = Timeout("cmd-reply", 2.0)
2321

2422

2523
class Commander(object):
@@ -34,8 +32,10 @@ def disconnect(self):
3432
if self.__connection:
3533
try:
3634
self.__connection.send(QUIT_MSG)
37-
except:
38-
logger.debug('Unable to properly shutdown MAX Cube connection. Resetting it...')
35+
except Exception:
36+
logger.debug(
37+
"Unable to properly shutdown MAX Cube connection. Resetting it..."
38+
)
3939
finally:
4040
self.__close()
4141

@@ -51,7 +51,7 @@ def update(self) -> List[Message]:
5151
response = self.__call(L_MSG, deadline)
5252
if response:
5353
self.__unsolicited_messages.append(response)
54-
except:
54+
except Exception:
5555
self.__connect(deadline)
5656
else:
5757
self.__connect(deadline)
@@ -61,7 +61,9 @@ def update(self) -> List[Message]:
6161

6262
def send_radio_msg(self, hex_radio_msg: str) -> bool:
6363
deadline = SEND_RADIO_MSG_TIMEOUT.deadline()
64-
request = Message('s', base64.b64encode(bytearray.fromhex(hex_radio_msg)).decode('utf-8'))
64+
request = Message(
65+
"s", base64.b64encode(bytearray.fromhex(hex_radio_msg)).decode("utf-8")
66+
)
6567
while not deadline.is_expired():
6668
if self.__cmd_send_radio_msg(request, deadline):
6769
return True
@@ -70,15 +72,17 @@ def send_radio_msg(self, hex_radio_msg: str) -> bool:
7072
def __cmd_send_radio_msg(self, request: Message, deadline: Deadline) -> bool:
7173
try:
7274
response = self.__call(request, deadline)
73-
duty_cycle, status_code, free_slots = response.arg.split(',', 3)
75+
duty_cycle, status_code, free_slots = response.arg.split(",", 3)
7476
if int(status_code) == 0:
7577
return True
76-
logger.debug('Radio message %s was not send [DutyCycle:%s, StatusCode:%s, FreeSlots:%s]' %
77-
(request, duty_cycle, status_code, free_slots))
78+
logger.debug(
79+
"Radio message %s was not send [DutyCycle:%s, StatusCode:%s, FreeSlots:%s]"
80+
% (request, duty_cycle, status_code, free_slots)
81+
)
7882
if int(duty_cycle) == 100 and int(free_slots) == 0:
7983
sleep(deadline.remaining(upper_bound=10.0))
8084
except Exception as ex:
81-
logger.error('Error sending radio message to Max! Cube: ' + str(ex))
85+
logger.error("Error sending radio message to Max! Cube: " + str(ex))
8286
return False
8387

8488
def __call(self, msg: Message, deadline: Deadline) -> Message:
@@ -97,7 +101,7 @@ def __call(self, msg: Message, deadline: Deadline) -> Message:
97101
raise TimeoutError(str(subdeadline))
98102
return result
99103

100-
except:
104+
except Exception:
101105
self.__close()
102106
if already_connected:
103107
return self.__call(msg, deadline)
@@ -114,7 +118,9 @@ def __is_connected(self) -> bool:
114118
def __connect(self, deadline: Deadline):
115119
self.__unsolicited_messages = []
116120
self.__connection = Connection(self.__host, self.__port)
117-
reply = self.__wait_for_reply(L_REPLY_CMD, deadline.subtimeout(CMD_REPLY_TIMEOUT))
121+
reply = self.__wait_for_reply(
122+
L_REPLY_CMD, deadline.subtimeout(CMD_REPLY_TIMEOUT)
123+
)
118124
if reply:
119125
self.__unsolicited_messages.append(reply)
120126

maxcube/connection.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import socket
21
import logging
3-
import time
2+
import socket
43

54
from .deadline import Deadline
65
from .message import Message
@@ -10,21 +9,22 @@
109
BLOCK_SIZE = 4096
1110
DEFAULT_TIMEOUT = 2.0
1211

12+
1313
class Connection(object):
1414
def __init__(self, host: str, port: int):
1515
self.__buffer: bytearray = bytearray()
1616
self.__socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
1717
self.__socket.settimeout(DEFAULT_TIMEOUT)
1818
self.__socket.connect((host, port))
19-
logger.debug('Connected to %s:%d!' % (host, port))
19+
logger.debug("Connected to %s:%d!" % (host, port))
2020

2121
def __read_buffered_msg(self) -> Message:
2222
buf = self.__buffer
23-
pos = buf.find(b'\r\n')
23+
pos = buf.find(b"\r\n")
2424
if pos < 0:
2525
return None
2626
result = buf[0:pos]
27-
del buf[0:pos+2]
27+
del buf[0 : pos + 2]
2828
return Message.decode(result)
2929

3030
def recv(self, deadline: Deadline) -> Message:
@@ -38,11 +38,11 @@ def recv(self, deadline: Deadline) -> Message:
3838
msg = self.__read_buffered_msg()
3939
logger.debug("received: %s" % msg)
4040
else:
41-
logger.debug('Connection shutdown by remote peer')
41+
logger.debug("Connection shutdown by remote peer")
4242
self.close()
4343
return None
4444
except socket.timeout:
45-
logger.debug('readline timed out')
45+
logger.debug("readline timed out")
4646
finally:
4747
self.__socket.settimeout(DEFAULT_TIMEOUT)
4848
return msg
@@ -54,6 +54,6 @@ def send(self, msg: Message):
5454
def close(self):
5555
try:
5656
self.__socket.close()
57-
logger.debug('closed')
58-
except:
59-
logger.debug('Unable to close connection. Dropping it...')
57+
logger.debug("closed")
58+
except Exception:
59+
logger.debug("Unable to close connection. Dropping it...")

0 commit comments

Comments
 (0)