src/eric7/MicroPython/Devices/DeviceBase.py

branch
mpy_network
changeset 9989
286c2a21f36f
parent 9979
dbafba79461d
child 10019
e56089d00750
--- a/src/eric7/MicroPython/Devices/DeviceBase.py	Mon Apr 24 17:51:11 2023 +0200
+++ b/src/eric7/MicroPython/Devices/DeviceBase.py	Thu Apr 27 17:59:09 2023 +0200
@@ -118,7 +118,7 @@
         super().__init__(parent)
 
         self._deviceType = deviceType
-        self._interface = microPythonWidget.deviceInterface()
+        self._interface = None
         self.microPython = microPythonWidget
         self._deviceData = {}  # dictionary with essential device data
 
@@ -137,6 +137,7 @@
         self._deviceData = {}
 
         if connected:
+            self._interface = self.microPython.deviceInterface()
             with contextlib.suppress(OSError):
                 self._deviceData = self.__getDeviceData()
                 self._deviceData["local_mip"] = (
@@ -149,6 +150,8 @@
                     self._deviceData["ethernet_type"],
                 ) = self.hasEthernet()
                 self._deviceData["ntp"] = self.hasNetworkTime()
+        else:
+            self._interface = None
 
     def getDeviceType(self):
         """
@@ -381,6 +384,29 @@
             # user cancelled
             return ""
 
+    def executeCommands(self, commands, *, mode="raw", timeout=0):
+        """
+        Public method to send commands to the connected device and return the
+        result.
+
+        If no connected interface is available, empty results will be returned.
+
+        @param commands list of commands to be executed
+        @type str or list of str
+        @keyparam mode submit mode to be used (one of 'raw' or 'paste') (defaults to
+            'raw')
+        @type str
+        @keyparam timeout per command timeout in milliseconds (0 for configured default)
+            (defaults to 0)
+        @type int (optional)
+        @return tuple containing stdout and stderr output of the device
+        @rtype tuple of (bytes, bytes)
+        """
+        if self._interface is None:
+            return b"", b""
+
+        return self._interface.execute(commands, mode=mode, timeout=timeout)
+
     def sendCommands(self, commandsList):
         """
         Public method to send a list of commands to the device.
@@ -388,25 +414,8 @@
         @param commandsList list of commands to be sent to the device
         @type list of str
         """
-        if self._submitMode == "raw":
-            rawOn = [  # sequence of commands to enter raw mode
-                b"\x02",  # Ctrl-B: exit raw repl (just in case)
-                b"\r\x03\x03\x03",  # Ctrl-C three times: interrupt any running program
-                b"\r\x01",  # Ctrl-A: enter raw REPL
-            ]
-            newLine = [
-                b'print("\\n")\r',
-            ]
-            commands = [c.encode("utf-8)") + b"\r" for c in commandsList]
-            commands.append(b"\r")
-            commands.append(b"\x04")
-            rawOff = [b"\x02", b"\x02"]
-            commandSequence = rawOn + newLine + commands + rawOff
-            self._interface.executeAsync(commandSequence)
-        elif self._submitMode == "paste":
-            commands = b"\n".join([c.encode("utf-8)") for c in commandsList])
-            commandSequence = ["@PasteOn@", commands]
-            self._interface.executeAsyncPaste(commandSequence)
+        if self._interface is not None:
+            self._interface.executeAsync(commandsList, self._submitMode)
 
     @pyqtSlot()
     def handleDataFlood(self):
@@ -543,7 +552,7 @@
 """.format(
             repr(pathname)
         )
-        out, err = self._interface.execute(command, mode=self._submitMode)
+        out, err = self.executeCommands(command, mode=self._submitMode)
         if err:
             raise OSError(self._shortError(err))
         return out.strip() == b"True"
@@ -565,7 +574,7 @@
 """.format(
             dirname
         )
-        out, err = self._interface.execute(command, mode=self._submitMode)
+        out, err = self.executeCommands(command, mode=self._submitMode)
         if err:
             raise OSError(self._shortError(err))
         return ast.literal_eval(out.decode("utf-8"))
@@ -617,7 +626,7 @@
 """.format(
             dirname, showHidden
         )
-        out, err = self._interface.execute(command, mode=self._submitMode)
+        out, err = self.executeCommands(command, mode=self._submitMode)
         if err:
             raise OSError(self._shortError(err))
         fileslist = ast.literal_eval(out.decode("utf-8"))
@@ -645,7 +654,7 @@
 """.format(
                 dirname
             )
-            out, err = self._interface.execute(command, mode=self._submitMode)
+            out, err = self.executeCommands(command, mode=self._submitMode)
             if err:
                 raise OSError(self._shortError(err))
 
@@ -662,7 +671,7 @@
 print(__os_.getcwd())
 del __os_
 """
