Changed MicroPython device imports to use importlib.import_module(). eric7

Wed, 09 Nov 2022 10:37:21 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Wed, 09 Nov 2022 10:37:21 +0100
branch
eric7
changeset 9496
05017f795c24
parent 9495
28ab5f487f71
child 9497
8beca4047c53

Changed MicroPython device imports to use importlib.import_module().

src/eric7/MicroPython/CircuitPythonDevices.py file | annotate | diff | comparison | revisions
src/eric7/MicroPython/EspDevices.py file | annotate | diff | comparison | revisions
src/eric7/MicroPython/GenericMicroPythonDevices.py file | annotate | diff | comparison | revisions
src/eric7/MicroPython/MicroPythonDevices.py file | annotate | diff | comparison | revisions
src/eric7/MicroPython/MicroPythonWidget.py file | annotate | diff | comparison | revisions
src/eric7/MicroPython/MicrobitDevices.py file | annotate | diff | comparison | revisions
src/eric7/MicroPython/PyBoardDevices.py file | annotate | diff | comparison | revisions
src/eric7/MicroPython/RP2040Devices.py file | annotate | diff | comparison | revisions
src/eric7/Preferences/ConfigurationPages/MicroPythonPage.py file | annotate | diff | comparison | revisions
--- a/src/eric7/MicroPython/CircuitPythonDevices.py	Wed Nov 09 09:42:19 2022 +0100
+++ b/src/eric7/MicroPython/CircuitPythonDevices.py	Wed Nov 09 10:37:21 2022 +0100
@@ -324,3 +324,21 @@
                 Preferences.getMicroPython("CircuitPythonLibrariesUrl"),
             ),
         ]
