Skip to content

Commit 3ec4d1d

Browse files
committed
Fixes #220 for platforms that do not support typing
1 parent abe4055 commit 3ec4d1d

File tree

10 files changed

+120
-17
lines changed

10 files changed

+120
-17
lines changed

.readthedocs.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
version: 2
1010

1111
python:
12-
version: "3.6"
12+
version: "3.7"
1313
install:
1414
- requirements: docs/requirements.txt
1515
- requirements: requirements.txt

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Dependencies
4141
=============
4242
This driver depends on:
4343

44-
* Python 3.6 or higher
44+
* Python 3.7 or higher
4545

4646
Installing from PyPI
4747
=====================

adafruit_platformdetect/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
Attempt to detect the current platform.
77
"""
88
import re
9-
from typing import Optional
9+
10+
try:
11+
from typing import Optional
12+
except ImportError:
13+
pass
1014
from .board import Board
1115
from .chip import Chip
1216

adafruit_platformdetect/board.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,25 @@
1616
1717
**Software and Dependencies:**
1818
19-
* Linux and Python 3.6 or Higher
19+
* Linux and Python 3.7 or Higher
2020
2121
"""
2222

2323
# imports
24-
from __future__ import annotations
2524
import os
2625
import re
27-
from typing import Optional, TYPE_CHECKING
26+
27+
try:
28+
from typing import TYPE_CHECKING, Optional
29+
except ImportError:
30+
TYPE_CHECKING = False
31+
2832
from .constants import boards, chips
2933

3034
if TYPE_CHECKING:
31-
from . import Detector
35+
from .protocols import DetectorProtocol
36+
else:
37+
DetectorProtocol = ...
3238

3339
__version__ = "0.0.0-auto.0"
3440
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PlatformDetect.git"
@@ -37,7 +43,8 @@
3743
class Board:
3844
"""Attempt to detect specific boards."""
3945

40-
def __init__(self, detector: Detector) -> None:
46+
# pylint: disable=used-before-assignment
47+
def __init__(self, detector: DetectorProtocol) -> None:
4148
self.detector = detector
4249
self._board_id = None
4350

adafruit_platformdetect/chip.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,20 @@
2020
"""
2121

2222
# imports
23-
from __future__ import annotations
2423
import os
2524
import sys
26-
from typing import Optional, TYPE_CHECKING
25+
26+
try:
27+
from typing import TYPE_CHECKING, Optional
28+
except ImportError:
29+
TYPE_CHECKING = False
30+
2731
from .constants import chips
2832

2933
if TYPE_CHECKING:
30-
from . import Detector
34+
from .protocols import DetectorProtocol
35+
else:
36+
DetectorProtocol = ...
3137

3238
__version__ = "0.0.0-auto.0"
3339
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PlatformDetect.git"
@@ -36,7 +42,8 @@
3642
class Chip:
3743
"""Attempt detection of current chip / CPU."""
3844

39-
def __init__(self, detector: Detector) -> None:
45+
# pylint: disable=used-before-assignment
46+
def __init__(self, detector: DetectorProtocol) -> None:
4047
self.detector = detector
4148
self._chip_id = None
4249

adafruit_platformdetect/protocols.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# SPDX-FileCopyrightText: 2022 Chris Wilson <[email protected]>
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
"""
6+
`adafruit_platformdetect.protocols`
7+
================================================================================
8+
9+
Protocol definitions
10+
11+
* Author(s): Chris Wilson
12+
13+
Implementation Notes
14+
--------------------
15+
16+
**Software and Dependencies:**
17+
18+
* Linux and Python 3.7 or Higher
19+
20+
"""
21+
22+
from typing import Optional
23+
24+
# Protocol was introduced in Python 3.8.
25+
try:
26+
from typing import Protocol
27+
except ImportError:
28+
from typing_extensions import Protocol
29+
30+
31+
class DetectorProtocol(Protocol):
32+
"""Wrap various platform detection functions."""
33+
34+
def get_cpuinfo_field(self, field: str) -> Optional[str]:
35+
"""
36+
Search /proc/cpuinfo for a field and return its value, if found,
37+
otherwise None.
38+
"""
39+
...
40+
41+
def check_dt_compatible_value(self, value: str) -> bool:
42+
"""
43+
Search /proc/device-tree/compatible for a value and return True, if found,
44+
otherwise False.
45+
"""
46+
...
47+
48+
def get_armbian_release_field(self, field: str) -> Optional[str]:
49+
"""
50+
Search /etc/armbian-release, if it exists, for a field and return its
51+
value, if found, otherwise None.
52+
"""
53+
...
54+
55+
def get_device_model(self) -> Optional[str]:
56+
"""
57+
Search /proc/device-tree/model for the device model and return its value, if found,
58+
otherwise None.
59+
"""
60+
...
61+
62+
def get_device_compatible(self) -> Optional[str]:
63+
"""
64+
Search /proc/device-tree/compatible for the compatible chip name.
65+
"""
66+
...
67+
68+
def check_board_asset_tag_value(self) -> Optional[str]:
69+
"""
70+
Search /sys/devices/virtual/dmi/id for the device model and return its value, if found,
71+
otherwise None.
72+
"""
73+
...
74+
75+
def check_board_name_value(self) -> Optional[str]:
76+
"""
77+
Search /sys/devices/virtual/dmi/id for the board name and return its value, if found,
78+
otherwise None. Debian/ubuntu based
79+
"""
80+
...

bin/detect.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
1616
**Software and Dependencies:**
1717
18-
* Linux and Python 3.5 or Higher
18+
* Linux and Python 3.7 or Higher
1919
2020
"""
2121

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
autodoc_mock_imports = ["machine"]
2828

2929
intersphinx_mapping = {
30-
"python": ("https://docs.python.org/3.6", None),
30+
"python": ("https://docs.python.org/3.7", None),
3131
"CircuitPython": ("https://circuitpython.readthedocs.io/en/latest/", None),
3232
}
3333

requirements.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# SPDX-FileCopyrightText: 2022 Chris Wilson <[email protected]>
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
typing_extensions; python_version <= '3.7'

setup.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@
2828
description="Platform detection for use by libraries like Adafruit-Blinka.",
2929
long_description=long_description,
3030
long_description_content_type="text/x-rst",
31-
python_requires=">=3.6.0",
31+
python_requires=">=3.7.0",
3232
url="https://github.com/adafruit/Adafruit_Python_PlatformDetect",
3333
# If your package is a single module, use this instead of 'packages':
3434
author="Adafruit Industries",
3535
author_email="[email protected]",
36-
install_requires=[],
36+
install_requires=["typing_extensions; python_version <= '3.7'"],
3737
# Choose your license
3838
license="MIT",
3939
# See https://pypi.python.org/pypi?%3Aaction=list_classifiers
@@ -44,7 +44,7 @@
4444
"Topic :: System :: Hardware",
4545
"License :: OSI Approved :: MIT License",
4646
"Programming Language :: Python :: 3",
47-
"Programming Language :: Python :: 3.6",
47+
"Programming Language :: Python :: 3.7",
4848
],
4949
packages=["adafruit_platformdetect", "adafruit_platformdetect.constants"],
5050
)

0 commit comments

Comments
 (0)