diff --git a/belay/__init__.py b/belay/__init__.py index 05adb55..b4b326b 100644 --- a/belay/__init__.py +++ b/belay/__init__.py @@ -3,12 +3,15 @@ __all__ = [ "AuthenticationError", - "minify", + "ConnectionLost", "Device", - "SpecialFunctionNameError", - "PyboardException", - "Implementation", "FeatureUnavailableError", + "Implementation", + "MaxHistoryLengthError", + "PyboardException", + "SpecialFunctionNameError", + "list_devices", + "minify", ] from ._minify import minify from .device import Device, Implementation @@ -19,4 +22,5 @@ MaxHistoryLengthError, SpecialFunctionNameError, ) +from .helpers import list_devices from .pyboard import PyboardException diff --git a/belay/helpers.py b/belay/helpers.py index d6e2bbc..4bffdb0 100644 --- a/belay/helpers.py +++ b/belay/helpers.py @@ -2,6 +2,9 @@ import secrets import string from functools import lru_cache, partial, wraps +from typing import List + +from serial.tools import list_ports from . import snippets @@ -22,3 +25,18 @@ def random_python_identifier(n=16): @lru_cache def read_snippet(name): return pkg_resources.read_text(snippets, f"{name}.py") + + +def list_devices() -> List[str]: + """Lists available device ports. + + For example:: + + ['/dev/cu.usbmodem1143401', '/dev/cu.usbmodem113101'] + + Returns + ------- + list[str] + Available devices identifiers. + """ + return [port.device for port in list_ports.comports()]