Skip to content

change to ruff #29

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

Merged
merged 1 commit into from
May 16, 2025
Merged
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
11 changes: 11 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# SPDX-FileCopyrightText: 2024 Justin Myers for Adafruit Industries
#
# SPDX-License-Identifier: Unlicense

.py text eol=lf
.rst text eol=lf
.txt text eol=lf
.yaml text eol=lf
.toml text eol=lf
.license text eol=lf
.md text eol=lf
43 changes: 11 additions & 32 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,42 +1,21 @@
# SPDX-FileCopyrightText: 2020 Diego Elio Pettenò
# SPDX-FileCopyrightText: 2024 Justin Myers for Adafruit Industries
#
# SPDX-License-Identifier: Unlicense

repos:
- repo: https://github.com/python/black
rev: 23.3.0
hooks:
- id: black
- repo: https://github.com/fsfe/reuse-tool
rev: v1.1.2
hooks:
- id: reuse
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v4.5.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/pycqa/pylint
rev: v2.17.4
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.4
hooks:
- id: pylint
name: pylint (library code)
types: [python]
args:
- --disable=consider-using-f-string
exclude: "^(docs/|examples/|tests/|setup.py$)"
- id: pylint
name: pylint (example code)
description: Run pylint rules on "examples/*.py" files
types: [python]
files: "^examples/"
args:
- --disable=missing-docstring,invalid-name,consider-using-f-string,duplicate-code
- id: pylint
name: pylint (test code)
description: Run pylint rules on "tests/*.py" files
types: [python]
files: "^tests/"
args:
- --disable=missing-docstring,consider-using-f-string,duplicate-code
- id: ruff-format
- id: ruff
args: ["--fix"]
- repo: https://github.com/fsfe/reuse-tool
rev: v3.0.1
hooks:
- id: reuse
399 changes: 0 additions & 399 deletions .pylintrc

This file was deleted.

6 changes: 3 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
@@ -13,9 +13,9 @@ Introduction
:target: https://github.com/adafruit/Adafruit_CircuitPython_miniesptool/actions/
:alt: Build Status

.. image:: https://img.shields.io/badge/code%20style-black-000000.svg
:target: https://github.com/psf/black
:alt: Code Style: Black
.. image:: https://img.shields.io/endpoint?url=https://github.com/raw/astral-sh/ruff/main/assets/badge/v2.json
:target: https://github.com/astral-sh/ruff
:alt: Code Style: Ruff

ROM loader for ESP chips, works with ESP8266 or ESP32.
This is a 'no-stub' loader, so you can't read MD5 or firmware back on ESP8266.
51 changes: 21 additions & 30 deletions adafruit_miniesptool.py
Original file line number Diff line number Diff line change
@@ -34,8 +34,9 @@
"""

import os
import time
import struct
import time

from digitalio import Direction

__version__ = "0.0.0+auto.0"
@@ -77,7 +78,7 @@
}


class miniesptool: # pylint: disable=invalid-name
class miniesptool:
"""A miniature version of esptool, a programming command line tool for
ESP8266 and ESP32 chips. This version is minimized to work on CircuitPython
boards, so you can burn ESP firmware direct from the CPy disk drive. Handy
@@ -91,11 +92,11 @@ class miniesptool: # pylint: disable=invalid-name
def __init__(
self,
uart,
gpio0_pin, # pylint: disable=too-many-arguments
gpio0_pin,
reset_pin,
*,
flashsize,
baudrate=ESP_ROM_BAUD
baudrate=ESP_ROM_BAUD,
):
gpio0_pin.direction = Direction.OUTPUT
reset_pin.direction = Direction.OUTPUT
@@ -196,7 +197,7 @@ def chip_type(self):
def chip_name(self):
"""The specific name of the chip, e.g. ESP8266EX, to the best
of our ability to determine without a stub bootloader."""
self.chip_type # pylint: disable=pointless-statement
self.chip_type
self._read_efuses()

