Skip to content

Commit 25d5071

Browse files
committedMay 15, 2025·
change to ruff
1 parent a6598ca commit 25d5071

13 files changed

+161
-493
lines changed
 

‎.gitattributes

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# SPDX-FileCopyrightText: 2024 Justin Myers for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: Unlicense
4+
5+
.py text eol=lf
6+
.rst text eol=lf
7+
.txt text eol=lf
8+
.yaml text eol=lf
9+
.toml text eol=lf
10+
.license text eol=lf
11+
.md text eol=lf

‎.pre-commit-config.yaml

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,21 @@
1-
# SPDX-FileCopyrightText: 2020 Diego Elio Pettenò
1+
# SPDX-FileCopyrightText: 2024 Justin Myers for Adafruit Industries
22
#
33
# SPDX-License-Identifier: Unlicense
44

55
repos:
6-
- repo: https://github.com/python/black
7-
rev: 23.3.0
8-
hooks:
9-
- id: black
10-
- repo: https://github.com/fsfe/reuse-tool
11-
rev: v1.1.2
12-
hooks:
13-
- id: reuse
146
- repo: https://github.com/pre-commit/pre-commit-hooks
15-
rev: v4.4.0
7+
rev: v4.5.0
168
hooks:
179
- id: check-yaml
1810
- id: end-of-file-fixer
1911
- id: trailing-whitespace
20-
- repo: https://github.com/pycqa/pylint
21-
rev: v2.17.4
12+
- repo: https://github.com/astral-sh/ruff-pre-commit
13+
rev: v0.3.4
2214
hooks:
23-
- id: pylint
24-
name: pylint (library code)
25-
types: [python]
26-
args:
27-
- --disable=consider-using-f-string
28-
exclude: "^(docs/|examples/|tests/|setup.py$)"
29-
- id: pylint
30-
name: pylint (example code)
31-
description: Run pylint rules on "examples/*.py" files
32-
types: [python]
33-
files: "^examples/"
34-
args:
35-
- --disable=missing-docstring,invalid-name,consider-using-f-string,duplicate-code
36-
- id: pylint
37-
name: pylint (test code)
38-
description: Run pylint rules on "tests/*.py" files
39-
types: [python]
40-
files: "^tests/"
41-
args:
42-
- --disable=missing-docstring,consider-using-f-string,duplicate-code
15+
- id: ruff-format
16+
- id: ruff
17+
args: ["--fix"]
18+
- repo: https://github.com/fsfe/reuse-tool
19+
rev: v3.0.1
20+
hooks:
21+
- id: reuse

‎.pylintrc

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

‎README.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ Introduction
1414
:target: https://github.com/adafruit/Adafruit_CircuitPython_AVRprog/actions/
1515
:alt: Build Status
1616

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

2121
Program your favorite AVR chips directly from CircuitPython with this handy helper class that will let you make stand-alone programmers right from your REPL. Should work with any/all AVR chips, via SPI programming. Tested with ATmega328, ATtiny85 and ATmega2560
2222

‎adafruit_avrprog.py

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,17 @@
3333
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_AVRprog.git"
3434

3535
try:
36-
from typing import List, Optional, Tuple, Union, TypedDict
37-
from typing_extensions import TypeAlias
3836
from os import PathLike
37+
from typing import List, Optional, Tuple, TypedDict, Union
38+
3939
from busio import SPI
4040
from microcontroller import Pin
41+
from typing_extensions import TypeAlias
4142

4243
# Technically this type should come from: from _typeshed import FileDescriptorOrPath
4344
# Unfortunately _typeshed is only in the standard library in newer releases of Python, e.g. 3.11
4445
# Thus have to define a placeholder
45-
FileDescriptorOrPath: TypeAlias = Union[
46-
int, str, bytes, PathLike[str], PathLike[bytes]
47-
]
46+
FileDescriptorOrPath: TypeAlias = Union[int, str, bytes, PathLike[str], PathLike[bytes]]
4847

4948
from io import TextIOWrapper
5049

