Skip to content

Fixes #220 for platforms that do not support typing #222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .readthedocs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
version: 2

python:
version: "3.6"
version: "3.7"
install:
- requirements: docs/requirements.txt
- requirements: requirements.txt
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Dependencies
=============
This driver depends on:

* Python 3.6 or higher
* Python 3.7 or higher

Installing from PyPI
=====================
Expand Down
6 changes: 5 additions & 1 deletion adafruit_platformdetect/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
Attempt to detect the current platform.
"""
import re
from typing import Optional

try:
from typing import Optional
except ImportError:
pass
from .board import Board
from .chip import Chip

Expand Down
17 changes: 12 additions & 5 deletions adafruit_platformdetect/board.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,25 @@

**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 re
from typing import Optional, TYPE_CHECKING

try:
from typing import TYPE_CHECKING, Optional
except ImportError:
TYPE_CHECKING = False

from .constants import boards, chips

if TYPE_CHECKING:
from . import Detector
from .protocols import DetectorProtocol
else:
DetectorProtocol = ...

__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PlatformDetect.git"
Comment on lines 39 to 40
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dhalbert or @makermelissa can you give me some context as to why these lines are included in __init__.py, board.py, and chip.py (vs. just having it in __init__.py)? Just wondering if these should be added to any new files I add (like protocols.py that I added in this draft)

Expand All @@ -37,7 +43,8 @@
class Board:
"""Attempt to detect specific boards."""

def __init__(self, detector: Detector) -> None:
# pylint: disable=used-before-assignment
def __init__(self, detector: DetectorProtocol) -> None:
self.detector = detector
self._board_id = None

Expand Down
15 changes: 11 additions & 4 deletions adafruit_platformdetect/chip.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,20 @@
"""

# imports
from __future__ import annotations
import os
import sys
from typing import Optional, TYPE_CHECKING

try:
from typing import TYPE_CHECKING, Optional
except ImportError:
TYPE_CHECKING = False

from .constants import chips

if TYPE_CHECKING:
from . import Detector
from .protocols import DetectorProtocol
else:
DetectorProtocol = ...

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

def __init__(self, detector: Detector) -> None:
# pylint: disable=used-before-assignment
def __init__(self, detector: DetectorProtocol) -> None:
self.detector = detector
self._chip_id = None

Expand Down
80 changes: 80 additions & 0 deletions adafruit_platformdetect/protocols.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# SPDX-FileCopyrightText: 2022 Chris Wilson <[email protected]>
#
# SPDX-License-Identifier: MIT

"""
`adafruit_platformdetect.protocols`
================================================================================

Protocol definitions

* Author(s): Chris Wilson

Implementation Notes
--------------------

**Software and Dependencies:**

* Linux and Python 3.7 or Higher

"""

from typing import Optional

# Protocol was introduced in Python 3.8.
try:
from typing import Protocol
except ImportError:
from typing_extensions import Protocol


class DetectorProtocol(Protocol):
"""Wrap various platform detection functions."""

def get_cpuinfo_field(self, field: str) -> Optional[str]:
"""
Search /proc/cpuinfo for a field and return its value, if found,
otherwise None.
"""
...

def check_dt_compatible_value(self, value: str) -> bool:
"""
Search /proc/device-tree/compatible for a value and return True, if found,
otherwise False.
"""
...

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.
"""
...

def get_device_model(self) -> Optional[str]:
"""
Search /proc/device-tree/model for the device model and return its value, if found,
otherwise None.
"""
...

def get_device_compatible(self) -> Optional[str]:
"""
Search /proc/device-tree/compatible for the compatible chip name.
"""
...

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.
"""
...

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
"""
...
2 changes: 1 addition & 1 deletion bin/detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

**Software and Dependencies:**

* Linux and Python 3.5 or Higher
* Linux and Python 3.7 or Higher

"""

Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
autodoc_mock_imports = ["machine"]

intersphinx_mapping = {
"python": ("https://docs.python.org/3.6", None),
"python": ("https://docs.python.org/3.7", None),
"CircuitPython": ("https://circuitpython.readthedocs.io/en/latest/", None),
}

Expand Down
5 changes: 5 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# SPDX-FileCopyrightText: 2022 Chris Wilson <[email protected]>
#
# SPDX-License-Identifier: MIT

typing_extensions; python_version <= '3.7'
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
description="Platform detection for use by libraries like Adafruit-Blinka.",
long_description=long_description,
long_description_content_type="text/x-rst",
python_requires=">=3.6.0",
python_requires=">=3.7.0",
url="https://github.com/adafruit/Adafruit_Python_PlatformDetect",
# If your package is a single module, use this instead of 'packages':
author="Adafruit Industries",
author_email="[email protected]",
install_requires=[],
install_requires=["typing_extensions; python_version <= '3.7'"],
# Choose your license
license="MIT",
# See https://pypi.python.org/pypi?%3Aaction=list_classifiers
Expand All @@ -44,7 +44,7 @@
"Topic :: System :: Hardware",
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
],
packages=["adafruit_platformdetect", "adafruit_platformdetect.constants"],
)