src/eric7/MicroPython/Devices/DeviceBase.py

branch
eric7
changeset 11186
c2d18aefef6b
parent 11183
553b50b72f5b
child 11190
f5ffdf0164ab
--- a/src/eric7/MicroPython/Devices/DeviceBase.py	Fri Mar 21 18:12:17 2025 +0100
+++ b/src/eric7/MicroPython/Devices/DeviceBase.py	Sun Mar 23 14:55:14 2025 +0100
@@ -550,10 +550,55 @@
 
         return self.tr("Detected an error without indications.")
 
+    def isMicrobit(self):
+        """
+        Public method to check, if the device is a BBC micro:bit or Calliope mini.
+
+        @return flag indicating a micro:bit device
+        @rtype bool
+        """
+        return False
+
     ##################################################################
     ## Methods below implement the file system commands
     ##################################################################
 
+    def supportsDirectories(self):
+        """
+        Public method to check, if the device supports directory operations.
+
+        @return flag indicating directory operations are supported
+        @rtype bool
+        """
+        command = """
+import os as __os_
+print('getcwd' in dir(__os_))
+del __os_
+"""
+        out, err = self.executeCommands(command, mode=self._submitMode)
+        if err:
+            return False
+
+        return out.strip() == b"True"
+
+    def supportsRenameFiles(self):
+        """
+        Public method to check, if the device supports renaming of files.
+
+        @return flag indicating renaming of files are supported
+        @rtype bool
+        """
+        command = """
+import os as __os_
+print('getcwd' in dir(__os_))
+del __os_
+"""
+        out, err = self.executeCommands(command, mode=self._submitMode)
+        if err:
+            return False
+
+        return out.strip() == b"True"
+
     def exists(self, pathname):
         """
         Public method to check the existence of a file or directory.
@@ -879,6 +924,7 @@
         @rtype bool
         @exception OSError raised to indicate an issue with the device
         """
+        # TODO: micro:bit with old MPy does not support leading '/'
         if not deviceFileName:
             raise OSError("Missing device file name")
 
@@ -941,6 +987,7 @@
         @rtype bytes
         @exception OSError raised to indicate an issue with the device
         """
+        # TODO: micro:bit with old MPy does not support leading '/'
         if not deviceFileName:
             raise OSError("Missing device file name")
 
@@ -1164,7 +1211,7 @@
 
 print(get_device_data())
 del get_device_data
-"""
+""",
         ]
         res = {}
         for command in commands:
@@ -1174,15 +1221,36 @@
             res.update(ast.literal_eval(out.decode("utf-8")))
         return res
 
-    def getBoardInformation(self):
+    def _boardInformationCommands(self):
         """
-        Public method to get some information data of the connected board.
+        Protected method defining the list of commands to be execute on the board
+        for determining information about the board.
+
+        @return list of command strings to be executed
+        @rtype list of str
+        """
+        return [  # needs to be splitted for boards with low memory
+            """def get_board_info():
+    res = {}
 
-        @return dictionary containing the determined data
-        @rtype dict
-        @exception OSError raised to indicate an issue with the device
-        """
-        commands = [  # needs to be splitted for boards with low memory
+    import gc
+    gc.enable()
+    gc.collect()
+    mem_alloc = gc.mem_alloc()
+    mem_free = gc.mem_free()
+    mem_total = mem_alloc + mem_free
+    res['mem_total_kb'] = mem_total / 1024.0
+    res['mem_used_kb'] = mem_alloc / 1024.0
+    res['mem_used_pc'] = mem_alloc / mem_total * 100.0
+    res['mem_free_kb'] = mem_free / 1024.0
+    res['mem_free_pc'] = mem_free / mem_total * 100.0
+    del gc, mem_alloc, mem_free, mem_total
+
+    return res
+
+print(get_board_info())
+del get_board_info
+""",
             """def get_board_info():
     res = {}
 
@@ -1246,6 +1314,53 @@
     res = {}
 
     try:
+        import os
+        stat_ = os.statvfs('/flash')
+        res['flash_info_available'] = True
+        res['flash_total_kb'] = stat_[2] * stat_[0] / 1024.0
+        res['flash_free_kb'] = stat_[3] * stat_[0] / 1024.0
+        res['flash_used_kb'] = res['flash_total_kb'] - res['flash_free_kb']
+        res['flash_free_pc'] = res['flash_free_kb'] / res['flash_total_kb'] * 100.0
+        res['flash_used_pc'] = res['flash_used_kb'] / res['flash_total_kb'] * 100.0
+    except (AttributeError, OSError):
+        res['flash_info_available'] = False
+
+    return res
+
+print(get_board_info())
+del get_board_info
+""",
+            """def get_board_info():
+    res = {}
+
+    try:
+        import machine as mc
+        if isinstance(mc.freq(), tuple):
+            res['mc_frequency_mhz'] = mc.freq()[0] / 1000000.0
+        else:
+           res['mc_frequency_mhz'] = mc.freq() / 1000000.0
+        res['mc_id'] = mc.unique_id()
+    except ImportError:
+        try:
+            import microcontroller as mc
+            res['mc_frequency_mhz'] = mc.cpu.frequency / 1000000.0
+            res['mc_temp_c'] = mc.cpu.temperature
+            res['mc_id'] = mc.cpu.uid
+        except ImportError:
+            res['mc_frequency'] = None
+            res['mc_temp'] = None
+    if 'mc_id' in res:
+        res['mc_id'] = ':'.join('{0:02X}'.format(x) for x in res['mc_id'])
+
+    return res
+
+print(get_board_info())
+del get_board_info
+""",
+            """def get_board_info():
+    res = {}
+
+    try:
         import ulab
         res['ulab'] = ulab.__version__
     except ImportError:
@@ -1258,123 +1373,22 @@
 """,
         ]
 
