Skip to content

Commit c33e8ad

Browse files
committed
Reformat code & pre-commit (ruff).
1 parent 35bfce4 commit c33e8ad

21 files changed

+1476
-1893
lines changed

.pre-commit-config.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
repos:
2+
- repo: https://github.com/astral-sh/ruff-pre-commit
3+
# Ruff version.
4+
rev: v0.4.9
5+
hooks:
6+
# Run the linter.
7+
- id: ruff
8+
args: [ --fix ]
9+
# Run the formatter.
10+
- id: ruff-format

.vscode/launch.json

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
{
2-
// Use IntelliSense to learn about possible attributes.
3-
// Hover to view descriptions of existing attributes.
4-
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
52
"version": "0.2.0",
63
"configurations": [
74
{
85
"name": "Python: Current File",
96
"type": "python",
107
"request": "launch",
118
"program": "${file}",
12-
"console": "integratedTerminal"
13-
}
9+
"console": "integratedTerminal",
10+
"cwd": "${workspaceFolder}",
11+
"env": {
12+
"PYTHONPATH": "${workspaceFolder}"
13+
}
14+
},
15+
{
16+
"name": "Python: Module - inelsmqtt.utils.core",
17+
"type": "python",
18+
"request": "launch",
19+
"module": "inelsmqtt.utils.core",
20+
"console": "integratedTerminal",
21+
"cwd": "${workspaceFolder}"
22+
},
1423
]
15-
}
24+
}

.vscode/settings.json

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
{
2-
"python.pythonPath": "/usr/local/bin/python",
3-
/** Enable format on save */
2+
"python.pythonPath": "${workspaceFolder}/venv/bin/python",
3+
"python.testing.pytestArgs": [],
4+
"python.testing.unittestEnabled": false,
5+
"python.testing.pytestEnabled": true,
6+
"python.testing.cwd": "${workspaceFolder}/tests",
7+
"python.testing.autoTestDiscoverOnSaveEnabled": true,
48
"editor.formatOnSave": true,
5-
"python.testing.pytestEnabled": false,
6-
"python.formatting.provider": "none",
7-
"python.formatting.blackArgs": ["\"--line-length\", \"80\""],
8-
"python.testing.unittestArgs": ["-v", "-s", "./tests", "-p", "*_test.py"],
9-
"python.testing.unittestEnabled": true,
109
"editor.formatOnType": false,
11-
"python.formatting.blackPath": "/usr/local/bin/black",
12-
"python.analysis.extraPaths": ["./inelsmqtt"],
13-
"githubPullRequests.ignoredPullRequestBranches": ["master"],
14-
"[python]": {
15-
"editor.defaultFormatter": "ms-python.black-formatter"
16-
}
10+
"python.analysis.extraPaths": [
11+
"./inelsmqtt"
12+
],
13+
"githubPullRequests.ignoredPullRequestBranches": [
14+
"master"
15+
],
16+
"terminal.integrated.env.linux": {
17+
"PYTHONPATH": "${env:PYTHONPATH}:${workspaceFolder}",
18+
},
1719
}

dev-requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ruff==0.4.9
2+
pre-commit==3.7.1

inelsmqtt/__init__.py

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
11
"""Library specified for inels-mqtt."""
2-
from collections import defaultdict
2+
3+
import copy
34
import logging
45
import time
56
import uuid
6-
import copy
7-
7+
from collections import defaultdict
88
from datetime import datetime
99
from typing import Any, Callable, Optional
1010

1111
import paho.mqtt.client as mqtt
1212

1313
from .const import (
14+
DEVICE_TYPE_DICT,
15+
DISCOVERY_TIMEOUT_IN_SEC,
16+
FRAGMENT_DEVICE_TYPE,
17+
FRAGMENT_STATE,
1418
MQTT_CLIENT_ID,
1519
MQTT_HOST,
1620
MQTT_PASSWORD,
1721
MQTT_PORT,
22+
MQTT_PROTOCOL,
1823
MQTT_STATUS_TOPIC_PREFIX,
1924
MQTT_TIMEOUT,
2025
MQTT_TOTAL_CONNECTED_TOPIC,
2126
MQTT_TOTAL_STATUS_TOPIC,
2227
MQTT_TRANSPORT,
23-
MQTT_USERNAME,
24-
MQTT_PROTOCOL,
2528
MQTT_TRANSPORTS,
26-
VERSION,
27-
DEVICE_TYPE_DICT,
28-
FRAGMENT_DEVICE_TYPE,
29-
FRAGMENT_STATE,
29+
MQTT_USERNAME,
3030
TOPIC_FRAGMENTS,
31-
DISCOVERY_TIMEOUT_IN_SEC,
31+
VERSION,
3232
)
3333