if self.chip_type == ESP32:
@@ -241,9 +242,7 @@ def flash_begin(self, *, size=0, offset=0):
if self._chipfamily == ESP32:
self.check_command(ESP_SPI_ATTACH, bytes([0] * 8))
# We are hardcoded for 4MB flash on ESP32
buffer = struct.pack(
"<IIIIII", 0, self._flashsize, 0x10000, 4096, 256, 0xFFFF
)
buffer = struct.pack("<IIIIII", 0, self._flashsize, 0x10000, 4096, 256, 0xFFFF)
self.check_command(ESP_SPI_SET_PARAMS, buffer)

num_blocks = (size + self.FLASH_WRITE_SIZE - 1) // self.FLASH_WRITE_SIZE
@@ -253,25 +252,18 @@ def flash_begin(self, *, size=0, offset=0):
erase_size = size
timeout = 13
stamp = time.monotonic()
buffer = struct.pack(
"<IIII", erase_size, num_blocks, self.FLASH_WRITE_SIZE, offset
)
buffer = struct.pack("<IIII", erase_size, num_blocks, self.FLASH_WRITE_SIZE, offset)
print(
"Erase size %d, num_blocks %d, size %d, offset 0x%04x"
% (erase_size, num_blocks, self.FLASH_WRITE_SIZE, offset)
)

self.check_command(ESP_FLASH_BEGIN, buffer, timeout=timeout)
if size != 0:
print(
"Took %.2fs to erase %d flash blocks"
% (time.monotonic() - stamp, num_blocks)
)
print("Took %.2fs to erase %d flash blocks" % (time.monotonic() - stamp, num_blocks))
return num_blocks

