Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: use ruff for linting and code formatting #258

Merged
merged 1 commit into from
Dec 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,11 @@ jobs:
run: |
python -m pip install --upgrade pip
pip install .[dev]
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --exit-zero
- name: Pylint
run: pylint ./ruuvitag_sensor
- name: Mypy
- name: ruff lint
run: ruff check ./ruuvitag_sensor
- name: mypy
run: mypy ./ruuvitag_sensor
- name: isort
run: isort . --diff --check-only
- name: black
run: black . --check
- name: ruff format
run: ruff format --check
- name: Tests
run: pytest -v -s --show-capture all
18 changes: 4 additions & 14 deletions developer_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,9 @@ $ python -m pip install -e .[dev]
$ python -m pip install -e .\[dev\]
```

Flake8
ruff
```sh
$ flake8
```

Pylint
```sh
$ pylint ./ruuvitag_sensor/
$ ruff check
```

mypy
Expand All @@ -57,14 +52,9 @@ $ mypy ./ruuvitag_sensor/

## Formatting

isort
```sh
$ isort .
```

black
ruff
```sh
$ black
$ ruff format --check
```

## Project files
Expand Down
2 changes: 1 addition & 1 deletion examples/http_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def update_data():
"""
Update data sent by background process to global all_data
"""
global all_data # pylint: disable=global-variable-not-assigned
global all_data # noqa: PLW0602
while not q.empty():
data = q.get()
all_data[data[0]] = data[1]
Expand Down
26 changes: 12 additions & 14 deletions examples/post_to_influxdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,20 @@ def write_to_influxdb(received_data):
mac = received_data[0]
payload = received_data[1]

dataFormat = payload["data_format"] if ("data_format" in payload) else None
dataFormat = payload.get("data_format", None)
fields = {}
fields["temperature"] = payload["temperature"] if ("temperature" in payload) else None
fields["humidity"] = payload["humidity"] if ("humidity" in payload) else None
fields["pressure"] = payload["pressure"] if ("pressure" in payload) else None
fields["accelerationX"] = payload["acceleration_x"] if ("acceleration_x" in payload) else None
fields["accelerationY"] = payload["acceleration_y"] if ("acceleration_y" in payload) else None
fields["accelerationZ"] = payload["acceleration_z"] if ("acceleration_z" in payload) else None
fields["temperature"] = payload.get("temperature", None)
fields["humidity"] = payload.get("humidity", None)
fields["pressure"] = payload.get("pressure", None)
fields["accelerationX"] = payload.get("acceleration_x", None)
fields["accelerationY"] = payload.get("acceleration_y", None)
fields["accelerationZ"] = payload.get("acceleration_z", None)
fields["batteryVoltage"] = payload["battery"] / 1000.0 if ("battery" in payload) else None
fields["txPower"] = payload["tx_power"] if ("tx_power" in payload) else None
fields["movementCounter"] = payload["movement_counter"] if ("movement_counter" in payload) else None
fields["measurementSequenceNumber"] = (
payload["measurement_sequence_number"] if ("measurement_sequence_number" in payload) else None
)
fields["tagID"] = payload["tagID"] if ("tagID" in payload) else None
fields["rssi"] = payload["rssi"] if ("rssi" in payload) else None
fields["txPower"] = payload.get("tx_power", None)
fields["movementCounter"] = payload.get("movement_counter", None)
fields["measurementSequenceNumber"] = payload.get("measurement_sequence_number", None)
fields["tagID"] = payload.get("tagID", None)
fields["rssi"] = payload.get("rssi", None)
json_body = [
{"measurement": "ruuvi_measurements", "tags": {"mac": mac, "dataFormat": dataFormat}, "fields": fields}
]
Expand Down
28 changes: 12 additions & 16 deletions examples/post_to_influxdb_rx.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,20 @@ def write_to_influxdb(received_data):
"""
Convert data into RuuviCollector naming scheme and scale and write to InfluxDB.
"""
dataFormat = received_data[1]["data_format"] if ("data_format" in received_data[1]) else None
dataFormat = received_data[1].get("data_format", None)
fields = {}
fields["temperature"] = received_data[1]["temperature"] if ("temperature" in received_data[1]) else None
fields["humidity"] = received_data[1]["humidity"] if ("humidity" in received_data[1]) else None
fields["pressure"] = received_data[1]["pressure"] if ("pressure" in received_data[1]) else None
fields["accelerationX"] = received_data[1]["acceleration_x"] if ("acceleration_x" in received_data[1]) else None
fields["accelerationY"] = received_data[1]["acceleration_y"] if ("acceleration_y" in received_data[1]) else None
fields["accelerationZ"] = received_data[1]["acceleration_z"] if ("acceleration_z" in received_data[1]) else None
fields["temperature"] = received_data[1].get("temperature", None)
fields["humidity"] = received_data[1].get("humidity", None)
fields["pressure"] = received_data[1].get("pressure", None)
fields["accelerationX"] = received_data[1].get("acceleration_x", None)
fields["accelerationY"] = received_data[1].get("acceleration_y", None)
fields["accelerationZ"] = received_data[1].get("acceleration_z", None)
fields["batteryVoltage"] = received_data[1]["battery"] / 1000.0 if ("battery" in received_data[1]) else None
fields["txPower"] = received_data[1]["tx_power"] if ("tx_power" in received_data[1]) else None
fields["movementCounter"] = (
received_data[1]["movement_counter"] if ("movement_counter" in received_data[1]) else None
)
fields["measurementSequenceNumber"] = (
received_data[1]["measurement_sequence_number"] if ("measurement_sequence_number" in received_data[1]) else None
)
fields["tagID"] = received_data[1]["tagID"] if ("tagID" in received_data[1]) else None
fields["rssi"] = received_data[1]["rssi"] if ("rssi" in received_data[1]) else None
fields["txPower"] = received_data[1].get("tx_power", None)
fields["movementCounter"] = received_data[1].get("movement_counter", None)
fields["measurementSequenceNumber"] = received_data[1].get("measurement_sequence_number", None)
fields["tagID"] = received_data[1].get("tagID", None)
fields["rssi"] = received_data[1].get("rssi", None)
json_body = [
{
"measurement": "ruuvi_measurements",
Expand Down
4 changes: 1 addition & 3 deletions examples/post_to_mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@


# let's trap ctrl-c, SIGINT and come down nicely
# pylint: disable=unused-argument,redefined-outer-name
def signal_handler(signal, frame):
print("\nterminating gracefully.")
client.disconnect()
Expand All @@ -64,9 +63,8 @@ def signal_handler(signal, frame):


# The callback for when the client receives a CONNACK response from the MQTT server.
# pylint: disable=unused-argument,redefined-outer-name
def on_connect(client, userdata, flags, rc):
print(f"Connected to MQTT broker with result code {str(rc)}")
print(f"Connected to MQTT broker with result code {rc!s}")

# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
Expand Down
2 changes: 0 additions & 2 deletions examples/print_to_screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
Pressure: 689
"""

# pylint: disable=duplicate-code

import os
from datetime import datetime

Expand Down
2 changes: 0 additions & 2 deletions examples/print_to_screen_ruuvitag_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
process and eventually process creation may fail.
"""

# pylint: disable=duplicate-code

import os
import time
from datetime import datetime
Expand Down
6 changes: 2 additions & 4 deletions examples/reactive_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
# Print only last updated every 10 seconds for F4:A5:74:89:16:57
ruuvi_rx.get_subject().pipe(ops.filter(lambda x: x[0] == "F4:A5:74:89:16:57"), ops.sample(interval_in_s)).subscribe(
lambda data: print(data)
) # pylint: disable=unnecessary-lambda
)

# Print only last updated every 10 seconds for every foud sensor
ruuvi_rx.get_subject().pipe(ops.group_by(lambda x: x[0])).subscribe(
Expand All @@ -29,9 +29,7 @@
# Print all from the last 10 seconds for F4:A5:74:89:16:57
ruuvi_rx.get_subject().pipe(
ops.filter(lambda x: x[0] == "F4:A5:74:89:16:57"), ops.buffer_with_time(interval_in_s)
).subscribe(
lambda data: print(data)
) # pylint: disable=unnecessary-lambda
).subscribe(lambda data: print(data))

# Execute subscribe only once for F4:A5:74:89:16:57
# when temperature goes over 80 degrees
Expand Down
4 changes: 2 additions & 2 deletions examples/send_updated_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ async def send_post(session, update_data):
async with session.post(
f"{server_url}/sensordata", data=json.dumps(update_data), headers={"content-type": "application/json"}
) as response:
response = await response.read()
response = await response.read() # noqa: PLW2901

async def send_put(session, update_data):
async with session.put(
f'{server_url}/sensors/{quote(update_data["mac"])}',
data=json.dumps(update_data),
headers={"content-type": "application/json"},
) as response:
response = await response.read()
response = await response.read() # noqa: PLW2901

async with ClientSession() as session:
while True:
Expand Down
59 changes: 28 additions & 31 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,8 @@ dependencies = [
dev = [
"pytest",
"pytest-asyncio",
"flake8-pyproject",
"pylint",
"mypy",
"isort",
"black"
"ruff",
"mypy"
]

[project.urls]
Expand All @@ -53,35 +50,35 @@ changelog = "https://github.com/ttu/ruuvitag-sensor/blob/master/CHANGELOG.md"
[tool.pytest.ini_options]
testpaths = "tests/"

[tool.flake8]
exclude = ".venv, .git, .eggs, __pycache__, build, dist"
max-line-length = 120
ignore = "E402, E203"
show-source = true
max-complexity = 12
[tool.ruff]
exclude = [".venv", ".git", ".eggs", "__pycache__", "build", "dist"]
line-length = 120

[tool.pylint.messages_control]
max-line-length = 120
disable = [
"import-error",
"missing-docstring",
"invalid-name",
"bare-except",
"broad-except",
"broad-exception-raised",
"fixme",
"dangerous-default-value",
"too-few-public-methods",
"useless-object-inheritance"
[tool.ruff.lint]
select = [
"A", # flake8-builtins
"C90", # maccabe
"E", # pycodestyle errors
"F", # pyflakes
"G", # flake8-logging-format
"I", # isort
"PERF", # perflint
"PIE", # flake8-pie
"PL", # pylint
"PTH", # flake8-use-pathlib
"Q", # flake8-quotes
"RUF", # ruff-specific rules
"SIM", # flake8-simplify
"W", # pycodestyle warnings
]
ignore = [
"PLR2004", # Magic value used in comparison
"RUF006", # Store a reference to the return value of asyncio.create_task
]

[tool.ruff.lint.mccabe]
max-complexity = 12

[tool.mypy]
python_version = 3.9
ignore_missing_imports = true

[tool.isort]
line_length = 120
ensure_newline_before_comments = true

[tool.black]
line-length = 120
2 changes: 1 addition & 1 deletion ruuvitag_sensor/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import importlib.metadata

__version__ = importlib.metadata.version(__package__ or __name__) # pylint: disable=no-member
__version__ = importlib.metadata.version(__package__ or __name__)
4 changes: 1 addition & 3 deletions ruuvitag_sensor/adapters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

from ruuvitag_sensor.ruuvi_types import MacAndRawData, RawData

# pylint: disable=import-outside-toplevel, cyclic-import, too-many-return-statements


def get_ble_adapter():
forced_ble_adapter = os.environ.get("RUUVI_BLE_ADAPTER", "").lower()
Expand Down Expand Up @@ -98,5 +96,5 @@ async def get_data(blacklist: List[str] = [], bt_device: str = "") -> AsyncGener
# if False: yield is a mypy fix for
# error: Return type "AsyncGenerator[Tuple[str, str], None]" of "get_data" incompatible with return type
# "Coroutine[Any, Any, AsyncGenerator[Tuple[str, str], None]]" in supertype "BleCommunicationAsync"
if False: # pylint: disable=unreachable,using-constant-test
if False:
yield 0
1 change: 0 additions & 1 deletion ruuvitag_sensor/adapters/bleak_ble.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ def _get_scanner(detection_callback: AdvertisementDataCallback, bt_device: str =
scanning_mode = "passive" if sys.platform.startswith("win") else "active"

if "bleak_dev" in os.environ.get("RUUVI_BLE_ADAPTER", "").lower():
# pylint: disable=import-outside-toplevel
from ruuvitag_sensor.adapters.development.dev_bleak_scanner import DevBleakScanner

return DevBleakScanner(detection_callback, scanning_mode)
Expand Down
9 changes: 2 additions & 7 deletions ruuvitag_sensor/adapters/bleson.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

log = logging.getLogger(__name__)

# pylint: disable=duplicate-code


class BleCommunicationBleson(BleCommunication):
"""Bluetooth LE communication with Bleson"""
Expand Down Expand Up @@ -76,11 +74,8 @@ def start(bt_device=""):
device (string): BLE device (default 0)
"""

if not bt_device:
bt_device = 0
else:
# Old communication used hci0 etc.
bt_device = bt_device.replace("hci", "")
# Old communication used hci0 etc.
bt_device = 0 if not bt_device else bt_device.replace("hci", "")

log.info("Start receiving broadcasts (device %s)", bt_device)

Expand Down
2 changes: 0 additions & 2 deletions ruuvitag_sensor/adapters/development/dev_bleak_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,9 @@ def __init__(self, callback, _):
async def start(self):
self.running = True
asyncio.create_task(self.run())
return None

async def stop(self):
self.running = False
return None

async def run(self):
while self.running:
Expand Down
13 changes: 5 additions & 8 deletions ruuvitag_sensor/adapters/nix_hci.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

log = logging.getLogger(__name__)

# pylint: disable=duplicate-code


class BleCommunicationNix(BleCommunication):
"""Bluetooth LE communication for Linux"""
Expand All @@ -24,12 +22,12 @@ def start(bt_device=""):
"""
# import ptyprocess here so as long as all implementations are in
# the same file, all will work
import ptyprocess # pylint: disable=import-outside-toplevel
import ptyprocess

if not bt_device:
bt_device = "hci0"

is_root = os.getuid() == 0 # pylint: disable=no-member
is_root = os.getuid() == 0

log.info("Start receiving broadcasts (device %s)", bt_device)
DEVNULL = subprocess.DEVNULL
Expand Down Expand Up @@ -95,9 +93,8 @@ def get_lines(hcidump):
data = line[2:].replace(" ", "")
elif line.startswith("< "):
data = None
else:
if data:
data += line.replace(" ", "")
elif data:
data += line.replace(" ", "")
except KeyboardInterrupt:
return
except Exception as ex:
Expand All @@ -112,7 +109,7 @@ def get_data(blacklist: List[str] = [], bt_device: str = "") -> Generator[MacAnd
log.debug("Parsing line %s", line)
try:
# Make sure we're in upper case
line = line.upper()
line = line.upper() # noqa: PLW2901
# We're interested in LE meta events, sent by Ruuvitags.
# Those start with "043E", followed by a length byte.

Expand Down
Loading
Loading