eric6/MicroPython/MicroPythonFileSystem.py

branch
micropython
changeset 7077
3b7475b7a1ef
parent 7070
3368ce0e7879
child 7080
9a3adf033f90
--- a/eric6/MicroPython/MicroPythonFileSystem.py	Sat Jul 20 14:48:09 2019 +0200
+++ b/eric6/MicroPython/MicroPythonFileSystem.py	Sun Jul 21 19:54:15 2019 +0200
@@ -13,7 +13,9 @@
 import time
 import stat
 
-from PyQt5.QtCore import QObject, QThread
+from PyQt5.QtCore import pyqtSlot, pyqtSignal, QObject, QThread
+
+from .MicroPythonSerialPort import MicroPythonSerialPort
 
 
 class MicroPythonFileSystem(QObject):
@@ -328,60 +330,132 @@
         @exception IOError raised to indicate an issue with the device
         """
         # TODO: not implemented yet
+
+
+class MicroPythonFileManager(QObject):
+    """
+    Class implementing an interface to the device file system commands with
+    some additional sugar.
     
-    ##################################################################
-    ## Utility methods below
-    ##################################################################
+    @signal longListFiles(result) emitted with a tuple of tuples containing the
+        name, mode, size and time for each directory entry
+    @signal currentDir(dirname) emitted to report the current directory of the
+        device
     
-    def mtime2string(self, mtime):
+    @signal longListFilesFailed(exc) emitted with a failure message to indicate
+        a failed long listing operation
+    @signal currentDirFailed(exc) emitted with a failure message to indicate
+        that the current directory is not available
+    """
+    longListFiles = pyqtSignal(tuple)
+    currentDir = pyqtSignal(str)
+    
+    longListFilesFailed = pyqtSignal(str)
+    currentDirFailed = pyqtSignal(str)
+    
+    def __init__(self, port, parent=None):
         """
-        Public method to convert a time value to a string representation.
+        Constructor
         
-        @param mtime time value
-        @type int
-        @return string representation of the given time
-        @rtype str
+        @param port port name of the device
+        @type str
+        @param parent reference to the parent object
+        @type QObject
         """
-        return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(mtime))
+        super(MicroPythonFileManager, self).__init__(parent)
+        
+        self.__serialPort = port
+        self.__serial = MicroPythonSerialPort(parent=self)
+        self.__fs = MicroPythonFileSystem(parent=self)
     
-    def mode2string(self, mode):
+    @pyqtSlot()
+    def connect(self):
+        """
+        Public slot to start the manager.
         """
-        Public method to convert a mode value to a string representation.
+        self.__serial.openSerialLink(self.__serialPort)
+        self.__fs.setSerial(self.__serial)
+    
+    @pyqtSlot()
+    def disconnect(self):
+        """
+        Public slot to stop the thread.
+        """
+        self.__serial.closeSerialLink()
+    
+    @pyqtSlot(str)
+    def lls(self, dirname):
+        """
+        Public method to get a long listing of the given directory.
         
-        @param mode mode value
-        @type int
-        @return string representation of the given mode value
-        @rtype str
+        @param dirname name of the directory to list
+        @type str
         """
-        return stat.filemode(mode)
-# TODO: remove this
-##
-##if __name__ == "__main__":
-##    from PyQt5.QtCore import QCoreApplication, QTimer
-##    from MicroPythonSerialPort import MicroPythonSerialPort
-##    
-##    app = QCoreApplication([])
-##    
-##    serial = MicroPythonSerialPort()
-##    serial.openSerialLink("/dev/ttyUSB0")
-##    fs = MicroPythonFileSystem()
-##    fs.setSerial(serial)
-##    
-##    def tf():
-##        fs.cd("/flash")
-##        print(fs.pwd())
-##        fs.cd("odroid_go")
-##        print(fs.pwd())
-##        ll = fs.lls()
-##        print(ll)
-##        for f, (m, s, t) in ll:
-##            print(fs.mode2string(m), s, fs.mtime2string(t), f)
-##        fs.cd("..")
-##        print(fs.pwd())
-##        ll = fs.lls("odroid_go")
-##        print(ll)
-##        for f, (m, s, t) in ll:
-##            print(fs.mode2string(m), s, fs.mtime2string(t), f)
-##    
-##    QTimer.singleShot(0, tf)
-##    app.exec_()
+        try:
+            filesList = self.__fs.lls(dirname)
+            result = [(decoratedName(name, mode),
+                       mode2string(mode),
+                       str(size),
+                       mtime2string(time)) for
+                      name, (mode, size, time) in filesList]
+            self.longListFiles.emit(tuple(result))
+        except Exception as exc:
+            self.longListFilesFailed.emit(str(exc))
+    
+    @pyqtSlot()
+    def pwd(self):
+        """
+        Public method to get the current directory of the device.
+        """
+        try:
+            pwd = self.__fs.pwd()
+            self.currentDir.emit(pwd)
+        except Exception as exc:
+            self.currentDirFailed.emit(str(exc))
+
+##################################################################
+## Utility methods below
+##################################################################
+
+
+def mtime2string(mtime):
+    """
+    Function to convert a time value to a string representation.
+    
+    @param mtime time value
+    @type int
+    @return string representation of the given time
+    @rtype str
+    """
+    return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(mtime))
+
+
+def mode2string(mode):
+    """
+    Function to convert a mode value to a string representation.
+    
+    @param mode mode value
+    @type int
+    @return string representation of the given mode value
+    @rtype str
+    """
+    return stat.filemode(mode)
+
+
+def decoratedName(name, mode):
+    """
+    Function to decorate the given name according to the given mode.
+    
+    @param name file or directory name
+    @type str
+    @param mode mode value
+    @type int
+    @return decorated file or directory name
+    @rtype str
+    """
+    if stat.S_ISDIR(mode):
+        # append a '/' for directories
+        return name + "/"
+    else:
+        # no change
+        return name

eric ide

mercurial