def check_command(
self, opcode, buffer, checksum=0, timeout=0.1
): # pylint: disable=unused-argument
def check_command(self, opcode, buffer, checksum=0, timeout=0.1):
"""Send a command packet, check that the command succeeded and
return a tuple with the value and data.
See the ESP Serial Protocol for more details on what value/data are"""
@@ -281,9 +273,8 @@ def check_command(
status_len = 2
elif self._chipfamily == ESP32:
status_len = 4
else:
if len(data) in (2, 4):
status_len = len(data)
elif len(data) in {2, 4}:
status_len = len(data)
if data is None or len(data) < status_len:
raise RuntimeError("Didn't get enough status bytes")
status = data[-status_len:]
@@ -317,7 +308,7 @@ def send_command(self, opcode, buffer):
print("Writing:", bytearray(packet))
self._uart.write(bytearray(packet))

def get_response(self, opcode, timeout=0.1): # pylint: disable=too-many-branches
def get_response(self, opcode, timeout=0.1):
"""Read response data and decodes the slip packet, then parses
out the value/data and returns as a tuple of (value, data) where
each is a list of bytes"""
@@ -328,14 +319,14 @@ def get_response(self, opcode, timeout=0.1): # pylint: disable=too-many-branche
escaped_byte = False
while (time.monotonic() - stamp) < timeout:
if self._uart.in_waiting > 0:
c = self._uart.read(1) # pylint: disable=invalid-name
if c == b"\xDB":
c = self._uart.read(1)
if c == b"\xdb":
escaped_byte = True
elif escaped_byte:
if c == b"\xDD":
reply += b"\xDB"
elif c == b"\xDC":
reply += b"\xC0"
if c == b"\xdd":
reply += b"\xdb"
elif c == b"\xdc":
reply += b"\xc0"
else:
reply += [0xDB, c]
escaped_byte = False
@@ -356,7 +347,7 @@ def get_response(self, opcode, timeout=0.1): # pylint: disable=too-many-branche
# Check to see if we have a complete packet. If not, we timed out.
if len(reply) != packet_length + 10:
if self._debug:
print("Timed out after {} seconds".format(timeout))
print(f"Timed out after {timeout} seconds")
return (None, None)
if self._debug:
print("Packet:", [hex(i) for i in reply])
@@ -434,7 +425,7 @@ def _sync(self):
any hardware resetting"""
self.send_command(0x08, SYNC_PACKET)
for _ in range(8):
reply, data = self.get_response( # pylint: disable=unused-variable
reply, data = self.get_response( # noqa: F841
0x08, 0.1
)
if not data:
3 changes: 3 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
@@ -4,5 +4,8 @@
.. If your library file(s) are nested in a directory (e.g. /adafruit_foo/foo.py)
.. use this format as the module name: "adafruit_foo.foo"
API Reference
#############

.. automodule:: adafruit_miniesptool
:members:
8 changes: 2 additions & 6 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
# -*- coding: utf-8 -*-

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
#
# SPDX-License-Identifier: MIT

import datetime
import os
import sys
import datetime

sys.path.insert(0, os.path.abspath(".."))

@@ -48,9 +46,7 @@
creation_year = "2018"
current_year = str(datetime.datetime.now().year)
year_duration = (
current_year
if current_year == creation_year
else creation_year + " - " + current_year
current_year if current_year == creation_year else creation_year + " - " + current_year
)
copyright = year_duration + " ladyada"
author = "ladyada"
27 changes: 8 additions & 19 deletions examples/miniesptool_esp32argon.py
Original file line number Diff line number Diff line change
@@ -2,10 +2,11 @@
# SPDX-License-Identifier: MIT

import time

import board
import busio
from digitalio import DigitalInOut, Direction

from digitalio import DigitalInOut, Direction # pylint: disable=unused-import
import adafruit_miniesptool

print("ESP32 mini prog")
@@ -24,9 +25,7 @@
esp_cts.direction = Direction.OUTPUT
esp_cts.value = False

esptool = adafruit_miniesptool.miniesptool(
uart, gpio0pin, resetpin, flashsize=4 * 1024 * 1024
)
esptool = adafruit_miniesptool.miniesptool(uart, gpio0pin, resetpin, flashsize=4 * 1024 * 1024)
esptool.debug = False

esptool.sync()
@@ -38,18 +37,12 @@
print("MAC ADDR: ", [hex(i) for i in esptool.mac_addr])

# 0x10000 ota_data_initial.bin
esptool.flash_file(
"esp32/ota_data_initial.bin", 0x10000, "84d04c9d6cc8ef35bf825d51a5277699"
)
esptool.flash_file("esp32/ota_data_initial.bin", 0x10000, "84d04c9d6cc8ef35bf825d51a5277699")

# 0x1000 bootloader/bootloader.bin
esptool.flash_file(
"esp32/bootloader/bootloader.bin", 0x1000, "195dae16eda6ab703a45928182baa863"
)
esptool.flash_file("esp32/bootloader/bootloader.bin", 0x1000, "195dae16eda6ab703a45928182baa863")
# 0x20000 at_customize.bin
esptool.flash_file(
"esp32/at_customize.bin", 0x20000, "9853055e077ba0c90cd70691b9d8c3d5"
)
esptool.flash_file("esp32/at_customize.bin", 0x20000, "9853055e077ba0c90cd70691b9d8c3d5")

# 0x24000 customized_partitions/server_cert.bin
esptool.flash_file(
@@ -94,17 +87,13 @@
)

# 0xf000 phy_init_data.bin
esptool.flash_file(
"esp32/phy_init_data.bin", 0xF000, "bc9854aa3687ca73e25d213d20113b23"
)
esptool.flash_file("esp32/phy_init_data.bin", 0xF000, "bc9854aa3687ca73e25d213d20113b23")

# 0x100000 esp-at.bin
esptool.flash_file("esp32/esp-at.bin", 0x100000, "ae256e4ab546354cd8dfa241e1056996")

# 0x8000 partitions_at.bin
esptool.flash_file(
"esp32/partitions_at.bin", 0x8000, "d3d1508993d61aedf17280140fc22a6b"
)
esptool.flash_file("esp32/partitions_at.bin", 0x8000, "d3d1508993d61aedf17280140fc22a6b")

esptool.reset()
time.sleep(0.5)
27 changes: 8 additions & 19 deletions examples/miniesptool_esp32multifile.py
Original file line number Diff line number Diff line change
@@ -2,10 +2,11 @@
# SPDX-License-Identifier: MIT

import time

import board
import busio
from digitalio import DigitalInOut, Direction

from digitalio import DigitalInOut, Direction # pylint: disable=unused-import
import adafruit_miniesptool

print("ESP32 mini prog")
@@ -25,9 +26,7 @@
esp_cts.value = False
"""

esptool = adafruit_miniesptool.miniesptool(
uart, gpio0pin, resetpin, flashsize=4 * 1024 * 1024
)
esptool = adafruit_miniesptool.miniesptool(uart, gpio0pin, resetpin, flashsize=4 * 1024 * 1024)
esptool.debug = False

esptool.sync()
@@ -39,18 +38,12 @@
print("MAC ADDR: ", [hex(i) for i in esptool.mac_addr])

# 0x10000 ota_data_initial.bin
esptool.flash_file(
"esp32/ota_data_initial.bin", 0x10000, "84d04c9d6cc8ef35bf825d51a5277699"
)
esptool.flash_file("esp32/ota_data_initial.bin", 0x10000, "84d04c9d6cc8ef35bf825d51a5277699")

# 0x1000 bootloader/bootloader.bin
esptool.flash_file(
"esp32/bootloader/bootloader.bin", 0x1000, "894e5f067a44773ac1ae987a14e85787"
)
esptool.flash_file("esp32/bootloader/bootloader.bin", 0x1000, "894e5f067a44773ac1ae987a14e85787")
# 0x20000 at_customize.bin
esptool.flash_file(
"esp32/at_customize.bin", 0x20000, "9853055e077ba0c90cd70691b9d8c3d5"
)
esptool.flash_file("esp32/at_customize.bin", 0x20000, "9853055e077ba0c90cd70691b9d8c3d5")

# 0x24000 customized_partitions/server_cert.bin
esptool.flash_file(
@@ -95,17 +88,13 @@
)

# 0xf000 phy_init_data.bin
esptool.flash_file(
"esp32/phy_init_data.bin", 0xF000, "bc9854aa3687ca73e25d213d20113b23"
)
esptool.flash_file("esp32/phy_init_data.bin", 0xF000, "bc9854aa3687ca73e25d213d20113b23")

# 0x100000 esp-at.bin
esptool.flash_file("esp32/esp-at.bin", 0x100000, "7018a1b4c8a5c108377ecda7632b899c")

# 0x8000 partitions_at.bin
esptool.flash_file(
"esp32/partitions_at.bin", 0x8000, "d3d1508993d61aedf17280140fc22a6b"
)
esptool.flash_file("esp32/partitions_at.bin", 0x8000, "d3d1508993d61aedf17280140fc22a6b")

esptool.reset()
time.sleep(0.5)
2 changes: 2 additions & 0 deletions examples/miniesptool_esp8266program.py
Original file line number Diff line number Diff line change
@@ -2,9 +2,11 @@
# SPDX-License-Identifier: MIT

import time

import board
import busio
from digitalio import DigitalInOut

import adafruit_miniesptool

print("ESP8266 mini prog")
4 changes: 3 additions & 1 deletion examples/miniesptool_simpletest.py
Original file line number Diff line number Diff line change
@@ -2,9 +2,11 @@
# SPDX-License-Identifier: MIT

import time

import board
import busio
from digitalio import DigitalInOut, Direction # pylint: disable=unused-import
from digitalio import DigitalInOut, Direction

import adafruit_miniesptool

print("ESP32 Nina-FW")
108 changes: 108 additions & 0 deletions ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# SPDX-FileCopyrightText: 2024 Tim Cocks for Adafruit Industries
#
# SPDX-License-Identifier: MIT

target-version = "py38"
line-length = 100

[lint]
preview = true
select = ["I", "PL", "UP"]

extend-select = [
"D419", # empty-docstring
"E501", # line-too-long
"W291", # trailing-whitespace
"PLC0414", # useless-import-alias
"PLC2401", # non-ascii-name
"PLC2801", # unnecessary-dunder-call
"PLC3002", # unnecessary-direct-lambda-call
"E999", # syntax-error
"PLE0101", # return-in-init
"F706", # return-outside-function
"F704", # yield-outside-function
"PLE0116", # continue-in-finally
"PLE0117", # nonlocal-without-binding
"PLE0241", # duplicate-bases
"PLE0302", # unexpected-special-method-signature
"PLE0604", # invalid-all-object
"PLE0605", # invalid-all-format
"PLE0643", # potential-index-error
"PLE0704", # misplaced-bare-raise
"PLE1141", # dict-iter-missing-items
"PLE1142", # await-outside-async
"PLE1205", # logging-too-many-args
"PLE1206", # logging-too-few-args
"PLE1307", # bad-string-format-type
"PLE1310", # bad-str-strip-call
"PLE1507", # invalid-envvar-value
"PLE2502", # bidirectional-unicode
"PLE2510", # invalid-character-backspace
"PLE2512", # invalid-character-sub
"PLE2513", # invalid-character-esc
"PLE2514", # invalid-character-nul
"PLE2515", # invalid-character-zero-width-space
"PLR0124", # comparison-with-itself
"PLR0202", # no-classmethod-decorator
"PLR0203", # no-staticmethod-decorator
"UP004", # useless-object-inheritance
"PLR0206", # property-with-parameters
"PLR0904", # too-many-public-methods
"PLR0911", # too-many-return-statements
"PLR0912", # too-many-branches
"PLR0913", # too-many-arguments
"PLR0914", # too-many-locals
"PLR0915", # too-many-statements
"PLR0916", # too-many-boolean-expressions
"PLR1702", # too-many-nested-blocks
"PLR1704", # redefined-argument-from-local
"PLR1711", # useless-return
"C416", # unnecessary-comprehension
"PLR1733", # unnecessary-dict-index-lookup
"PLR1736", # unnecessary-list-index-lookup

# ruff reports this rule is unstable
#"PLR6301", # no-self-use

"PLW0108", # unnecessary-lambda
"PLW0120", # useless-else-on-loop
"PLW0127", # self-assigning-variable
"PLW0129", # assert-on-string-literal
"B033", # duplicate-value
"PLW0131", # named-expr-without-context
"PLW0245", # super-without-brackets
"PLW0406", # import-self
"PLW0602", # global-variable-not-assigned
"PLW0603", # global-statement
"PLW0604", # global-at-module-level

# fails on the try: import typing used by libraries
#"F401", # unused-import

"F841", # unused-variable
"E722", # bare-except
"PLW0711", # binary-op-exception
"PLW1501", # bad-open-mode
"PLW1508", # invalid-envvar-default
"PLW1509", # subprocess-popen-preexec-fn
"PLW2101", # useless-with-lock
"PLW3301", # nested-min-max
]

ignore = [
"PLR2004", # magic-value-comparison
"UP030", # format literals
"PLW1514", # unspecified-encoding
"PLR0913", # too-many-arguments
"PLR0915", # too-many-statements
"PLR0917", # too-many-positional-arguments
"PLR0904", # too-many-public-methods
"PLR0912", # too-many-branches
"PLR0916", # too-many-boolean-expressions
"PLR6301", # could-be-static no-self-use
"PLC0415", # import outside toplevel
"PLC2701", # private import
]

[format]
line-ending = "lf"