-        out, err = self._interface.execute(command, mode=self._submitMode)
+        out, err = self.executeCommands(command, mode=self._submitMode)
         if err:
             raise OSError(self._shortError(err))
         return out.decode("utf-8").strip()
@@ -687,7 +696,7 @@
 """.format(
                 filename
             )
-            out, err = self._interface.execute(command, mode=self._submitMode)
+            out, err = self.executeCommands(command, mode=self._submitMode)
             if err:
                 raise OSError(self._shortError(err))
 
@@ -734,7 +743,7 @@
 """.format(
                 name, recursive, force
             )
-            out, err = self._interface.execute(
+            out, err = self.executeCommands(
                 command, mode=self._submitMode, timeout=20000
             )
             if err:
@@ -759,7 +768,7 @@
 """.format(
                 dirname
             )
-            out, err = self._interface.execute(command, mode=self._submitMode)
+            out, err = self.executeCommands(command, mode=self._submitMode)
             if err:
                 raise OSError(self._shortError(err))
 
@@ -786,7 +795,7 @@
 """.format(
                 dirname
             )
-            out, err = self._interface.execute(command, mode=self._submitMode)
+            out, err = self.executeCommands(command, mode=self._submitMode)
             if err:
                 raise OSError(self._shortError(err))
 
@@ -848,7 +857,7 @@
         )
         command = "\n".join(commands)
 
-        out, err = self._interface.execute(command, mode=self._submitMode)
+        out, err = self.executeCommands(command, mode=self._submitMode)
         if err:
             raise OSError(self._shortError(err))
         return True
@@ -917,7 +926,7 @@
 """.format(
             deviceFileName
         )
-        out, err = self._interface.execute(command, mode=self._submitMode)
+        out, err = self.executeCommands(command, mode=self._submitMode)
         if err:
             raise OSError(self._shortError(err))
 
@@ -962,7 +971,7 @@
 print(fsinfo())
 del __os_, fsinfo
 """
-        out, err = self._interface.execute(command, mode=self._submitMode)
+        out, err = self.executeCommands(command, mode=self._submitMode)
         if err:
             raise OSError(self._shortError(err))
         infodict = ast.literal_eval(out.decode("utf-8"))
@@ -1084,7 +1093,7 @@
 print(get_device_data())
 del get_device_data
 """
-        out, err = self._interface.execute(command, mode=self._submitMode)
+        out, err = self.executeCommands(command, mode=self._submitMode)
         if err:
             raise OSError(self._shortError(err))
         return ast.literal_eval(out.decode("utf-8"))
@@ -1198,7 +1207,7 @@
 print(get_board_info())
 del get_board_info
 """
-        out, err = self._interface.execute(command, mode=self._submitMode)
+        out, err = self.executeCommands(command, mode=self._submitMode)
         if err:
             raise OSError(self._shortError(err))
         return ast.literal_eval(out.decode("utf-8"))
@@ -1212,7 +1221,7 @@
         @exception OSError raised to indicate an issue with the device
         """
         commands = ["help('modules')"]
-        out, err = self._interface.execute(commands, mode=self._submitMode)
+        out, err = self.executeCommands(commands, mode=self._submitMode)
         if err:
             raise OSError(self._shortError(err))
 
@@ -1255,7 +1264,7 @@
 get_time()
 del get_time
 """
-        out, err = self._interface.execute(command, mode=self._submitMode)
+        out, err = self.executeCommands(command, mode=self._submitMode)
         if err:
             if b"NotImplementedError" in err:
                 return "<unsupported> <unsupported>"
@@ -1327,7 +1336,7 @@
                     now.tm_isdst,
                 ),
             )
-            out, err = self._interface.execute(command, mode=self._submitMode)
+            out, err = self.executeCommands(command, mode=self._submitMode)
             if err:
                 raise OSError(self._shortError(err))
 
@@ -1354,7 +1363,7 @@
 """.format(
             repr(packages)
         )
-        return self._interface.execute(command, mode=self._submitMode, timeout=60000)
+        return self.executeCommands(command, mode=self._submitMode, timeout=60000)
 
     def mipInstall(self, package, index=None, target=None, version=None, mpy=True):
         """
@@ -1393,7 +1402,7 @@
 """.format(
             parameterStr
         )
-        return self._interface.execute(command, mode=self._submitMode, timeout=60000)
+        return self.executeCommands(command, mode=self._submitMode, timeout=60000)
 
     def getLibPaths(self):
         """
@@ -1411,7 +1420,7 @@
 lib_paths()
 del lib_paths
 """
-        out, err = self._interface.execute(command, mode=self._submitMode)
+        out, err = self.executeCommands(command, mode=self._submitMode)
         if err:
             raise OSError(self._shortError(err))
 

eric ide

mercurial