src/eric7/MicroPython/MicroPythonDevices.py

branch
eric7
changeset 9496
05017f795c24
parent 9482
a2bc06a54d9d
child 9498
d48061567354
--- a/src/eric7/MicroPython/MicroPythonDevices.py	Wed Nov 09 09:42:19 2022 +0100
+++ b/src/eric7/MicroPython/MicroPythonDevices.py	Wed Nov 09 10:37:21 2022 +0100
@@ -8,6 +8,8 @@
 class.
 """
 
+import contextlib
+import importlib
 import logging
 import os
 
@@ -408,35 +410,23 @@
     @return instantiated device interface
     @rtype MicroPythonDevice
     """
-    if deviceType == "esp":
-        from .EspDevices import EspDevice  # __IGNORE_WARNING_I101__
-
-        return EspDevice(microPythonWidget, deviceType)
-    elif deviceType == "circuitpython":
-        from .CircuitPythonDevices import CircuitPythonDevice  # __IGNORE_WARNING_I101__
-
-        return CircuitPythonDevice(microPythonWidget, deviceType)
-    elif deviceType in ("bbc_microbit", "calliope"):
-        from .MicrobitDevices import MicrobitDevice  # __IGNORE_WARNING_I101__
-
-        return MicrobitDevice(microPythonWidget, deviceType)
-    elif deviceType == "pyboard":
-        from .PyBoardDevices import PyBoardDevice  # __IGNORE_WARNING_I101__
+    deviceMapping = {
+        "bbc_microbit": ".MicrobitDevices",
+        "calliope": ".MicrobitDevices",
+        "circuitpython": ".CircuitPythonDevices",
+        "esp": ".EspDevices",
+        "generic": ".GenericMicroPythonDevices",
+        "pyboard": ".PyBoardDevices",
+        "rp2040": ".RP2040Devices",
+    }
 
-        return PyBoardDevice(microPythonWidget, deviceType)
-    elif deviceType == "rp2040":
-        from .RP2040Devices import RP2040Device  # __IGNORE_WARNING_I101__
+    with contextlib.suppress(KeyError):
+        mod = importlib.import_module(deviceMapping[deviceType], __package__)
+        if mod:
+            return mod.createDevice(microPythonWidget, deviceType, vid, pid)
 
-        return RP2040Device(microPythonWidget, deviceType)
-    elif deviceType == "generic":
-        from .GenericMicroPythonDevices import (  # __IGNORE_WARNING_I101__
-            GenericMicroPythonDevice,
-        )
-
-        return GenericMicroPythonDevice(microPythonWidget, deviceType, vid, pid)
-    else:
-        # nothing specific requested
-        return MicroPythonDevice(microPythonWidget, deviceType)
+    # nothing specific requested or specific one failed or is not supported yet
+    return MicroPythonDevice(microPythonWidget, deviceType)
 
 
 class MicroPythonDevice(QObject):

eric ide

mercurial