Skip to content

Commit 534776a

Browse files
committed
add tweaks for pylint; detect (some) generic linux systems; rename class
Main wrapper class is now `Detector`, which is more noun-y than verb-y. Added GENERIC_X86 and GENERIC_LINUX_PC to chip and board, respectively. Made pylint happier.
1 parent 7d2e0c8 commit 534776a

File tree

5 files changed

+55
-40
lines changed

5 files changed

+55
-40
lines changed

adafruit_platformdetect/__init__.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@
2828
from adafruit_platformdetect.board import Board
2929
from adafruit_platformdetect.chip import Chip
3030

31-
class PlatformDetector:
31+
# Various methods here may retain state in future, so tell pylint not to worry
32+
# that they don't use self right now:
33+
# pylint: disable=no-self-use
34+
class Detector:
35+
"""Wrap various platform detection functions."""
3236

3337
def __init__(self):
3438
self.board = Board(self)
@@ -59,8 +63,8 @@ def get_armbian_release_field(self, field):
5963
field_value = None
6064
pattern = r'^' + field + r'=(.*)'
6165
try:
62-
with open("/etc/armbian-release", 'r') as f:
63-
armbian = f.read().split('\n')
66+
with open("/etc/armbian-release", 'r') as release_file:
67+
armbian = release_file.read().split('\n')
6468
for line in armbian:
6569
match = re.search(pattern, line)
6670
if match:

adafruit_platformdetect/board.py

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
"""Detect boards."""
12
import adafruit_platformdetect.chip as ap_chip
2-
import platform
3-
import sys
4-
import re
53

4+
# Allow for aligned constant definitions:
5+
# pylint: disable=bad-whitespace
66
BEAGLEBONE = 'BEAGLEBONE'
77
BEAGLEBONE_BLACK = 'BEAGLEBONE_BLACK'
88
BEAGLEBONE_BLUE = 'BEAGLEBONE_BLUE'
@@ -21,6 +21,7 @@
2121

2222
FEATHER_HUZZAH = "FEATHER_HUZZAH"
2323
FEATHER_M0_EXPRESS = "FEATHER_M0_EXPRESS"
24+
GENERIC_LINUX_PC = "GENERIC_LINUX_PC"
2425
PYBOARD = "PYBOARD"
2526
NODEMCU = "NODEMCU"
2627
ORANGE_PI_PC = "ORANGE_PI_PC"
@@ -37,8 +38,8 @@
3738
RASPBERRY_PI_3B_PLUS = "RASPBERRY_PI_3B_PLUS"
3839
RASPBERRY_PI_CM3 = "RASPBERRY_PI_CM3"
3940
RASPBERRY_PI_3A_PLUS = "RASPBERRY_PI_3A_PLUS"
41+
# pylint: enable=bad-whitespace
4042

41-
# TODO: Should this include RASPBERRY_PI_3A_PLUS or any other models?
4243
ANY_RASPBERRY_PI_2_OR_3 = (
4344
RASPBERRY_PI_2B,
4445
RASPBERRY_PI_3B,
@@ -96,7 +97,6 @@
9697
BEAGLEBONE_AIR: (
9798
('A0', 'A335BNLTNAD0'),
9899
),
99-
# TODO: Does this differ meaningfully from the PocketBeagle?
100100
BEAGLEBONE_POCKETBONE: (
101101
('0', 'A335BNLTBP00'),
102102
),
@@ -130,32 +130,35 @@
130130
}
131131

132132
class Board:
133-
"""
134-
Attempt to detect specific boards.
135-
"""
133+
"""Attempt to detect specific boards."""
136134
def __init__(self, detector):
137135
self.detector = detector
138136

137+
# pylint: disable=invalid-name
139138
@property
140139
def id(self):
141140
"""Return a unique id for the detected board, if any."""
142141

143142
chip_id = self.detector.chip.id
143+
board_id = None
144144

145145
if chip_id == ap_chip.BCM2XXX:
146-
return self._pi_id()
146+
board_id = self._pi_id()
147147
elif chip_id == ap_chip.AM33XX:
148-
return self._beaglebone_id()
148+
board_id = self._beaglebone_id()
149+
elif chip_id == ap_chip.GENERIC_X86:
150+
board_id = GENERIC_LINUX_PC
149151
elif chip_id == ap_chip.SUN8I:
150-
return self._armbian_id()
152+
board_id = self._armbian_id()
151153
elif chip_id == ap_chip.ESP8266:
152-
return FEATHER_HUZZAH
154+
board_id = FEATHER_HUZZAH
153155
elif chip_id == ap_chip.SAMD21:
154-
return FEATHER_M0_EXPRESS
156+
board_id = FEATHER_M0_EXPRESS
155157
elif chip_id == ap_chip.STM32:
156-
return PYBOARD
158+
board_id = PYBOARD
157159

158-
return None
160+
return board_id
161+
# pylint: enable=invalid-name
159162

