Skip to content

Commit 7a1ff1b

Browse files
committed
Update pre-commit and re-lint
1 parent e88f9ff commit 7a1ff1b

File tree

3 files changed

+103
-30
lines changed

3 files changed

+103
-30
lines changed

adafruit_platformdetect/revcodes.py

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,25 @@
22
#
33
# SPDX-License-Identifier: MIT
44

5-
# Class to help with Raspberry Pi Rev Codes
6-
# Written by Melissa LeBlanc-Williams for Adafruit Industries
7-
#
8-
# Data values from https://github.com/raspberrypi/documentation/blob/develop/
9-
# documentation/asciidoc/computers/raspberry-pi/revision-codes.adoc#new-style-revision-codes
5+
"""
6+
`adafruit_platformdetect.revcodes`
7+
================================================================================
8+
9+
Class to help with Raspberry Pi Rev Codes
10+
11+
* Author(s): Melissa LeBlanc-Williams
12+
13+
Implementation Notes
14+
--------------------
15+
16+
**Software and Dependencies:**
17+
18+
* Linux and Python 3.7 or Higher
19+
20+
Data values from https://github.com/raspberrypi/documentation/blob/develop/
21+
documentation/asciidoc/computers/raspberry-pi/revision-codes.adoc#new-style-revision-codes
22+
23+
"""
1024

1125
NEW_OVERVOLTAGE = (
1226
"Overvoltage allowed",
@@ -135,34 +149,39 @@
135149
0x15: (2, 1.1, 2, 2),
136150
}
137151

152+
138153
class PiDecoder:
154+
"""Raspberry Pi Revision Code Decoder"""
155+
139156
def __init__(self, rev_code):
140157
try:
141158
self.rev_code = int(rev_code, 16) & 0xFFFFFFFF
142159
except ValueError:
143160
print("Invalid revision code. It should be a hexadecimal value.")
144161

145162
def is_valid_code(self):
146-
# This is a check intended to quickly check the validity of a code
163+
"""Quickly check the validity of a code"""
147164
if self.is_new_format():
148-
for format in NEW_REV_STRUCTURE.values():
149-
lower_bit, bit_size, values = format
165+
for code_format in NEW_REV_STRUCTURE.values():
166+
lower_bit, bit_size, values = code_format
150167
prop_value = (self.rev_code >> lower_bit) & ((1 << bit_size) - 1)
151168
if not self._valid_value(prop_value, values):
152169
return False
153170
else:
154171
if (self.rev_code & 0xFFFF) not in OLD_REV_LUT.keys():
155172
return False
156-
for format in OLD_REV_STRUCTURE.values():
157-
index, values = format
158-
format = OLD_REV_LUT[self.rev_code & 0xFFFF]
159-
if index >= len(format):
173+
for code_format in OLD_REV_STRUCTURE.values():
174+
index, values = code_format
175+
code_format = OLD_REV_LUT[self.rev_code & 0xFFFF]
176+
if index >= len(code_format):
160177
return False
161-
if not self._valid_value(format[index], values):
178+
if not self._valid_value(code_format[index], values):
162179
return False
163180
return True
164181

165-
def _get_rev_prop_value(self, name, structure=NEW_REV_STRUCTURE, raw=False):
182+
def _get_rev_prop_value(self, name, structure=None, raw=False):
183+
if structure is None:
184+
structure = NEW_REV_STRUCTURE
166185
if name not in structure.keys():
167186
raise ValueError(f"Unknown property {name}")
168187
lower_bit, bit_size, values = structure[name]
@@ -189,12 +208,14 @@ def _get_old_rev_prop_value(self, name, raw=False):
189208
return data[index]
190209
return self._format_value(data[index], values)
191210

192-
def _format_value(self, value, valid_values):
211+
@staticmethod
212+
def _format_value(value, valid_values):
193213
if valid_values is float or valid_values is int:
194214
return valid_values(value)
195215
return valid_values[value]
196216

197-
def _valid_value(self, value, valid_values):
217+
@staticmethod
218+
def _valid_value(value, valid_values):
198219
if valid_values is float or valid_values is int:
199220
return isinstance(value, valid_values)
200221
if isinstance(valid_values, (tuple, list)) and 0 <= value < len(valid_values):
@@ -208,57 +229,68 @@ def _get_property(self, name, raw=False):
208229
raise ValueError(f"Unknown property {name}")
209230
if self.is_new_format():
210231
return self._get_rev_prop_value(name, raw=raw)
211-
elif name in OLD_REV_EXTRA_PROPS:
232+
if name in OLD_REV_EXTRA_PROPS:
212233
return self._get_rev_prop_value(
213234
name, structure=OLD_REV_EXTRA_PROPS, raw=raw
214235
)
215-
else:
216-
return self._get_old_rev_prop_value(name, raw=raw)
236+
return self._get_old_rev_prop_value(name, raw=raw)
217237

218238
def is_new_format(self):
239+
"""Check if the code is in the new format"""
219240
return self._get_rev_prop_value("rev_style", raw=True) == 1
220241

221242
@property
222243
def overvoltage(self):
244+
"""Overvoltage allowed/disallowed"""
223245
return self._get_property("overvoltage")
224246

225247
@property
226248
def warranty_bit(self):
249+
"""Warranty bit"""
227250
return self._get_property("warranty")
228251