@@ -64,7 +63,6 @@ class FileState(TypedDict):
6463
Dictionary representing a File State
6564
"""
6665

67-
# pylint: disable=invalid-name
6866
line: int
6967
ext_addr: int
7068
eof: bool
@@ -90,7 +88,6 @@ class Boards:
9088
Some well known board definitions.
9189
"""
9290

93-
# pylint: disable=too-few-public-methods
9491
ATtiny13a = {
9592
"name": "ATtiny13a",
9693
"sig": [0x1E, 0x90, 0x07],
@@ -158,12 +155,11 @@ def verify_sig(self, chip: ChipDictionary, verbose: bool = False) -> bool:
158155
sig = self.read_signature()
159156
self.end()
160157
if verbose:
161-
print("Found signature: %s" % [hex(i) for i in sig])
158+
print(f"Found signature: {[hex(i) for i in sig]}")
162159
if sig != chip["sig"]:
163160
return False
164161
return True
165162

166-
# pylint: disable=too-many-branches
167163
def program_file(
168164
self,
169165
chip: ChipDictionary,
@@ -188,12 +184,12 @@ def program_file(
188184

189185
# create a file state dictionary
190186
file_state = {"line": 0, "ext_addr": 0, "eof": False, "f": None}
191-
with open(file_name, "r") as file_state["f"]:
187+
with open(file_name) as file_state["f"]:
192188
page_size = chip["page_size"]
193189

194190
for page_addr in range(0, chip["flash_size"], page_size):
195191
if verbose:
196-
print("Programming page $%04X..." % page_addr, end="")
192+
print(f"Programming page ${page_addr:04X}...", end="")
197193
page_buffer = bytearray(page_size)
198194
for b in range(page_size):
199195
page_buffer[b] = 0xFF # make an empty page
@@ -214,19 +210,16 @@ def program_file(
214210
continue
215211

216212
if verbose:
217-
print("Verifying page @ $%04X" % page_addr)
213+
print(f"Verifying page @ ${page_addr:04X}")
218214
read_buffer = bytearray(page_size)
219215
self.read(page_addr, read_buffer)
220216
# print("From memory: ", read_buffer)
221217

222218
if page_buffer != read_buffer:
223219
if verbose:
224-
# pylint: disable=line-too-long
225220
print(
226-
"Verify fail at address %04X\nPage should be: %s\nBut contains: %s"
227-
% (page_addr, page_buffer, read_buffer)
221+
f"Verify fail at address {page_addr:04X}\nPage should be: {page_buffer}\nBut contains: {read_buffer}"
228222
)
229-
# pylint: enable=line-too-long
230223
self.end()
231224
return False
232225

@@ -251,7 +244,7 @@ def verify_file(
251244

252245
# create a file state dictionary
253246
file_state = {"line": 0, "ext_addr": 0, "eof": False, "f": None}
254-
with open(file_name, "r") as file_state["f"]:
247+
with open(file_name) as file_state["f"]:
255248
page_size = chip["page_size"]
256249
clock_speed = chip.get("clock_speed", _FAST_CLOCK)
257250
self.begin(clock=clock_speed)
@@ -263,20 +256,17 @@ def verify_file(
263256
read_hex_page(file_state, page_addr, page_size, page_buffer)
264257

265258
if verbose:
266-
print("Verifying page @ $%04X" % page_addr)
259+
print(f"Verifying page @ ${page_addr:04X}")
267260
read_buffer = bytearray(page_size)
268261
self.read(page_addr, read_buffer)
269262
# print("From memory: ", read_buffer)
270263
# print("From file : ", page_buffer)
271264

272265
if page_buffer != read_buffer:
273266
if verbose:
274-
# pylint: disable=line-too-long
275267
print(
276-
"Verify fail at address %04X\nPage should be: %s\nBut contains: %s"
277-
% (page_addr, page_buffer, read_buffer)
268+
f"Verify fail at address {page_addr:04X}\nPage should be: {page_buffer}\nBut contains: {read_buffer}"
278269
)
279-
# pylint: enable=line-too-long
280270
self.end()
281271
return False
282272

@@ -300,7 +290,6 @@ def read_fuses(self, chip: ChipDictionary) -> Tuple[int, int, int, int]:
300290
self.end()
301291
return (low, high, ext, lock)
302292

303-
# pylint: disable=unused-argument,too-many-arguments
304293
def write_fuses(
305294
self,
306295
chip: ChipDictionary,
@@ -322,7 +311,6 @@ def write_fuses(
322311
self._busy_wait()
323312
self.end()
324313

325-
# pylint: disable=too-many-arguments
326314
def verify_fuses(
327315
self,
328316
chip: ChipDictionary,
@@ -413,9 +401,7 @@ def _flash_word(self, addr: int, low: int, high: int) -> None:
413401
self._transaction((0x40, addr >> 8, addr, low))
414402
self._transaction((0x48, addr >> 8, addr, high))
415403

416-
def _flash_page(
417-
self, page_buffer: bytearray, page_addr: int, page_size: int
418-
) -> None:
404+
def _flash_page(self, page_buffer: bytearray, page_addr: int, page_size: int) -> None:
419405
page_addr //= 2 # address is by 'words' not bytes!
420406
for i in range(page_size // 2): # page indexed by words, not bytes
421407
lo_byte, hi_byte = page_buffer[2 * i : 2 * i + 2]
@@ -448,7 +434,6 @@ def _busy_wait(self) -> None:
448434
def read_hex_page(
449435
file_state: FileState, page_addr: int, page_size: int, page_buffer: bytearray
450436
) -> bool:
451-
# pylint: disable=too-many-branches
452437
"""
453438
Helper function that does the Intel Hex parsing. Takes in a dictionary
454439
that contains the file 'state'. The dictionary should have file_state['f']
@@ -486,9 +471,7 @@ def read_hex_page(
486471
file_state["line_addr"] = line_addr
487472
rec_type = int(line[7:9], 16)
488473
except ValueError as err:
489-
raise RuntimeError(
490-
"Could not parse HEX line %d addr" % file_state["line"]
491-
) from err
474+
raise RuntimeError("Could not parse HEX line %d addr" % file_state["line"]) from err
492475

493476
if file_state["ext_addr"]:
494477
line_addr += file_state["ext_addr"]
@@ -534,13 +517,7 @@ def read_hex_page(
534517
byte_buffer.append(int(line[9 + i * 2 : 11 + i * 2], 16))
535518

536519
# check chksum now!
537-
chksum = (
538-
hex_len
539-
+ (line_addr >> 8)
540-
+ (line_addr & 0xFF)
541-
+ rec_type
542-
+ sum(byte_buffer)
543-
)
520+
chksum = hex_len + (line_addr >> 8) + (line_addr & 0xFF) + rec_type + sum(byte_buffer)
544521
# print("checksum: "+hex(chksum))
545522
if (chksum & 0xFF) != 0:
546523
raise RuntimeError("HEX Checksum fail")

‎docs/api.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11

22
.. If you created a package, create one automodule per module in the package.
33
4+
API Reference
5+
#############
6+
47
.. automodule:: adafruit_avrprog
58
:members:

‎docs/conf.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
# -*- coding: utf-8 -*-
2-
31
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
42
#
53
# SPDX-License-Identifier: MIT
64

5+
import datetime
76
import os
87
import sys
9-
import datetime
108

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

@@ -45,9 +43,7 @@
4543
creation_year = "2017"
4644
current_year = str(datetime.datetime.now().year)
4745
year_duration = (
48-
current_year
49-
if current_year == creation_year
50-
else creation_year + " - " + current_year
46+
current_year if current_year == creation_year else creation_year + " - " + current_year
5147
)
5248
copyright = year_duration + " ladyada"
5349
author = "ladyada"

‎examples/avrprog_program_mega2560.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import board
1616
import busio
17+
1718
import adafruit_avrprog
1819

1920
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
@@ -54,15 +55,10 @@ def error(err):
5455

5556
avrprog.write_fuses(atmega2560, low=0xFF, high=0xD8, ext=0x05, lock=0x3F)
5657
if not avrprog.verify_fuses(atmega2560, low=0xFF, high=0xD8, ext=0x05, lock=0x3F):
57-
error(
58-
"Failure programming fuses: "
59-
+ str([hex(i) for i in avrprog.read_fuses(atmega2560)])
60-
)
58+
error("Failure programming fuses: " + str([hex(i) for i in avrprog.read_fuses(atmega2560)]))
6159

6260
print("Programming flash from file")
63-
avrprog.program_file(
64-
atmega2560, "stk500boot_v2_mega2560.hex", verbose=True, verify=True
65-
)
61+
avrprog.program_file(atmega2560, "stk500boot_v2_mega2560.hex", verbose=True, verify=True)
6662

6763
avrprog.write_fuses(atmega2560, lock=0x0F)
6864
if not avrprog.verify_fuses(atmega2560, lock=0x0F):

‎examples/avrprog_program_tiny13a.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import board
1616
import busio
17+
1718
import adafruit_avrprog
1819

1920
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)

‎examples/avrprog_program_trinket85.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import board
1616
import busio
17+
1718
import adafruit_avrprog
1819

1920
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)

‎examples/avrprog_program_uno328.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,15 @@
1515
import board
1616
import busio
1717
import pwmio
18+
1819
import adafruit_avrprog
1920

2021
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
2122
avrprog = adafruit_avrprog.AVRprog()
2223
avrprog.init(spi, board.D5)
2324

24-
# pylint: disable-msg=no-member
2525
# we can generate an 6 MHz clock for driving bare chips too!
2626
clock_pwm = pwmio.PWMOut(board.D9, frequency=6000000, duty_cycle=65536 // 2)
27-
# pylint: enable-msg=no-member
2827

2928
# Each chip has to have a definition so the script knows how to find it
3029
atmega328p = avrprog.Boards.ATmega328p
@@ -50,10 +49,7 @@ def error(err):
5049

5150
avrprog.write_fuses(atmega328p, low=0xFF, high=0xDE, ext=0x05, lock=0x3F)
5251
if not avrprog.verify_fuses(atmega328p, low=0xFF, high=0xDE, ext=0x05, lock=0x3F):
53-
error(
54-
"Failure programming fuses: "
55-
+ str([hex(i) for i in avrprog.read_fuses(atmega328p)])
56-
)
52+
error("Failure programming fuses: " + str([hex(i) for i in avrprog.read_fuses(atmega328p)]))
5753

5854
print("Programming flash from file")
5955
avrprog.program_file(atmega328p, "optiboot_atmega328.hex", verbose=True, verify=True)

‎examples/avrprog_read_signature_simpletest.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,15 @@
99
import board
1010
import busio
1111
import pwmio
12+
1213
import adafruit_avrprog
1314

1415
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
1516
avrprog = adafruit_avrprog.AVRprog()
1617
avrprog.init(spi, board.D5)
1718

18-
# pylint: disable-msg=no-member
1919
# we can generate an 6 MHz clock for driving bare chips too!
2020
clock_pwm = pwmio.PWMOut(board.D9, frequency=6000000, duty_cycle=65536 // 2)
21-
# pylint: enable-msg=no-member
2221

2322
avrprog.begin()
2423
print("Signature bytes: ", [hex(i) for i in avrprog.read_signature()])

‎ruff.toml

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# SPDX-FileCopyrightText: 2024 Tim Cocks for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
target-version = "py38"
6+
line-length = 100
7+
8+
[lint]
9+
preview = true
10+
select = ["I", "PL", "UP"]
11+
12+
extend-select = [
13+
"D419", # empty-docstring
14+
"E501", # line-too-long
15+
"W291", # trailing-whitespace
16+
"PLC0414", # useless-import-alias
17+
"PLC2401", # non-ascii-name
18+
"PLC2801", # unnecessary-dunder-call
19+
"PLC3002", # unnecessary-direct-lambda-call
20+
"E999", # syntax-error
21+
"PLE0101", # return-in-init
22+
"F706", # return-outside-function
23+
"F704", # yield-outside-function
24+
"PLE0116", # continue-in-finally
25+
"PLE0117", # nonlocal-without-binding
26+
"PLE0241", # duplicate-bases
27+
"PLE0302", # unexpected-special-method-signature
28+
"PLE0604", # invalid-all-object
29+
"PLE0605", # invalid-all-format
30+
"PLE0643", # potential-index-error
31+
"PLE0704", # misplaced-bare-raise
32+
"PLE1141", # dict-iter-missing-items
33+
"PLE1142", # await-outside-async
34+
"PLE1205", # logging-too-many-args
35+
"PLE1206", # logging-too-few-args
36+
"PLE1307", # bad-string-format-type
37+
"PLE1310", # bad-str-strip-call
38+
"PLE1507", # invalid-envvar-value
39+
"PLE2502", # bidirectional-unicode
40+
"PLE2510", # invalid-character-backspace
41+
"PLE2512", # invalid-character-sub
42+
"PLE2513", # invalid-character-esc
43+
"PLE2514", # invalid-character-nul
44+
"PLE2515", # invalid-character-zero-width-space
45+
"PLR0124", # comparison-with-itself
46+
"PLR0202", # no-classmethod-decorator
47+
"PLR0203", # no-staticmethod-decorator
48+
"UP004", # useless-object-inheritance
49+
"PLR0206", # property-with-parameters
50+
"PLR0904", # too-many-public-methods
51+
"PLR0911", # too-many-return-statements
52+
"PLR0912", # too-many-branches
53+
"PLR0913", # too-many-arguments
54+
"PLR0914", # too-many-locals
55+
"PLR0915", # too-many-statements
56+
"PLR0916", # too-many-boolean-expressions
57+
"PLR1702", # too-many-nested-blocks
58+
"PLR1704", # redefined-argument-from-local
59+
"PLR1711", # useless-return
60+
"C416", # unnecessary-comprehension
61+
"PLR1733", # unnecessary-dict-index-lookup
62+
"PLR1736", # unnecessary-list-index-lookup
63+
64+
# ruff reports this rule is unstable
65+
#"PLR6301", # no-self-use
66+
67+
"PLW0108", # unnecessary-lambda
68+
"PLW0120", # useless-else-on-loop
69+
"PLW0127", # self-assigning-variable
70+
"PLW0129", # assert-on-string-literal
71+
"B033", # duplicate-value
72+
"PLW0131", # named-expr-without-context
73+
"PLW0245", # super-without-brackets
74+
"PLW0406", # import-self
75+
"PLW0602", # global-variable-not-assigned
76+
"PLW0603", # global-statement
77+
"PLW0604", # global-at-module-level
78+
79+
# fails on the try: import typing used by libraries
80+
#"F401", # unused-import
81+
82+
"F841", # unused-variable
83+
"E722", # bare-except
84+
"PLW0711", # binary-op-exception
85+
"PLW1501", # bad-open-mode
86+
"PLW1508", # invalid-envvar-default
87+
"PLW1509", # subprocess-popen-preexec-fn
88+
"PLW2101", # useless-with-lock
89+
"PLW3301", # nested-min-max
90+
]
91+
92+
ignore = [
93+
"PLR2004", # magic-value-comparison
94+
"UP030", # format literals
95+
"PLW1514", # unspecified-encoding
96+
"PLR0913", # too-many-arguments
97+
"PLR0915", # too-many-statements
98+
"PLR0917", # too-many-positional-arguments
99+
"PLR0904", # too-many-public-methods
100+
"PLR0912", # too-many-branches
101+
"PLR0916", # too-many-boolean-expressions
102+
"PLR6301", # could-be-static no-self-use
103+
"PLC0415", # import outside toplevel
104+
"E501", # line too long
105+
]
106+
107+
[format]
108+
line-ending = "lf"

0 commit comments

Comments
 (0)
Please sign in to comment.