+
+
+def createDevice(microPythonWidget, deviceType, vid, pid):
+    """
+    Function to instantiate a MicroPython device object.
+
+    @param microPythonWidget reference to the main MicroPython widget
+    @type MicroPythonWidget
+    @param deviceType device type assigned to this device interface
+    @type str
+    @param vid vendor ID
+    @type int
+    @param pid product ID
+    @type int
+    @return reference to the instantiated device object
+    @rtype CircuitPythonDevice
+    """
+    return CircuitPythonDevice(microPythonWidget, deviceType)
--- a/src/eric7/MicroPython/EspDevices.py	Wed Nov 09 09:42:19 2022 +0100
+++ b/src/eric7/MicroPython/EspDevices.py	Wed Nov 09 10:37:21 2022 +0100
@@ -463,3 +463,21 @@
         @rtype str
         """
         return Preferences.getMicroPython("MicroPythonFirmwareUrl")
+
+
+def createDevice(microPythonWidget, deviceType, vid, pid):
+    """
+    Function to instantiate a MicroPython device object.
+
+    @param microPythonWidget reference to the main MicroPython widget
+    @type MicroPythonWidget
+    @param deviceType device type assigned to this device interface
+    @type str
+    @param vid vendor ID
+    @type int
+    @param pid product ID
+    @type int
+    @return reference to the instantiated device object
+    @rtype EspDevice
+    """
+    return EspDevice(microPythonWidget, deviceType)
--- a/src/eric7/MicroPython/GenericMicroPythonDevices.py	Wed Nov 09 09:42:19 2022 +0100
+++ b/src/eric7/MicroPython/GenericMicroPythonDevices.py	Wed Nov 09 10:37:21 2022 +0100
@@ -200,3 +200,21 @@
                 )
 
             return super().getWorkspace()
+
+
+def createDevice(microPythonWidget, deviceType, vid, pid):
+    """
+    Function to instantiate a MicroPython device object.
+
+    @param microPythonWidget reference to the main MicroPython widget
+    @type MicroPythonWidget
+    @param deviceType device type assigned to this device interface
+    @type str
+    @param vid vendor ID
+    @type int
+    @param pid product ID
+    @type int
+    @return reference to the instantiated device object
+    @rtype GenericMicroPythonDevice
+    """
+    return GenericMicroPythonDevice(microPythonWidget, deviceType, vid, pid)
--- 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):
--- a/src/eric7/MicroPython/MicroPythonWidget.py	Wed Nov 09 09:42:19 2022 +0100
+++ b/src/eric7/MicroPython/MicroPythonWidget.py	Wed Nov 09 10:37:21 2022 +0100
@@ -498,10 +498,11 @@
             vid = self.deviceTypeComboBox.itemData(index, self.DeviceVidRole)
             pid = self.deviceTypeComboBox.itemData(index, self.DevicePidRole)
 
-            self.__device = MicroPythonDevices.getDevice(deviceType, self, vid, pid)
-            self.__device.setButtons()
+            if deviceType or (pid is not None and pid is not None):
+                self.__device = MicroPythonDevices.getDevice(deviceType, self, vid, pid)
+                self.__device.setButtons()
 
-            self.connectButton.setEnabled(bool(deviceType))
+                self.connectButton.setEnabled(bool(deviceType))
 
     @pyqtSlot()
     def on_checkButton_clicked(self):
--- a/src/eric7/MicroPython/MicrobitDevices.py	Wed Nov 09 09:42:19 2022 +0100
+++ b/src/eric7/MicroPython/MicrobitDevices.py	Wed Nov 09 10:37:21 2022 +0100
@@ -434,3 +434,21 @@
                     Preferences.getMicroPython("CalliopeDAPLinkUrl"),
                 ),
             ]
+
+
+def createDevice(microPythonWidget, deviceType, vid, pid):
+    """
+    Function to instantiate a MicroPython device object.
+
+    @param microPythonWidget reference to the main MicroPython widget
+    @type MicroPythonWidget
+    @param deviceType device type assigned to this device interface
+    @type str
+    @param vid vendor ID
+    @type int
+    @param pid product ID
+    @type int
+    @return reference to the instantiated device object
+    @rtype MicrobitDevice
+    """
+    return MicrobitDevice(microPythonWidget, deviceType)
--- a/src/eric7/MicroPython/PyBoardDevices.py	Wed Nov 09 09:42:19 2022 +0100
+++ b/src/eric7/MicroPython/PyBoardDevices.py	Wed Nov 09 10:37:21 2022 +0100
@@ -414,3 +414,21 @@
             )
             # simulate pressing the disconnect button
             self.microPython.on_connectButton_clicked()
+
+
+def createDevice(microPythonWidget, deviceType, vid, pid):
+    """
+    Function to instantiate a MicroPython device object.
+
+    @param microPythonWidget reference to the main MicroPython widget
+    @type MicroPythonWidget
+    @param deviceType device type assigned to this device interface
+    @type str
+    @param vid vendor ID
+    @type int
+    @param pid product ID
+    @type int
+    @return reference to the instantiated device object
+    @rtype PyBoardDevice
+    """
+    return PyBoardDevice(microPythonWidget, deviceType)
--- a/src/eric7/MicroPython/RP2040Devices.py	Wed Nov 09 09:42:19 2022 +0100
+++ b/src/eric7/MicroPython/RP2040Devices.py	Wed Nov 09 10:37:21 2022 +0100
@@ -191,3 +191,21 @@
                 Preferences.getMicroPython("CircuitPythonLibrariesUrl"),
             ),
         ]
+
+
+def createDevice(microPythonWidget, deviceType, vid, pid):
+    """
+    Function to instantiate a MicroPython device object.
+
+    @param microPythonWidget reference to the main MicroPython widget
+    @type MicroPythonWidget
+    @param deviceType device type assigned to this device interface
+    @type str
+    @param vid vendor ID
+    @type int
+    @param pid product ID
+    @type int
+    @return reference to the instantiated device object
+    @rtype RP2040Device
+    """
+    return RP2040Device(microPythonWidget, deviceType)
--- a/src/eric7/Preferences/ConfigurationPages/MicroPythonPage.py	Wed Nov 09 09:42:19 2022 +0100
+++ b/src/eric7/Preferences/ConfigurationPages/MicroPythonPage.py	Wed Nov 09 10:37:21 2022 +0100
@@ -21,6 +21,7 @@
     QChart = None
 
 
+# TODO: add option to enable/disable manual device selection
 class MicroPythonPage(ConfigurationPageBase, Ui_MicroPythonPage):
     """
     Class implementing the MicroPython configuration page.

eric ide

mercurial