160163
def _pi_id(self):
161164
"""Try to detect id of a Raspberry Pi."""
@@ -178,6 +181,7 @@ def _pi_rev_code(self):
178181
return None
179182
return self.detector.get_cpuinfo_field('Revision')
180183

184+
# pylint: disable=no-self-use
181185
def _beaglebone_id(self):
182186
"""Try to detect id of a Beaglebone."""
183187
try:
@@ -190,14 +194,14 @@ def _beaglebone_id(self):
190194
return None
191195

192196
id_string = eeprom_bytes[4:].decode("ascii")
193-
for model, ids in _BEAGLEBONE_BOARD_IDS.items():
194-
for id in ids:
195-
if id_string == id[1]:
197+
for model, bb_ids in _BEAGLEBONE_BOARD_IDS.items():
198+
for bb_id in bb_ids:
199+
if id_string == bb_id[1]:
196200
return model
197201

198202
return None
203+
# pylint: enable=no-self-use
199204

200-
@property
201205
def _armbian_id(self):
202206
"""Check whether the current board is an OrangePi PC."""
203207
board_value = self.detector.get_armbian_release_field('BOARD')
@@ -212,6 +216,7 @@ def any_raspberry_pi(self):
212216

213217
@property
214218
def any_raspberry_pi_2_or_3(self):
219+
"""Check whether the current board is any Raspberry Pi 2 or 3."""
215220
return self.id in ANY_RASPBERRY_PI_2_OR_3
216221

217222
def __getattr__(self, attr):

adafruit_platformdetect/chip.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"""Attempt detection of current chip / CPU."""
12
import sys
23

34
AM33XX = "AM33XX"
@@ -6,39 +7,47 @@
67
SAMD21 = "SAMD21"
78
STM32 = "STM32"
89
SUN8I = "SUN8I"
10+
GENERIC_X86 = "GENERIC_X86"
911

1012
class Chip:
1113
"""Attempt detection of current chip / CPU."""
1214
def __init__(self, detector):
1315
self.detector = detector
1416

1517
@property
18+
# pylint: disable=invalid-name
1619
def id(self):
20+
"""Return a unique id for the detected chip, if any."""
1721
platform = sys.platform
1822
if platform == "linux":
1923
return self._linux_id()
20-
elif platform == "esp8266":
24+
if platform == "esp8266":
2125
return ESP8266
22-
elif platform == "samd21":
26+
if platform == "samd21":
2327
return SAMD21
24-
elif platform == "pyboard":
28+
if platform == "pyboard":
2529
return STM32
26-
else:
27-
return None
30+
return None
31+
# pylint: enable=invalid-name
2832

2933
def _linux_id(self):
3034
"""Attempt to detect the CPU on a computer running the Linux kernel."""
31-
id = None
35+
linux_id = None
3236

3337
hardware = self.detector.get_cpuinfo_field("Hardware")
34-
if hardware in ('BCM2708', 'BCM2708', 'BCM2835'):
35-
id = BCM2XXX
38+
39+
if hardware is None:
40+
vendor_id = self.detector.get_cpuinfo_field("vendor_id")
41+
if vendor_id in ("GenuineIntel", "AuthenticAMD"):
42+
linux_id = GENERIC_X86
43+
elif hardware in ("BCM2708", "BCM2708", "BCM2835"):
44+
linux_id = BCM2XXX
3645
elif "AM33XX" in hardware:
37-
id = AM33XX
46+
linux_id = AM33XX
3847
elif "sun8i" in hardware:
39-
id = SUN8I
48+
linux_id = SUN8I
4049

41-
return id
50+
return linux_id
4251

4352
def __getattr__(self, attr):
4453
"""

bin/detect.py

100644100755
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@
22

33
import adafruit_platformdetect
44

5-
detector = adafruit_platformdetect.PlatformDetector()
5+
detector = adafruit_platformdetect.Detector()
66

77
print("Chip id: ", detector.chip.id)
88

99
print("Board id: ", detector.board.id)
1010

1111
print("Is this a Pi 3B+?", detector.board.RASPBERRY_PI_3B_PLUS)
1212
print("Is this a BBB?", detector.board.BEAGLEBONE_BLACK)
13+
print("Is this an Orange Pi PC?", detector.board.ORANGE_PI_PC)
14+
print("Is this a generic Linux PC?", detector.board.GENERIC_LINUX_PC)
1315

1416
if detector.board.any_raspberry_pi:
1517
print("Raspberry Pi detected.")
16-
print("Revision code: ", detector.board._pi_rev_code())
17-
18-
if detector.board.BEAGLEBONE_BLACK:
19-
print("BBB detected")

setup.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
#!/usr/bin/env python
1+
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

44
# Note: To use the 'upload' functionality of this file, you must:
55
# $ pip install twine
66

77
import io
88
import os
9-
import sys
109

1110
from setuptools import setup, find_packages
1211

0 commit comments

Comments
 (0)