Browse Source

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.
Brennen Bearnes 7 months ago
parent
commit
534776a825
5 changed files with 55 additions and 40 deletions
  1. 7
    3
      adafruit_platformdetect/__init__.py
  2. 24
    19
      adafruit_platformdetect/board.py
  3. 20
    11
      adafruit_platformdetect/chip.py
  4. 3
    5
      bin/detect.py
  5. 1
    2
      setup.py

+ 7
- 3
adafruit_platformdetect/__init__.py View File

@@ -28,7 +28,11 @@ import re
28 28
 from adafruit_platformdetect.board import Board
29 29
 from adafruit_platformdetect.chip import Chip
30 30
 
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."""
32 36
 
33 37
     def __init__(self):
34 38
         self.board = Board(self)
@@ -59,8 +63,8 @@ class PlatformDetector:
59 63
         field_value = None
60 64
         pattern = r'^' + field + r'=(.*)'
61 65
         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')
64 68
                 for line in armbian:
65 69
                     match = re.search(pattern, line)
66 70
                     if match:

+ 24
- 19
adafruit_platformdetect/board.py View File

@@ -1,8 +1,8 @@
1
+"""Detect boards."""
1 2
 import adafruit_platformdetect.chip as ap_chip
2
-import platform
3
-import sys
4
-import re
5 3
 
4
+# Allow for aligned constant definitions:
5
+# pylint: disable=bad-whitespace
6 6
 BEAGLEBONE                  = 'BEAGLEBONE'
7 7
 BEAGLEBONE_BLACK            = 'BEAGLEBONE_BLACK'
8 8
 BEAGLEBONE_BLUE             = 'BEAGLEBONE_BLUE'
@@ -21,6 +21,7 @@ OSD3358_SM_RED              = 'OSD3358_SM_RED'
21 21
 
22 22
 FEATHER_HUZZAH              = "FEATHER_HUZZAH"
23 23
 FEATHER_M0_EXPRESS          = "FEATHER_M0_EXPRESS"
24
+GENERIC_LINUX_PC            = "GENERIC_LINUX_PC"
24 25
 PYBOARD                     = "PYBOARD"
25 26
 NODEMCU                     = "NODEMCU"
26 27
 ORANGE_PI_PC                = "ORANGE_PI_PC"
@@ -37,8 +38,8 @@ RASPBERRY_PI_3B             = "RASPBERRY_PI_3B"
37 38
 RASPBERRY_PI_3B_PLUS        = "RASPBERRY_PI_3B_PLUS"
38 39
 RASPBERRY_PI_CM3            = "RASPBERRY_PI_CM3"
39 40
 RASPBERRY_PI_3A_PLUS        = "RASPBERRY_PI_3A_PLUS"
41
+# pylint: enable=bad-whitespace
40 42
 
41
-# TODO: Should this include RASPBERRY_PI_3A_PLUS or any other models?
42 43
 ANY_RASPBERRY_PI_2_OR_3 = (
43 44
     RASPBERRY_PI_2B,
44 45
     RASPBERRY_PI_3B,
@@ -96,7 +97,6 @@ _BEAGLEBONE_BOARD_IDS = {
96 97
     BEAGLEBONE_AIR: (
97 98
         ('A0', 'A335BNLTNAD0'),
98 99
     ),
99
-    # TODO: Does this differ meaningfully from the PocketBeagle?
100 100
     BEAGLEBONE_POCKETBONE: (
101 101
         ('0', 'A335BNLTBP00'),
102 102
     ),
@@ -130,32 +130,35 @@ _PI_REV_CODES = {
130 130
 }
131 131
 
132 132
 class Board:
133
-    """
134
-    Attempt to detect specific boards.
135
-    """
133
+    """Attempt to detect specific boards."""
136 134
     def __init__(self, detector):
137 135
         self.detector = detector
138 136
 
137
+    # pylint: disable=invalid-name
139 138
     @property
140 139
     def id(self):
141 140
         """Return a unique id for the detected board, if any."""
142 141
 
143 142
         chip_id = self.detector.chip.id
143
+        board_id = None
144 144
 
145 145
         if chip_id == ap_chip.BCM2XXX:
146
-            return self._pi_id()
146
+            board_id = self._pi_id()
147 147
         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
149 151
         elif chip_id == ap_chip.SUN8I:
150
-            return self._armbian_id()
152
+            board_id = self._armbian_id()
151 153
         elif chip_id == ap_chip.ESP8266:
152
-            return FEATHER_HUZZAH
154
+            board_id = FEATHER_HUZZAH
153 155
         elif chip_id == ap_chip.SAMD21:
154
-            return FEATHER_M0_EXPRESS
156
+            board_id = FEATHER_M0_EXPRESS
155 157
         elif chip_id == ap_chip.STM32:
156
-            return PYBOARD
158
+            board_id = PYBOARD
157 159
 
158
-        return None
160
+        return board_id
161
+    # pylint: enable=invalid-name
159 162
 
160 163
     def _pi_id(self):
161 164
         """Try to detect id of a Raspberry Pi."""
@@ -178,6 +181,7 @@ class Board:
178 181
             return None
179 182
         return self.detector.get_cpuinfo_field('Revision')
180 183
 
184
+    # pylint: disable=no-self-use
181 185
     def _beaglebone_id(self):
182 186
         """Try to detect id of a Beaglebone."""
183 187
         try:
@@ -190,14 +194,14 @@ class Board:
190 194
             return None
191 195
 
192 196
         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]:
196 200
                     return model
197 201
 
198 202
         return None
203
+    # pylint: enable=no-self-use
199 204
 
200
-    @property
201 205
     def _armbian_id(self):
202 206
         """Check whether the current board is an OrangePi PC."""
203 207
         board_value = self.detector.get_armbian_release_field('BOARD')
@@ -212,6 +216,7 @@ class Board:
212 216
 
213 217
     @property
214 218
     def any_raspberry_pi_2_or_3(self):
219
+        """Check whether the current board is any Raspberry Pi 2 or 3."""
215 220
         return self.id in ANY_RASPBERRY_PI_2_OR_3
216 221
 
217 222
     def __getattr__(self, attr):

+ 20
- 11
adafruit_platformdetect/chip.py View File

@@ -1,3 +1,4 @@
1
+"""Attempt detection of current chip / CPU."""
1 2
 import sys
2 3
 
3 4
 AM33XX = "AM33XX"
@@ -6,6 +7,7 @@ ESP8266 = "ESP8266"
6 7
 SAMD21 = "SAMD21"
7 8
 STM32 = "STM32"
8 9
 SUN8I = "SUN8I"
10
+GENERIC_X86 = "GENERIC_X86"
9 11
 
10 12
 class Chip:
11 13
     """Attempt detection of current chip / CPU."""
@@ -13,32 +15,39 @@ class Chip:
13 15
         self.detector = detector
14 16
 
15 17
     @property
18
+    # pylint: disable=invalid-name
16 19
     def id(self):
20
+        """Return a unique id for the detected chip, if any."""
17 21
         platform = sys.platform
18 22
         if platform == "linux":
19 23
             return self._linux_id()
20
-        elif platform == "esp8266":
24
+        if platform == "esp8266":
21 25
             return ESP8266
22
-        elif platform == "samd21":
26
+        if platform == "samd21":
23 27
             return SAMD21
24
-        elif platform == "pyboard":
28
+        if platform == "pyboard":
25 29
             return STM32
26
-        else:
27
-            return None
30
+        return None
31
+    # pylint: enable=invalid-name
28 32
 
29 33
     def _linux_id(self):
30 34
         """Attempt to detect the CPU on a computer running the Linux kernel."""
31
-        id = None
35
+        linux_id = None
32 36
 
33 37
         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
36 45
         elif "AM33XX" in hardware:
37
-            id = AM33XX
46
+            linux_id = AM33XX
38 47
         elif "sun8i" in hardware:
39
-            id = SUN8I
48
+            linux_id = SUN8I
40 49
 
41
-        return id
50
+        return linux_id
42 51
 
43 52
     def __getattr__(self, attr):
44 53
         """

