Josverl / micropython-stubber

Generate and maintain stubs for different MicroPython ports to use with VSCode and Pylance, PyRight, Thonny, PyCharm or MyPy
https://micropython-stubber.readthedocs.io
Other
172 stars 14 forks source link

getinfo: determine boardname of firmware #357

Closed Josverl closed 1 year ago

Josverl commented 1 year ago

depends on #285

Josverl commented 1 year ago

working propotype

mpremote COM16 mount . get-port

"stubber: get-port"
import gc
import os
import sys

try:
    from collections import OrderedDict
except ImportError:
    from ucollections import OrderedDict  # type: ignore

info = OrderedDict(
    {
        "family": sys.implementation.name,
        "version": "",
        "port": "stm32" if sys.platform.startswith("pyb") else sys.platform,  # port: esp32 / win32 / linux / stm32
        "board": "GENERIC",
        "cpu": "",
        "build": "",
        "board_d": "",
    }
)
try:
    info["version"] = ".".join([str(n) for n in sys.implementation.version])
except AttributeError:
    pass
try:
    machine = sys.implementation._machine if "_machine" in dir(sys.implementation) else os.uname().machine
    info["board"] = machine.strip()
    info["cpu"] = machine.split("with")[1].strip()
except (AttributeError, IndexError):
    pass

gc.collect()
try:
    # look up the board name in the board_info.csv file
    with open("board_info.csv", "r") as f:
        b = info["board"]
        for line in f:
            descr_, board_ = line.split(",")[1].strip(), line.split(",")[0].strip()
            if board_ == b:
                info["board_d"] = board_
                info["board"] = descr_
                break
    info["board"] = info["board"].replace(" ", "_")
except (AttributeError, IndexError, OSError):
    pass
gc.collect()

try:
    info["build"] = sys.version.split(";")[1].strip().split(" ")[1].split("-")[1]
except (AttributeError, IndexError):
    pass

if info["version"] == "" and sys.platform not in ("unix", "win32"):
    try:
        u = os.uname()
        info["version"] = u.release
    except (IndexError, AttributeError, TypeError):
        pass

try:  # families
    from pycopy import const as _t  # type: ignore

    info["family"] = "pycopy"
    del _t
except (ImportError, KeyError):
    pass
try:  # families
    from pycom import FAT as _t  # type: ignore

    info["family"] = "pycom"
    del _t
except (ImportError, KeyError):
    pass

if info["family"] == "micropython":
    if (
        info["version"]
        and info["version"].endswith(".0")
        and info["version"] >= "1.10.0"  # versions from 1.10.0 to 1.20.0 do not have a micro .0
        and info["version"] <= "1.20.0"
    ):
        # drop the .0 for newer releases
        info["version"] = info["version"][:-2]

# output the info dict as a string with the OrderedDict() removed
print(str(info).replace("OrderedDict(", "").rstrip(")"))