3434
__version__ = VERSION
@@ -56,13 +56,9 @@ def __init__(
5656
transport (str): transportation protocol. Can be used tcp or websockets, defaltut tcp
5757
debug (bool): flag for debuging mqtt comunication. Default False
5858
"""
59-
proto = (
60-
config.get(MQTT_PROTOCOL) if config.get(MQTT_PROTOCOL) else mqtt.MQTTv311
61-
)
59+
proto = config.get(MQTT_PROTOCOL) if config.get(MQTT_PROTOCOL) else mqtt.MQTTv311
6260

63-
_t: str = (
64-
config.get(MQTT_TRANSPORT) if config.get(MQTT_TRANSPORT) else "tcp"
65-
).lower()
61+
_t: str = (config.get(MQTT_TRANSPORT) if config.get(MQTT_TRANSPORT) else "tcp").lower()
6662

6763
if _t not in MQTT_TRANSPORTS:
6864
raise Exception
@@ -91,7 +87,7 @@ def __init__(
9187
_t = config.get(MQTT_TIMEOUT)
9288
self.__timeout = _t if _t is not None else __DISCOVERY_TIMEOUT__
9389

94-
self.__listeners : dict[str, dict[str, Callable[[Any], Any]]] = defaultdict(lambda: dict())
90+
self.__listeners: dict[str, dict[str, Callable[[Any], Any]]] = defaultdict(lambda: dict())
9591
self.__is_subscribed_list = dict[str, bool]()
9692
self.__last_values = dict[str, str]()
9793
self.__try_connect = False
@@ -167,15 +163,15 @@ def test_connection(self) -> Optional[int]:
167163
self.disconnect()
168164
except Exception as e:
169165
if isinstance(e, ConnectionRefusedError):
170-
self.__connection_error = 3 # cannot connect
166+
self.__connection_error = 3 # cannot connect
171167
else:
172-
self.__connection_error = 6 # unknown
168+
self.__connection_error = 6 # unknown
173169

174170
return self.__connection_error
175171

176172
def subscribe_listener(self, topic: str, unique_id: str, fnc: Callable[[Any], Any]) -> None:
177173
"""Append new item into the datachange listener."""
178-
#if topic not in self.__listeners:
174+
# if topic not in self.__listeners:
179175
# self.__listeners[topic] = dict[str, Callable[[Any], Any]]()
180176

181177
stripped_topic = "/".join(topic.split("/")[2:])
@@ -422,7 +418,7 @@ def __on_discover(
422418
_LOGGER.info("Device of type %s found [status].\n", device_type)
423419
elif action == "connected":
424420
if topic not in self.__discovered:
425-
self.__discovered[topic] = None#msg.payload
421+
self.__discovered[topic] = None # msg.payload
426422
self.__last_values[msg.topic] = msg.payload
427423
self.__is_subscribed_list[msg.topic] = True
428424
_LOGGER.info("Device of type %s found [connected].\n", device_type)
@@ -457,13 +453,11 @@ def __on_message(
457453
if device_type in DEVICE_TYPE_DICT or device_type == "gw":
458454
# keep last value
459455
self.__last_values[msg.topic] = (
460-
copy.copy(self.__messages[msg.topic])
461-
if msg.topic in self.__messages
462-
else msg.payload
456+
copy.copy(self.__messages[msg.topic]) if msg.topic in self.__messages else msg.payload
463457
)
464458
self.__messages[msg.topic] = msg.payload
465459

466-
if device_type == "gw" and message_type == "connected":
460+
if device_type == "gw" and message_type == "connected":
467461
mac = message_parts[2]
468462
for stripped_topic in self.__listeners:
469463
if stripped_topic.startswith(mac):
@@ -481,7 +475,9 @@ def __on_message(
481475
def __notify_listeners(self, stripped_topic: str, is_connected_message: bool) -> None:
482476
"""Notify listeners for a specific topic."""
483477
if len(self.__listeners[stripped_topic]) > 0:
484-
for unique_id in list(self.__listeners[stripped_topic]): #prevents the dictionary increased in size during iteration exception
478+
for unique_id in list(
479+
self.__listeners[stripped_topic]
480+
): # prevents the dictionary increased in size during iteration exception
485481
self.__listeners[stripped_topic][unique_id](is_connected_message)
486482

487483
def __on_subscribe(
@@ -503,7 +499,7 @@ def __on_subscribe(
503499
properties (_type_, optional): Props from broker set.
504500
Defaults to None.
505501
"""
506-
#_LOGGER.info(mid)
502+
# _LOGGER.info(mid)
507503

508504
def __disconnect(self) -> None:
509505
"""Disconnecting from broker and stopping broker's loop"""

inelsmqtt/config.py

Lines changed: 0 additions & 128 deletions
This file was deleted.

0 commit comments

Comments
 (0)