-        gc_command = """def get_board_info():
-    res = {}
-
-    import gc
-    gc.enable()
-    gc.collect()
-    mem_alloc = gc.mem_alloc()
-    mem_free = gc.mem_free()
-    mem_total = mem_alloc + mem_free
-    res['mem_total'] = mem_total
-    res['mem_used'] = mem_alloc
-    res['mem_free'] = mem_free
-    del gc, mem_alloc, mem_free, mem_total
-
-    return res
-
-print(get_board_info())
-del get_board_info
-"""
-
-        flash_command = """def get_board_info():
-    res = {}
-
-    try:
-        import os
-        stat_ = os.statvfs('/flash')
-        res['flash_info_available'] = True
-        res['flash_total'] = stat_[2] * stat_[0]
-        res['flash_free'] = stat_[3] * stat_[0]
-    except (AttributeError, OSError):
-        res['flash_info_available'] = False
-
-    return res
-
-print(get_board_info())
-del get_board_info
-"""
+    def getBoardInformation(self):
+        """
+        Public method to get some information data of the connected board.
 
-        machine_command = """def get_board_info():
-    res = {}
+        @return dictionary containing the determined data
+        @rtype dict
+        @exception OSError raised to indicate an issue with the device
+        """
+        res = {}
 
-    try:
-        import machine as mc
-        try:
-            if isinstance(mc.freq(), tuple):
-                res['mc_frequency'] = mc.freq()[0]
-            else:
-               res['mc_frequency'] = mc.freq()
-        except NotImplementedError:
-            res['mc_frequency'] = None
-        try:
-            res['mc_temp_c'] = mc.Temp().read()
-        except AttributeError:
-            res['mc_temp_c'] = None
-        res['mc_id'] = mc.unique_id()
-    except ImportError:
-        try:
-            import microcontroller as mc
-            res['mc_frequency'] = mc.cpu.frequency
-            res['mc_temp_c'] = mc.cpu.temperature
-            res['mc_id'] = mc.cpu.uid
-        except ImportError:
-            res['mc_frequency'] = None
-            res['mc_temp_c'] = None
-    if 'mc_id' in res:
-        res['mc_id'] = ':'.join('{0:02X}'.format(x) for x in res['mc_id'])
-    else:
-        res['mc_id'] = None
-
-    return res
-
-print(get_board_info())
-del get_board_info
-"""
-        res = {}
-        for command in commands:
+        for command in self._boardInformationCommands():
             out, err = self.executeCommands(command, mode=self._submitMode)
             if err:
                 raise OSError(self._shortError(err))
             res.update(ast.literal_eval(out.decode("utf-8")))
 
-        # Execute the commands needing some special postprocessing due to some
-        # boards supporting integer only.
-        out, err = self.executeCommands(gc_command, mode=self._submitMode)
-        if err:
-            raise OSError(self._shortError(err))
-        gc_res = ast.literal_eval(out.decode("utf-8"))
-        res['mem_total_kb'] = gc_res["mem_total"] / 1024.0
-        res['mem_used_kb'] = gc_res["mem_used"] / 1024.0
-        res['mem_used_pc'] = gc_res["mem_used"] / gc_res["mem_total"] * 100.0
-        res['mem_free_kb'] = gc_res["mem_free"] / 1024.0
-        res['mem_free_pc'] = gc_res["mem_free"] / gc_res["mem_total"] * 100.0
-
-        out, err = self.executeCommands(flash_command, mode=self._submitMode)
-        if err:
-            raise OSError(self._shortError(err))
-        flash_res = ast.literal_eval(out.decode("utf-8"))
-        res['flash_info_available'] = flash_res["flash_info_available"]
-        if flash_res["flash_info_available"]:
-            res['flash_total_kb'] = flash_res["flash_total"] / 1024.0
-            res['flash_free_kb'] = flash_res["flash_free"] / 1024.0
-            res['flash_used_kb'] = res['flash_total_kb'] - res['flash_free_kb']
-            res['flash_free_pc'] = res['flash_free_kb'] / res['flash_total_kb'] * 100.0
-            res['flash_used_pc'] = res['flash_used_kb'] / res['flash_total_kb'] * 100.0
-
-        out, err = self.executeCommands(machine_command, mode=self._submitMode)
-        if err:
-            raise OSError(self._shortError(err))
-        machine_res = ast.literal_eval(out.decode("utf-8"))
-        res['mc_frequency_mhz'] = (
-            machine_res["mc_frequency"] / 1000000.0
-            if machine_res["mc_frequency"] is not None
-            else None
-        )
-        res["mc_temp_c"] = machine_res["mc_temp_c"]
-        res["mc_id"] = machine_res["mc_id"]
-
         return res
 
     def getModules(self):

eric ide

mercurial