229252
@property
230253
def otp_program(self):
254+
"""OTP programming allowed/disallowed"""
231255
return self._get_property("otp_program")
232256

233257
@property
234258
def otp_read(self):
259+
"""OTP reading allowed/disallowed"""
235260
return self._get_property("otp_read")
236261

237262
@property
238263
def rev_style(self):
264+
"""Revision Code style"""
239265
# Force new style for Rev Style
240266
return self._get_rev_prop_value("rev_style")
241267

242268
@property
243269
def memory_size(self):
270+
"""Memory size"""
244271
return self._get_property("memory_size")
245272

246273
@property
247274
def manufacturer(self):
275+
"""Manufacturer"""
248276
return self._get_property("manufacturer")
249277

250278
@property
251279
def processor(self):
280+
"""Processor"""
252281
return self._get_property("processor")
253282

254283
@property
255284
def type(self):
285+
"""Specific Model"""
256286
return self._get_property("type")
257287

258288
@property
259289
def type_raw(self):
260-
return self._get_property("type", raw = True)
290+
"""Raw Value of Specific Model"""
291+
return self._get_property("type", raw=True)
261292

262293
@property
263294
def revision(self):
295+
"""Revision Number"""
264296
return self._get_property("revision")

bin/rev_code_tester.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,38 @@
44
#
55
# SPDX-License-Identifier: MIT
66

7-
# This tests that all existing rev codes in the boards constant file
8-
# match what the decoder finds.
7+
"""
8+
`bin.rev_code_tester`
9+
================================================================================
10+
11+
Tests that all existing rev codes in the boards constant file
12+
match what the decoder finds.
13+
14+
* Author(s): Melissa LeBlanc-Williams
15+
16+
Implementation Notes
17+
--------------------
18+
19+
**Software and Dependencies:**
20+
21+
* Linux and Python 3.7 or Higher
22+
23+
"""
924

1025
import adafruit_platformdetect
1126
import adafruit_platformdetect.constants.boards as ap_board
1227
from adafruit_platformdetect.revcodes import PiDecoder
1328

1429
detector = adafruit_platformdetect.Detector()
1530

31+
1632
def print_property(label, value):
33+
"Format and print a property"
1734
print(f"{label}: {value}")
1835

36+
1937
def print_info(pi_decoder):
38+
"Print the info for the board"
2039
if pi_decoder.is_new_format():
2140
print_property("Overvoltage", pi_decoder.overvoltage)
2241
print_property("OTP Program", pi_decoder.otp_program)
@@ -35,17 +54,20 @@ def print_info(pi_decoder):
3554
print_property("RAM", pi_decoder.memory_size)
3655
print_property("Manufacturer", pi_decoder.manufacturer)
3756

57+
3858
# Iterate through the _PI_REV_CODES dictionary to find the model
3959
# Run the code through the decoder to check that:
4060
# - It is a valid code
4161
# - It matches the model
42-
for model, codes in ap_board._PI_REV_CODES.items():
62+
for model, codes in ap_board._PI_REV_CODES.items(): # pylint: disable=protected-access
4363
for pi_rev_code in codes:
4464
try:
4565
decoder = PiDecoder(pi_rev_code)
4666
except ValueError as e:
4767
print("Invalid revision code. It should be a hexadecimal value.")
48-
decoded_model = ap_board._PI_MODELS[decoder.type_raw]
68+
decoded_model = ap_board._PI_MODELS[ # pylint: disable=protected-access
69+
decoder.type_raw
70+
]
4971
if isinstance(decoded_model, dict):
5072
decoded_model = decoded_model[decoder.revision]
5173
if decoded_model == model:

bin/rpi_info.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,23 @@
44
#
55
# SPDX-License-Identifier: MIT
66

7-
# Interactive mode will prompt for the revision code
8-
# Otherwise it will be detected automatically
7+
"""
8+
`bin.rpi_info`
9+
================================================================================
10+
11+
Interactive mode will prompt for the revision code
12+
Otherwise it will be detected automatically
13+
14+
* Author(s): Melissa LeBlanc-Williams
15+
16+
Implementation Notes
17+
--------------------
18+
19+
**Software and Dependencies:**
20+
21+
* Linux and Python 3.7 or Higher
22+
23+
"""
924

1025
import sys
1126
import adafruit_platformdetect
@@ -14,7 +29,7 @@
1429
pi_rev_code = None
1530

1631
detector = adafruit_platformdetect.Detector()
17-
pi_rev_code = detector.board._pi_rev_code()
32+
pi_rev_code = detector.board._pi_rev_code() # pylint: disable=protected-access
1833

1934
if pi_rev_code is None:
2035
print("Raspberry Pi not detected. Using interactive mode")
@@ -26,12 +41,16 @@
2641
print("Invalid revision code. It should be a hexadecimal value.")
2742
sys.exit(1)
2843

29-
# if not decoder.is_valid_code():
30-
# print("Code is invalid. This rev code includes at least one value that is outside of the expected range.")
31-
# sys.exit(1)
44+
if not decoder.is_valid_code():
45+
print(
46+
"Code is invalid. This rev code includes at least one "
47+
"value that is outside of the expected range."
48+
)
49+
sys.exit(1)
3250

3351

3452
def print_property(label, value):
53+
"Format and print a property"
3554
print(f"{label}: {value}")
3655

3756

0 commit comments

Comments
 (0)