+ 3
- 5
bin/detect.py View File

@@ -2,7 +2,7 @@
2 2
 
3 3
 import adafruit_platformdetect
4 4
 
5
-detector = adafruit_platformdetect.PlatformDetector()
5
+detector = adafruit_platformdetect.Detector()
6 6
 
7 7
 print("Chip id: ", detector.chip.id)
8 8
 
@@ -10,10 +10,8 @@ print("Board id: ", detector.board.id)
10 10
 
11 11
 print("Is this a Pi 3B+?", detector.board.RASPBERRY_PI_3B_PLUS)
12 12
 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)
13 15
 
14 16
 if detector.board.any_raspberry_pi:
15 17
     print("Raspberry Pi detected.")
16
-    print("Revision code: ", detector.board._pi_rev_code())
17
-
18
-if detector.board.BEAGLEBONE_BLACK:
19
-    print("BBB detected")

+ 1
- 2
setup.py View File

@@ -1,4 +1,4 @@
1
-#!/usr/bin/env python
1
+#!/usr/bin/env python3
2 2
 # -*- coding: utf-8 -*-
3 3
 
4 4
 # Note: To use the 'upload' functionality of this file, you must:
@@ -6,7 +6,6 @@
6 6
 
7 7
 import io
8 8
 import os
9
-import sys
10 9
 
11 10
 from setuptools import setup, find_packages
12 11
 

Loading…
Cancel
Save