eric6/MicroPython/MicroPythonCommandsInterface.py

branch
micropython
changeset 7126
376deb7fefe7
parent 7124
1965daf1a14b
child 7136
bddcb634a9b8
diff -r 2028553ee58c -r 376deb7fefe7 eric6/MicroPython/MicroPythonCommandsInterface.py
--- a/eric6/MicroPython/MicroPythonCommandsInterface.py	Wed Aug 07 16:08:46 2019 +0200
+++ b/eric6/MicroPython/MicroPythonCommandsInterface.py	Wed Aug 07 16:10:12 2019 +0200
@@ -67,6 +67,8 @@
         """
         super(MicroPythonCommandsInterface, self).__init__(parent)
         
+        self.__repl = parent
+        
         self.__blockReadyRead = False
         
         self.__serial = MicroPythonSerialPort(
@@ -291,11 +293,19 @@
         @rtype tuple of str
         @exception IOError raised to indicate an issue with the device
         """
-        commands = [
-            "import os as __os_",
-            "print(__os_.listdir('{0}'))".format(dirname),
-            "del __os_",
-        ]
+        if self.__repl.isMicrobit():
+            # BBC micro:bit does not support directories
+            commands = [
+                "import os as __os_",
+                "print(__os_.listdir())",
+                "del __os_",
+            ]
+        else:
+            commands = [
+                "import os as __os_",
+                "print(__os_.listdir('{0}'))".format(dirname),
+                "del __os_",
+            ]
         out, err = self.execute(commands)
         if err:
             raise IOError(self.__shortError(err))
@@ -317,35 +327,58 @@
         @rtype tuple of (str, tuple)
         @exception IOError raised to indicate an issue with the device
         """
-        commands = [
-            "import os as __os_",
-            "\n".join([
-                "def is_visible(filename):",
-                "    return filename[0] != '.' and filename[-1] != '~'",
-            ]),
-            "\n".join([
-                "def stat(filename):",
-                "    try:",
-                "        rstat = __os_.lstat(filename)",
-                "    except:",
-                "        rstat = __os_.stat(filename)",
-                "    return tuple(rstat)",
-            ]),
-            "\n".join([
-                "def listdir_stat(dirname):",
-                "    try:",
-                "        files = __os_.listdir(dirname)",
-                "    except OSError:",
-                "        return None",
-                "    if dirname in ('', '/'):",
-                "        return list((f, stat(f)) for f in files if"
-                " is_visible(f))",
-                "    return list((f, stat(dirname + '/' + f)) for f in files"
-                " if is_visible(f))",
-            ]),
-            "print(listdir_stat('{0}'))".format(dirname),
-            "del __os_, stat, listdir_stat",
-        ]
+        if self.__repl.isMicrobit():
+            # BBC micro:bit does not support directories
+            commands = [
+                "import os as __os_",
+                "\n".join([
+                    "def is_visible(filename):",
+                    "    return filename[0] != '.' and filename[-1] != '~'",
+                ]),
+                "\n".join([
+                    "def stat(filename):",
+                    "    size = __os_.size(filename)",
+                    "    return (0, 0, 0, 0, 0, 0, size, 0, 0, 0)"
+                ]),
+                "\n".join([
+                    "def listdir_stat():",
+                    "    files = __os_.listdir()",
+                    "    return list((f, stat(f)) for f in files if"
+                    " is_visible(f))",
+                ]),
+                "print(listdir_stat())",
+                "del __os_, stat, listdir_stat, is_visible",
+            ]
+        else:
+            commands = [
+                "import os as __os_",
+                "\n".join([
+                    "def is_visible(filename):",
+                    "    return filename[0] != '.' and filename[-1] != '~'",
+                ]),
+                "\n".join([
+                    "def stat(filename):",
+                    "    try:",
+                    "        rstat = __os_.lstat(filename)",
+                    "    except:",
+                    "        rstat = __os_.stat(filename)",
+                    "    return tuple(rstat)",
+                ]),
+                "\n".join([
+                    "def listdir_stat(dirname):",
+                    "    try:",
+                    "        files = __os_.listdir(dirname)",
+                    "    except OSError:",
+                    "        return None",
+                    "    if dirname in ('', '/'):",
+                    "        return list((f, stat(f)) for f in files if"
+                    " is_visible(f))",
+                    "    return list((f, stat(dirname + '/' + f))"
+                    " for f in files if is_visible(f))",
+                ]),
+                "print(listdir_stat('{0}'))".format(dirname),
+                "del __os_, stat, listdir_stat, is_visible",
+            ]
         out, err = self.execute(commands)
         if err:
             raise IOError(self.__shortError(err))
@@ -385,6 +418,10 @@
         @rtype str
         @exception IOError raised to indicate an issue with the device
         """
+        if self.__repl.isMicrobit():
+            # BBC micro:bit does not support directories
+            return ""
+        
         commands = [
             "import os as __os_",
             "print(__os_.getcwd())",

eric ide

mercurial