From 8b96843830fedc769319fe24a2946b29d3933210 Mon Sep 17 00:00:00 2001 From: Melissa LeBlanc-Williams Date: Mon, 21 Feb 2022 11:54:33 -0800 Subject: [PATCH] Added Type Annotations --- adafruit_platformdetect/__init__.py | 22 ++--- adafruit_platformdetect/board.py | 127 +++++++++++++++------------- adafruit_platformdetect/chip.py | 18 ++-- 3 files changed, 89 insertions(+), 78 deletions(-) diff --git a/adafruit_platformdetect/__init__.py b/adafruit_platformdetect/__init__.py index 7fadce2d..018f72e2 100644 --- a/adafruit_platformdetect/__init__.py +++ b/adafruit_platformdetect/__init__.py @@ -6,9 +6,9 @@ Attempt to detect the current platform. """ import re - -from adafruit_platformdetect.board import Board -from adafruit_platformdetect.chip import Chip +from typing import Optional +from .board import Board +from .chip import Chip # Various methods here may retain state in future, so tell pylint not to worry @@ -17,11 +17,11 @@ class Detector: """Wrap various platform detection functions.""" - def __init__(self): + def __init__(self) -> None: self.board = Board(self) self.chip = Chip(self) - def get_cpuinfo_field(self, field): + def get_cpuinfo_field(self, field: str) -> Optional[str]: """ Search /proc/cpuinfo for a field and return its value, if found, otherwise None. @@ -37,7 +37,7 @@ def get_cpuinfo_field(self, field): return match.group(1) return None - def check_dt_compatible_value(self, value): + def check_dt_compatible_value(self, value: str) -> bool: """ Search /proc/device-tree/compatible for a value and return True, if found, otherwise False. @@ -49,7 +49,7 @@ def check_dt_compatible_value(self, value): return False - def get_armbian_release_field(self, field): + def get_armbian_release_field(self, field: str) -> Optional[str]: """ Search /etc/armbian-release, if it exists, for a field and return its value, if found, otherwise None. @@ -69,7 +69,7 @@ def get_armbian_release_field(self, field): return field_value - def get_device_model(self): + def get_device_model(self) -> Optional[str]: """ Search /proc/device-tree/model for the device model and return its value, if found, otherwise None. @@ -81,7 +81,7 @@ def get_device_model(self): pass return None - def get_device_compatible(self): + def get_device_compatible(self) -> Optional[str]: """ Search /proc/device-tree/compatible for the compatible chip name. """ @@ -94,7 +94,7 @@ def get_device_compatible(self): pass return None - def check_board_asset_tag_value(self): + def check_board_asset_tag_value(self) -> Optional[str]: """ Search /sys/devices/virtual/dmi/id for the device model and return its value, if found, otherwise None. @@ -108,7 +108,7 @@ def check_board_asset_tag_value(self): pass return None - def check_board_name_value(self): + def check_board_name_value(self) -> Optional[str]: """ Search /sys/devices/virtual/dmi/id for the board name and return its value, if found, otherwise None. Debian/ubuntu based diff --git a/adafruit_platformdetect/board.py b/adafruit_platformdetect/board.py index c635932f..047cf7d9 100644 --- a/adafruit_platformdetect/board.py +++ b/adafruit_platformdetect/board.py @@ -21,9 +21,14 @@ """ # imports +from __future__ import annotations import os import re -from adafruit_platformdetect.constants import boards, chips +from typing import Optional, TYPE_CHECKING +from .constants import boards, chips + +if TYPE_CHECKING: + from . import Detector __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PlatformDetect.git" @@ -32,13 +37,13 @@ class Board: """Attempt to detect specific boards.""" - def __init__(self, detector): + def __init__(self, detector: Detector) -> None: self.detector = detector self._board_id = None # pylint: disable=invalid-name, protected-access, too-many-return-statements @property - def id(self): + def id(self) -> Optional[str]: """Return a unique id for the detected board, if any.""" # There are some times we want to trick the platform detection # say if a raspberry pi doesn't have the right ID, or for testing @@ -154,7 +159,7 @@ def id(self): # pylint: enable=invalid-name - def _pi_id(self): + def _pi_id(self) -> Optional[str]: """Try to detect id of a Raspberry Pi.""" # Check for Pi boards: pi_rev_code = self._pi_rev_code() @@ -185,7 +190,7 @@ def _pi_id(self): return None - def _pi_rev_code(self): + def _pi_rev_code(self) -> Optional[str]: """Attempt to find a Raspberry Pi revision code for this board.""" # 2708 is Pi 1 # 2709 is Pi 2 @@ -211,7 +216,7 @@ def _pi_rev_code(self): return None # pylint: disable=no-self-use - def _beaglebone_id(self): + def _beaglebone_id(self) -> Optional[str]: """Try to detect id of a Beaglebone.""" board_value = self.detector.get_device_compatible() @@ -249,14 +254,14 @@ def _beaglebone_id(self): # pylint: enable=no-self-use - def _bbai_id(self): + def _bbai_id(self) -> Optional[str]: """Try to detect id of a Beaglebone AI related board.""" board_value = self.detector.get_device_model() if "BeagleBone AI" in board_value: return boards.BEAGLEBONE_AI return None - def _tisk_id(self): + def _tisk_id(self) -> Optional[str]: """Try to detect the id of aarch64 board.""" compatible = self.detector.get_device_compatible() print(compatible) @@ -269,7 +274,7 @@ def _tisk_id(self): return None # pylint: disable=too-many-return-statements - def _armbian_id(self): + def _armbian_id(self) -> Optional[str]: """Check whether the current board is an OrangePi board.""" board_value = self.detector.get_armbian_release_field("BOARD") board = None @@ -317,14 +322,14 @@ def _armbian_id(self): # pylint: enable=too-many-return-statements - def _sama5_id(self): + def _sama5_id(self) -> Optional[str]: """Check what type sama5 board.""" board_value = self.detector.get_device_model() if "Giant Board" in board_value: return boards.GIANT_BOARD return None - def _stm32mp1_id(self): + def _stm32mp1_id(self) -> Optional[str]: """Check what type stm32mp1 board.""" board_value = self.detector.get_device_model() if "STM32MP157C-DK2" in board_value: @@ -337,7 +342,7 @@ def _stm32mp1_id(self): return boards.OSD32MP1_RED return None - def _imx8mx_id(self): + def _imx8mx_id(self) -> Optional[str]: """Check what type iMX8M board.""" board_value = self.detector.get_device_model() if "FSL i.MX8MM DDR4 EVK" in board_value: @@ -348,14 +353,14 @@ def _imx8mx_id(self): return boards.CORAL_EDGE_TPU_DEV return None - def _imx6ull_id(self): + def _imx6ull_id(self) -> Optional[str]: """Check what type iMX6ULL board.""" board_value = self.detector.get_device_model() if "LubanCat" in board_value or "Embedfire" in board_value: return boards.LUBANCAT_IMX6ULL return None - def _tegra_id(self): + def _tegra_id(self) -> Optional[str]: """Try to detect the id of aarch64 board.""" compatible = self.detector.get_device_compatible() if not compatible: @@ -366,21 +371,21 @@ def _tegra_id(self): return board_id return None - def _sifive_id(self): + def _sifive_id(self) -> Optional[str]: """Try to detect the id for Sifive RISCV64 board.""" board_value = self.detector.get_device_model() if "hifive-unleashed-a00" in board_value: return boards.SIFIVE_UNLEASHED return None - def _allwinner_id(self): + def _allwinner_id(self) -> Optional[str]: """Try to detect the id for Allwiner D1 board.""" board_value = self.detector.get_device_model() if "sun20iw1p1" in board_value: return boards.ALLWINER_D1 return None - def _pine64_id(self): + def _pine64_id(self) -> Optional[str]: """Try to detect the id for Pine64 board or device.""" board_value = self.detector.get_device_model() board = None @@ -397,7 +402,7 @@ def _pine64_id(self): return board # pylint: disable=no-self-use - def _pynq_id(self): + def _pynq_id(self) -> Optional[str]: """Try to detect the id for Xilinx PYNQ boards.""" try: with open( @@ -414,7 +419,7 @@ def _pynq_id(self): except FileNotFoundError: return None - def _rock_pi_id(self): + def _rock_pi_id(self) -> Optional[str]: """Check what type of Rock Pi board.""" board_value = self.detector.get_device_model() board = None @@ -428,7 +433,7 @@ def _rock_pi_id(self): board = boards.ROCK_PI_X return board - def _clockwork_pi_id(self): + def _clockwork_pi_id(self) -> Optional[str]: """Check what type of Clockwork Pi board.""" board_value = self.detector.get_device_model() board = None @@ -436,7 +441,7 @@ def _clockwork_pi_id(self): board = boards.CLOCKWORK_CPI3 return board - def _udoo_id(self): + def _udoo_id(self) -> Optional[str]: """Try to detect the id of udoo board.""" board_asset_tag = self.detector.check_board_asset_tag_value() for board_id, board_tags in boards._UDOO_BOARD_IDS.items(): @@ -448,7 +453,7 @@ def _udoo_id(self): return None - def _asus_tinker_board_id(self): + def _asus_tinker_board_id(self) -> Optional[str]: """Check what type of Tinker Board.""" board_value = self.detector.get_device_model() board = None @@ -456,7 +461,7 @@ def _asus_tinker_board_id(self): board = boards._ASUS_TINKER_BOARD_IDS return board - def _allwinner_variants_id(self): + def _allwinner_variants_id(self) -> Optional[str]: """Try to detect the id of allwinner based board. (orangepi, nanopi)""" board_value = self.detector.get_device_model() board = None @@ -477,7 +482,7 @@ def _allwinner_variants_id(self): # TODO: Add other specifc board contexts here return board - def _rp2040_u2if_id(self): + def _rp2040_u2if_id(self) -> Optional[str]: import hid # look for it based on PID/VID @@ -507,127 +512,127 @@ def _rp2040_u2if_id(self): raise RuntimeError("RP2040_U2IF device was added to chip but not board.") @property - def any_nanopi(self): + def any_nanopi(self) -> bool: """Check whether the current board is any defined Nano Pi.""" return self.id in boards._NANOPI_IDS @property - def any_96boards(self): + def any_96boards(self) -> bool: """Check whether the current board is any 96boards board.""" return self.id in boards._LINARO_96BOARDS_IDS @property - def any_raspberry_pi(self): + def any_raspberry_pi(self) -> bool: """Check whether the current board is any Raspberry Pi.""" return self._pi_rev_code() is not None @property - def any_raspberry_pi_40_pin(self): + def any_raspberry_pi_40_pin(self) -> bool: """Check whether the current board is any 40-pin Raspberry Pi.""" return self.id in boards._RASPBERRY_PI_40_PIN_IDS @property - def any_raspberry_pi_cm(self): + def any_raspberry_pi_cm(self) -> bool: """Check whether the current board is any Compute Module Raspberry Pi.""" return self.id in boards._RASPBERRY_PI_CM_IDS @property - def any_beaglebone(self): + def any_beaglebone(self) -> bool: """Check whether the current board is any Beaglebone-family system.""" return self.id in boards._BEAGLEBONE_IDS @property - def any_orange_pi(self): + def any_orange_pi(self) -> bool: """Check whether the current board is any defined Orange Pi.""" return self.id in boards._ORANGE_PI_IDS @property - def any_lubancat(self): + def any_lubancat(self) -> bool: """Check whether the current board is any defined lubancat.""" return self.id in boards._LUBANCAT_IDS @property - def any_coral_board(self): + def any_coral_board(self) -> bool: """Check whether the current board is any defined Coral.""" return self.id in boards._CORAL_IDS @property - def any_pynq_board(self): + def any_pynq_board(self) -> bool: """Check whether the current board is any defined PYNQ Board.""" return self.id in boards._PYNQ_IDS @property - def any_giant_board(self): + def any_giant_board(self) -> bool: """Check whether the current board is any defined Giant Board.""" return self.GIANT_BOARD @property - def any_odroid_40_pin(self): + def any_odroid_40_pin(self) -> bool: """Check whether the current board is any defined 40-pin Odroid.""" return self.id in boards._ODROID_40_PIN_IDS @property - def any_jetson_board(self): + def any_jetson_board(self) -> bool: """Check whether the current board is any defined Jetson Board.""" return self.id in [v[0] for v in boards._JETSON_IDS] @property - def any_sifive_board(self): + def any_sifive_board(self) -> bool: """Check whether the current board is any defined Jetson Board.""" return self.id in boards._SIFIVE_IDS @property - def any_onion_omega_board(self): + def any_onion_omega_board(self) -> bool: """Check whether the current board is any defined OpenWRT board.""" return self.id in boards._ONION_OMEGA_BOARD_IDS @property - def any_pine64_board(self): + def any_pine64_board(self) -> bool: """Check whether the current board is any Pine64 device.""" return self.id in boards._PINE64_DEV_IDS @property - def any_rock_pi_board(self): + def any_rock_pi_board(self) -> bool: """Check whether the current board is any Rock Pi device.""" return self.id in boards._ROCK_PI_IDS @property - def any_clockwork_pi_board(self): + def any_clockwork_pi_board(self) -> bool: """Check whether the current board is any Clockwork Pi device.""" return self.CLOCKWORK_CPI3 @property - def any_udoo_board(self): + def any_udoo_board(self) -> bool: """Check to see if the current board is an UDOO board""" return self.id in boards._UDOO_BOARD_IDS @property - def any_asus_tinker_board(self): + def any_asus_tinker_board(self) -> bool: """Check to see if the current board is an ASUS Tinker Board""" return self.id in boards._ASUS_TINKER_BOARD_IDS @property - def any_stm32mp1(self): + def any_stm32mp1(self) -> bool: """Check whether the current board is any stm32mp1 board.""" return self.id in boards._STM32MP1_IDS @property - def any_bananapi(self): + def any_bananapi(self) -> bool: """Check whether the current board is any BananaPi-family system.""" return self.id in boards._BANANA_PI_IDS @property - def any_maaxboard(self): + def any_maaxboard(self) -> bool: """Check whether the current board is any BananaPi-family system.""" return self.id in boards._MAAXBOARD_IDS @property - def any_tisk_board(self): + def any_tisk_board(self) -> bool: """Check whether the current board is any defined TI SK Board.""" return self.id in [v[0] for v in boards._TI_SK_BOARD_IDS] @property - def any_embedded_linux(self): + def any_embedded_linux(self) -> bool: """Check whether the current board is any embedded Linux device.""" return any( [ @@ -658,61 +663,61 @@ def any_embedded_linux(self): ) @property - def ftdi_ft232h(self): + def ftdi_ft232h(self) -> bool: """Check whether the current board is an FTDI FT232H.""" return self.id == boards.FTDI_FT232H @property - def ftdi_ft2232h(self): + def ftdi_ft2232h(self) -> bool: """Check whether the current board is an FTDI FT2232H.""" return self.id == boards.FTDI_FT2232H @property - def microchip_mcp2221(self): + def microchip_mcp2221(self) -> bool: """Check whether the current board is a Microchip MCP2221.""" return self.id == boards.MICROCHIP_MCP2221 @property - def pico_u2if(self): + def pico_u2if(self) -> bool: """Check whether the current board is a RPi Pico w/ u2if.""" return self.id == boards.PICO_U2IF @property - def feather_u2if(self): + def feather_u2if(self) -> bool: """Check whether the current board is a Feather RP2040 w/ u2if.""" return self.id == boards.FEATHER_U2IF @property - def itsybitsy_u2if(self): + def itsybitsy_u2if(self) -> bool: """Check whether the current board is a Itsy Bitsy w/ u2if.""" return self.id == boards.ITSYBITSY_U2IF @property - def macropad_u2if(self): + def macropad_u2if(self) -> bool: """Check whether the current board is a MacroPad w/ u2if.""" return self.id == boards.MACROPAD_U2IF @property - def qtpy_u2if(self): + def qtpy_u2if(self) -> bool: """Check whether the current board is a QT Py w/ u2if.""" return self.id == boards.QTPY_U2IF @property - def qt2040_trinkey_u2if(self): + def qt2040_trinkey_u2if(self) -> bool: """Check whether the current board is a QT Py w/ u2if.""" return self.id == boards.QT2040_TRINKEY_U2IF @property - def binho_nova(self): + def binho_nova(self) -> bool: """Check whether the current board is an BINHO NOVA.""" return self.id == boards.BINHO_NOVA @property - def greatfet_one(self): + def greatfet_one(self) -> bool: """Check whether the current board is a GreatFET One.""" return self.id == boards.GREATFET_ONE - def __getattr__(self, attr): + def __getattr__(self, attr: str) -> bool: """ Detect whether the given attribute is the currently-detected board. See list of constants at the top of this module for available options. diff --git a/adafruit_platformdetect/chip.py b/adafruit_platformdetect/chip.py index 7404013a..c5b36a91 100644 --- a/adafruit_platformdetect/chip.py +++ b/adafruit_platformdetect/chip.py @@ -15,15 +15,19 @@ **Software and Dependencies:** -* Linux and Python 3.6 or Higher +* Linux and Python 3.7 or Higher """ # imports +from __future__ import annotations import os import sys +from typing import Optional, TYPE_CHECKING +from .constants import chips -from adafruit_platformdetect.constants import chips +if TYPE_CHECKING: + from . import Detector __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PlatformDetect.git" @@ -32,14 +36,16 @@ class Chip: """Attempt detection of current chip / CPU.""" - def __init__(self, detector): + def __init__(self, detector: Detector) -> None: self.detector = detector self._chip_id = None @property def id( self, - ): # pylint: disable=invalid-name,too-many-branches,too-many-return-statements + ) -> Optional[ + str + ]: # pylint: disable=invalid-name,too-many-branches,too-many-return-statements """Return a unique id for the detected chip, if any.""" # There are some times we want to trick the platform detection # say if a raspberry pi doesn't have the right ID, or for testing @@ -154,7 +160,7 @@ def id( # pylint: enable=invalid-name - def _linux_id(self): + def _linux_id(self) -> Optional[str]: # pylint: disable=too-many-branches,too-many-statements # pylint: disable=too-many-return-statements """Attempt to detect the CPU on a computer running the Linux kernel.""" @@ -332,7 +338,7 @@ def _linux_id(self): return linux_id - def __getattr__(self, attr): + def __getattr__(self, attr: str) -> bool: """ Detect whether the given attribute is the currently-detected chip. See list of constants at the top of this module for available options.