--- a/src/eric7/MicroPython/Devices/MicrobitDevices.py Tue Feb 14 11:09:49 2023 +0100 +++ b/src/eric7/MicroPython/Devices/MicrobitDevices.py Tue Feb 14 18:10:30 2023 +0100 @@ -8,6 +8,7 @@ Calliope mini boards. """ +import ast import contextlib import os import shutil @@ -21,9 +22,9 @@ from eric7.EricWidgets.EricApplication import ericApp from eric7.SystemUtilities import FileSystemUtilities +from ..MicroPythonWidget import HAS_QTCHART from . import FirmwareGithubUrls from .DeviceBase import BaseDevice -from ..MicroPythonWidget import HAS_QTCHART class MicrobitDevice(BaseDevice): @@ -530,7 +531,7 @@ for line in script.splitlines(): commands.append("f(" + repr(line + "\n") + ")") commands.append("fd.close()") - out, err = self.microPython.commandsInterface().execute(commands) + out, err = self.microPython.deviceInterface().execute(commands) if err: EricMessageBox.critical( self.microPython, @@ -551,7 +552,7 @@ """ if self.getDeviceType() == "bbc_microbit": # BBC micro:bit - self.microPython.commandsInterface().execute( + self.microPython.deviceInterface().execute( [ "import microbit", "microbit.reset()", @@ -559,7 +560,7 @@ ) else: # Calliope mini - self.microPython.commandsInterface().execute( + self.microPython.deviceInterface().execute( [ "import calliope_mini", "calliope_mini.reset()", @@ -632,6 +633,124 @@ ), ] + ################################################################## + ## Methods below implement the file system commands + ################################################################## + + def ls(self, dirname=""): + """ + Public method to get a directory listing of the connected device. + + @param dirname name of the directory to be listed + @type str + @return tuple containg the directory listing + @rtype tuple of str + @exception OSError raised to indicate an issue with the device + """ + # BBC micro:bit does not support directories + commands = [ + "import os as __os_", + "print(__os_.listdir())", + "del __os_", + ] + out, err = self._interface.execute(commands) + if err: + raise OSError(self._shortError(err)) + return ast.literal_eval(out.decode("utf-8")) + + def lls(self, dirname="", fullstat=False, showHidden=False): + """ + Public method to get a long directory listing of the connected device + including meta data. + + @param dirname name of the directory to be listed + @type str + @param fullstat flag indicating to return the full stat() tuple + @type bool + @param showHidden flag indicating to show hidden files as well + @type bool + @return list containing the directory listing with tuple entries of + the name and and a tuple of mode, size and time (if fullstat is + false) or the complete stat() tuple. 'None' is returned in case the + directory doesn't exist. + @rtype tuple of (str, tuple) + @exception OSError raised to indicate an issue with the device + """ + # BBC micro:bit does not support directories + commands = [ + "import os as __os_", + "\n".join( + [ + "def is_visible(filename, showHidden):", + " return showHidden or " + "(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(showHidden):", + " files = __os_.listdir()", + " return list((f, stat(f)) for f in files if" + " is_visible(f,showHidden))", + ] + ), + "print(listdir_stat({0}))".format(showHidden), + "del __os_, stat, listdir_stat, is_visible", + ] + out, err = self._interface.execute(commands) + if err: + raise OSError(self._shortError(err)) + fileslist = ast.literal_eval(out.decode("utf-8")) + if fileslist is None: + return None + else: + if fullstat: + return fileslist + else: + return [(f, (s[0], s[6], s[8])) for f, s in fileslist] + + def pwd(self): + """ + Public method to get the current directory of the connected device. + + @return current directory + @rtype str + """ + # BBC micro:bit does not support directories + return "" + + ################################################################## + ## time related methods below + ################################################################## + + def _getSetTimeCode(self): + """ + Protected method to get the device code to set the time. + + Note: This method must be implemented in the various device specific + subclasses. + + @return code to be executed on the connected device to set the time + @rtype str + """ + # rtc_time[0] - year 4 digit + # rtc_time[1] - month 1..12 + # rtc_time[2] - day 1..31 + # rtc_time[3] - weekday 1..7 1=Monday + # rtc_time[4] - hour 0..23 + # rtc_time[5] - minute 0..59 + # rtc_time[6] - second 0..59 + # rtc_time[7] - yearday 1..366 + # rtc_time[8] - isdst 0, 1, or -1 + return "" + def createDevice(microPythonWidget, deviceType, vid, pid, boardName, serialNumber): """