Skip to content

Commit

Permalink
update pre-commit hooks (#58)
Browse files Browse the repository at this point in the history
* update pre-commit hooks

- switch from black to ruff for code formatting
- remove useless pylint-specific comments
- run updated pre-commit hooks on all files

* add codecov config
  • Loading branch information
2bndy5 authored Jun 8, 2024
1 parent 1f23cfc commit a861db9
Show file tree
Hide file tree
Showing 30 changed files with 82 additions and 67 deletions.
14 changes: 7 additions & 7 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@
# SPDX-License-Identifier: Unlicense

repos:
- repo: https://github.com/python/black
rev: 23.11.0
hooks:
- id: black
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
rev: v4.6.0
hooks:
- id: check-yaml
exclude: ^docs/social_cards/layouts
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.7
rev: v0.4.8
hooks:
# Run the linter.
- id: ruff
args: [ --fix ]
# Run the formatter.
- id: ruff-format
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.7.1
rev: v1.10.0
hooks:
- id: mypy
name: mypy (library code)
Expand Down
15 changes: 6 additions & 9 deletions circuitpython_nrf24l01/fake_ble.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
# THE SOFTWARE.
"""Original research was done by Dmitry Grinberg and his write-up can be found at
http://dmitry.gr/index.php?r=05.Projects&proj=11.%20Bluetooth%20LE%20fakery"""

from os import urandom
import struct

Expand Down Expand Up @@ -192,9 +193,9 @@ def __init__(
# disable auto_ack, dynamic payloads, all TX features, & auto-retry
self._aa, self._dyn_pl, self._features, self._retry_setup = (0,) * 4
self._addr_len = 4 # use only 4 byte address length
self._tx_address[:4] = b"\x71\x91\x7D\x6B"
self._tx_address[:4] = b"\x71\x91\x7d\x6b"
with self:
super().open_rx_pipe(0, b"\x71\x91\x7D\x6B\0")
super().open_rx_pipe(0, b"\x71\x91\x7d\x6b\0")
#: The internal queue of received BLE payloads' data.
self.rx_queue: List[QueueElement] = []
self.rx_cache: bytearray = bytearray(0)
Expand Down Expand Up @@ -275,8 +276,9 @@ def _make_payload(self, payload: Union[bytes, bytearray]) -> bytes:
"""Assemble the entire packet to be transmitted as a payload."""
if self.len_available(payload) < 0:
raise ValueError(
"Payload length exceeds maximum buffer size by "
"{} bytes".format(abs(self.len_available(payload)))
"Payload length exceeds maximum buffer size by {} bytes".format(
abs(self.len_available(payload))
)
)
name_length = 0 if self._ble_name is None else (len(self._ble_name) + 2)
pl_size = 9 + len(payload) + name_length + self._show_dbm * 3
Expand Down Expand Up @@ -344,7 +346,6 @@ def available(self) -> bool:
self.rx_queue.append(QueueElement(self.rx_cache))
return bool(self.rx_queue)

# pylint: disable=arguments-differ
def read(self) -> Optional[QueueElement]: # type: ignore[override]
"""Get the First Out element from the queue."""
if self.rx_queue:
Expand All @@ -353,8 +354,6 @@ def read(self) -> Optional[QueueElement]: # type: ignore[override]
return ret_val
return None

# pylint: enable=arguments-differ
# pylint: disable=unused-argument
@RF24.dynamic_payloads.setter # type: ignore[attr-defined]
def dynamic_payloads(self, val):
raise NotImplementedError("using dynamic_payloads breaks BLE specifications")
Expand Down Expand Up @@ -385,8 +384,6 @@ def open_rx_pipe(self, pipe_number, address):
def open_tx_pipe(self, address):
raise NotImplementedError("BLE implementation only uses 1 address")

# pylint: enable=unused-argument


class ServiceData:
"""An abstract helper class to package specific service data using
Expand Down
1 change: 1 addition & 0 deletions circuitpython_nrf24l01/network/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
"""All the constants related to managing a network with nRF24L01 radios."""

from micropython import const


Expand Down
2 changes: 1 addition & 1 deletion circuitpython_nrf24l01/network/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
"""A module to hold all usually accessible RF24 API via the RF24Network API"""
# pylint: disable=missing-docstring

import time

try:
Expand Down
1 change: 1 addition & 0 deletions circuitpython_nrf24l01/network/structs.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
# THE SOFTWARE.
"""These classes are used to structure/store the payload data for wireless network
transactions."""

import struct

try:
Expand Down
5 changes: 3 additions & 2 deletions circuitpython_nrf24l01/rf24.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
"""rf24 module containing the base class RF24"""

import time

try:
Expand Down Expand Up @@ -877,8 +878,8 @@ def start_carrier_wave(self):
if not self.is_plus_variant:
self._reg_write(AUTO_ACK, 0)
self._reg_write(SETUP_RETR, 0)
self._reg_write_bytes(TX_ADDRESS, b"\xFF" * 5)
self._reg_write_bytes(0xA0, b"\xFF" * 32)
self._reg_write_bytes(TX_ADDRESS, b"\xff" * 5)
self._reg_write_bytes(0xA0, b"\xff" * 32)
self._reg_write(CONFIGURE, 0x73)
self._ce_pin.value = True
time.sleep(0.001)
Expand Down
4 changes: 0 additions & 4 deletions circuitpython_nrf24l01/rf24_lite.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# see license and copyright information in rf24.py
# pylint: disable=missing-docstring
import time
from adafruit_bus_device.spi_device import SPIDevice # type: ignore[import]

Expand Down Expand Up @@ -29,7 +28,6 @@ def __init__(self, spi, csn, ce_pin, spi_frequency=10000000):
self.flush_tx()
self.clear_status_flags()

# pylint: disable=no-member
def _reg_read(self, reg):
in_buf = bytearray([0, 0])
with self._spi as spi:
Expand Down Expand Up @@ -59,8 +57,6 @@ def _reg_write(self, reg, value=None):
spi.write_readinto(out_buf, in_buf)
self._status = in_buf[0]

# pylint: enable=no-member

@property
def ce_pin(self):
return self._ce_pin.value
Expand Down
5 changes: 3 additions & 2 deletions circuitpython_nrf24l01/rf24_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
"""rf24_network module containing the base class RF24Network"""

import time
import struct

Expand Down Expand Up @@ -154,7 +155,7 @@ def _lookup_2_master(self, number: int, lookup_type: int) -> int:
timeout = MESH_LOOKUP_TIMEOUT * 1000000 + time.monotonic_ns()
while self._net_update() not in (MESH_ID_LOOKUP, MESH_ADDR_LOOKUP):
if callable(self.block_less_callback):
self.block_less_callback() # pylint: disable=not-callable
self.block_less_callback()
if time.monotonic_ns() > timeout:
return -1
if lookup_type == MESH_ADDR_LOOKUP:
Expand Down Expand Up @@ -212,7 +213,7 @@ def _get_level(address: int) -> int:
else:
break
if callable(self.block_less_callback):
self.block_less_callback() # pylint: disable=not-callable
self.block_less_callback()
if new_addr is None:
return False
super()._begin(new_addr)
Expand Down
1 change: 1 addition & 0 deletions circuitpython_nrf24l01/rf24_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
"""rf24_network module containing the base class RF24Network"""

try:
from typing import Union
except ImportError:
Expand Down
1 change: 1 addition & 0 deletions circuitpython_nrf24l01/wrapper/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
"""import only accessible API wrappers"""

# ruff: noqa: F401
from adafruit_bus_device.spi_device import SPIDevice # type: ignore[import]
from .cpy_spidev import SPIDevCtx # for linux only
1 change: 1 addition & 0 deletions circuitpython_nrf24l01/wrapper/cpy_spidev.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
"""This module contains a wrapper class for `spidev.SpiDev` in CPython on Linux"""

try:
from typing import Optional
except ImportError:
Expand Down
11 changes: 11 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
coverage:
status:
patch:
default:
informational: true
project:
default:
target: auto
# adjust accordingly based on how flaky your tests are
# this allows a 2% drop from the previous base commit coverage
threshold: 2%
3 changes: 1 addition & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""This file is for `sphinx-build` configuration"""

from importlib.metadata import version as get_version
import os
import sys
Expand Down Expand Up @@ -43,9 +44,7 @@
# General information about the project.
project = "CircuitPython nRF24L01"
author = "Brendan Doherty"
# pylint: disable=redefined-builtin
copyright = f'{time.strftime("%Y", time.localtime())} {author}'
# pylint: enable=redefined-builtin

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
Expand Down
1 change: 1 addition & 0 deletions examples/nrf24l01_ack_payload_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Simple example of using the library to transmit
and retrieve custom automatic acknowledgment payloads.
"""

import time
import board
from digitalio import DigitalInOut
Expand Down
1 change: 1 addition & 0 deletions examples/nrf24l01_context_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
.. warning:: This script is not compatible with the rf24_lite module
"""

import board
from digitalio import DigitalInOut
from circuitpython_nrf24l01.rf24 import RF24
Expand Down
1 change: 1 addition & 0 deletions examples/nrf24l01_fake_ble_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
.. warning:: ATSAMD21 M0-based boards have memory allocation
error when loading 'fake_ble.mpy'
"""

import time
import board
from digitalio import DigitalInOut
Expand Down
1 change: 1 addition & 0 deletions examples/nrf24l01_interrupt_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
.. note:: this script uses the non-blocking `write()` function because
the function `send()` clears the IRQ flags upon returning
"""

import time
import board
import digitalio
Expand Down
1 change: 1 addition & 0 deletions examples/nrf24l01_manual_ack_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Example of using the library manually send Acknowledgement (ACK)
messages without using the nRF24L01's ACK payloads feature.
"""

import time
import board
from digitalio import DigitalInOut
Expand Down
11 changes: 6 additions & 5 deletions examples/nrf24l01_multiceiver_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Simple example of using 1 nRF24L01 to receive data from up to 6 other
transceivers. This technique is called "multiceiver" in the datasheet.
"""

import time
import struct
import board
Expand Down Expand Up @@ -45,11 +46,11 @@
# setup the addresses for all transmitting nRF24L01 nodes
addresses = [
b"\x78" * 5,
b"\xF1\xB6\xB5\xB4\xB3",
b"\xCD\xB6\xB5\xB4\xB3",
b"\xA3\xB6\xB5\xB4\xB3",
b"\x0F\xB6\xB5\xB4\xB3",
b"\x05\xB6\xB5\xB4\xB3",
b"\xf1\xb6\xb5\xb4\xb3",
b"\xcd\xb6\xb5\xb4\xb3",
b"\xa3\xb6\xb5\xb4\xb3",
b"\x0f\xb6\xb5\xb4\xb3",
b"\x05\xb6\xb5\xb4\xb3",
]

# uncomment the following 3 lines for compatibility with TMRh20 library
Expand Down
3 changes: 1 addition & 2 deletions examples/nrf24l01_network_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
An all-purpose example of using the nRF24L01 transceiver in a network of nodes.
"""

import time
import struct
import board
Expand Down Expand Up @@ -152,12 +153,10 @@ def emit(
message += struct.pack("<L", packets_sent[0])
result = False
start = time.monotonic_ns()
# pylint: disable=no-value-for-parameter
if IS_MESH: # send() is a little different for RF24Mesh vs RF24Network
result = nrf.send(node, "M", message)
else:
result = nrf.send(RF24NetworkHeader(node, "T"), message)
# pylint: enable=no-value-for-parameter
end = time.monotonic_ns()
print(
"Sending {} (len {})...".format(packets_sent[0], len(message)),
Expand Down
3 changes: 2 additions & 1 deletion examples/nrf24l01_scanner_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Received Power Detection (RPD) to scan for possible interference.
This example does not require a counterpart node.
"""

import time
import board
from digitalio import DigitalInOut
Expand Down Expand Up @@ -48,7 +49,7 @@
# use reverse engineering tactics for a better "snapshot"
nrf.address_length = 2
nrf.open_rx_pipe(1, b"\0\x55")
nrf.open_rx_pipe(0, b"\0\xAA")
nrf.open_rx_pipe(0, b"\0\xaa")


def scan(timeout=30):
Expand Down
1 change: 1 addition & 0 deletions examples/nrf24l01_simple_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Simple example of using the RF24 class.
"""

import time
import struct
import board
Expand Down
1 change: 1 addition & 0 deletions examples/nrf24l01_stream_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Example of library usage for streaming multiple payloads.
"""

import time
import board
from digitalio import DigitalInOut
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""All setup/install info is now in pyproject.toml"""

from setuptools import setup

setup()
Loading

0 comments on commit a861db9

Please sign in to comment.