Started to prepare the MicroPython file manager to access the device file system via a local directory. micropython

Sat, 10 Aug 2019 20:06:37 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 10 Aug 2019 20:06:37 +0200
branch
micropython
changeset 7130
6014d37d9683
parent 7129
3cc19aec959a
child 7131
f75e990caf99

Started to prepare the MicroPython file manager to access the device file system via a local directory.

eric6/MicroPython/MicroPythonFileManagerWidget.py file | annotate | diff | comparison | revisions
eric6/MicroPython/MicroPythonReplWidget.py file | annotate | diff | comparison | revisions
--- a/eric6/MicroPython/MicroPythonFileManagerWidget.py	Sat Aug 10 20:05:50 2019 +0200
+++ b/eric6/MicroPython/MicroPythonFileManagerWidget.py	Sat Aug 10 20:06:37 2019 +0200
@@ -43,12 +43,15 @@
     """
     Class implementing a file manager for MicroPython devices.
     """
-    def __init__(self, commandsInterface, parent=None):
+    def __init__(self, commandsInterface, deviceWithLocalAccess, parent=None):
         """
         Constructor
         
         @param commandsInterface reference to the commands interface object
         @type MicroPythonCommandsInterface
+        @param deviceWithLocalAccess flag indicating the device supports file
+            access via a local directory
+        @type bool
         @param parent reference to the parent widget
         @type QWidget
         """
@@ -56,6 +59,7 @@
         self.setupUi(self)
         
         self.__repl = parent
+        self.__deviceWithLocalAccess = deviceWithLocalAccess
         
         self.syncButton.setIcon(UI.PixmapCache.getIcon("2rightarrow"))
         self.putButton.setIcon(UI.PixmapCache.getIcon("1rightarrow"))
@@ -149,7 +153,12 @@
                        os.path.expanduser("~"))
         self.__listLocalFiles(dirname)
         
-        self.__fileManager.pwd()
+        if self.__deviceWithLocalAccess:
+            dirname = self.__repl.getDeviceWorkspace()
+            if dirname:
+                self.__listLocalFiles(dirname, True)
+        else:
+            self.__fileManager.pwd()
     
     def stop(self):
         """
@@ -203,19 +212,23 @@
         self.deviceFileTreeWidget.header().resizeSections(
             QHeaderView.ResizeToContents)
     
-    def __listLocalFiles(self, dirname=""):
+    def __listLocalFiles(self, dirname="", localDevice=False):
         """
         Private method to populate the local files list.
         
         @param dirname name of the local directory to be listed
         @type str
-        """     # __IGNORE_WARNING_D234__
-        
+        @param localDevice flag indicating device access via local file system
+        @type bool
+        """
         if not dirname:
             dirname = os.getcwd()
         if dirname.endswith(os.sep):
             dirname = dirname[:-1]
-        self.localCwd.setText(dirname)
+        if localDevice:
+            self.deviceCwd.setText(dirname)
+        else:
+            self.localCwd.setText(dirname)
         
         filesStatList = listdirStat(dirname)
         filesList = [(
@@ -223,12 +236,16 @@
             mode2string(s[0]),
             str(s[6]),
             mtime2string(s[8])) for f, s in filesStatList]
-        self.localFileTreeWidget.clear()
+        if localDevice:
+            fileTreeWidget = self.deviceFileTreeWidget
+        else:
+            fileTreeWidget = self.localFileTreeWidget
+        fileTreeWidget.clear()
         for item in filesList:
-            itm = QTreeWidgetItem(self.localFileTreeWidget, item)
+            itm = QTreeWidgetItem(fileTreeWidget, item)
             itm.setTextAlignment(1, Qt.AlignHCenter)
             itm.setTextAlignment(2, Qt.AlignRight)
-        self.localFileTreeWidget.header().resizeSections(
+        fileTreeWidget.header().resizeSections(
             QHeaderView.ResizeToContents)
     
     @pyqtSlot(QTreeWidgetItem, int)
@@ -281,6 +298,7 @@
         dirname = self.localCwd.text()
         self.__listLocalFiles(dirname)
     
+    # TODO:
     @pyqtSlot(QTreeWidgetItem, int)
     def on_deviceFileTreeWidget_itemActivated(self, item, column):
         """
@@ -312,6 +330,7 @@
         self.getButton.setEnabled(enable)
         self.getAsButton.setEnabled(enable)
     
+    # TODO:
     @pyqtSlot()
     def on_deviceUpButton_clicked(self):
         """
@@ -321,6 +340,7 @@
         dirname = os.path.dirname(cwd)
         self.__fileManager.cd(dirname)
     
+    # TODO:
     @pyqtSlot()
     def on_deviceReloadButton_clicked(self):
         """
@@ -350,6 +370,7 @@
         
         return False
     
+    # TODO:
     @pyqtSlot()
     def on_putButton_clicked(self, putAs=False):
         """
@@ -404,6 +425,7 @@
         """
         self.on_putButton_clicked(putAs=True)
     
+    # TODO:
     @pyqtSlot()
     def on_getButton_clicked(self, getAs=False):
         """
@@ -472,6 +494,7 @@
         """
         self.__listLocalFiles(self.localCwd.text())
     
+    # TODO:
     @pyqtSlot()
     def on_syncButton_clicked(self):
         """
@@ -555,38 +578,56 @@
         self.__localMenu.exec_(self.localFileTreeWidget.mapToGlobal(pos))
     
     @pyqtSlot()
-    def __changeLocalDirectory(self):
+    def __changeLocalDirectory(self, localDevice=False):
         """
         Private slot to change the local directory.
+        
+        @param localDevice flag indicating device access via local file system
+        @type bool
         """
+        if localDevice:
+            cwdWidget = self.deviceCwd
+        else:
+            cwdWidget = self.localCwd
+        
         dirPath, ok = E5PathPickerDialog.getPath(
             self,
             self.tr("Change Directory"),
             self.tr("Select Directory"),
             E5PathPickerModes.DirectoryShowFilesMode,
-            defaultDirectory=self.localCwd.text(),
+            path=cwdWidget.text(),
+            defaultDirectory=cwdWidget.text(),
         )
         if ok and dirPath:
             if not os.path.isabs(dirPath):
-                dirPath = os.path.join(self.localCwd.text(), dirPath)
-            self.localCwd.setText(dirPath)
-            self.__listLocalFiles(dirPath)
+                dirPath = os.path.join(cwdWidget.text(), dirPath)
+            cwdWidget.setText(dirPath)
+            self.__listLocalFiles(dirPath, localDevice=localDevice)
     
     @pyqtSlot()
-    def __createLocalDirectory(self):
+    def __createLocalDirectory(self, localDevice=False):
         """
         Private slot to create a local directory.
+        
+        @param localDevice flag indicating device access via local file system
+        @type bool
         """
+        if localDevice:
+            cwdWidget = self.deviceCwd
+        else:
+            cwdWidget = self.localCwd
+        
         dirPath, ok = QInputDialog.getText(
             self,
             self.tr("Create Directory"),
             self.tr("Enter directory name:"),
             QLineEdit.Normal)
         if ok and dirPath:
-            dirPath = os.path.join(self.localCwd.text(), dirPath)
+            dirPath = os.path.join(cwdWidget.text(), dirPath)
             try:
                 os.mkdir(dirPath)
-                self.__listLocalFiles(self.localCwd.text())
+                self.__listLocalFiles(cwdWidget.text(),
+                                      localDevice=localDevice)
             except (OSError, IOError) as exc:
                 E5MessageBox.critical(
                     self,
@@ -597,13 +638,23 @@
                 )
     
     @pyqtSlot()
-    def __deleteLocalDirectoryTree(self):
+    def __deleteLocalDirectoryTree(self, localDevice=False):
         """
         Private slot to delete a local directory tree.
+        
+        @param localDevice flag indicating device access via local file system
+        @type bool
         """
-        if bool(len(self.localFileTreeWidget.selectedItems())):
-            name = self.localFileTreeWidget.selectedItems()[0].text(0)
-            dirname = os.path.join(self.localCwd.text(), name[:-1])
+        if localDevice:
+            cwdWidget = self.deviceCwd
+            fileTreeWidget = self.deviceFileTreeWidget
+        else:
+            cwdWidget = self.localCwd
+            fileTreeWidget = self.localFileTreeWidget
+        
+        if bool(len(fileTreeWidget.selectedItems())):
+            name = fileTreeWidget.selectedItems()[0].text(0)
+            dirname = os.path.join(cwdWidget.text(), name[:-1])
             dlg = DeleteFilesConfirmationDialog(
                 self,
                 self.tr("Delete Directory Tree"),
@@ -613,7 +664,8 @@
             if dlg.exec_() == QDialog.Accepted:
                 try:
                     shutil.rmtree(dirname)
-                    self.__listLocalFiles(self.localCwd.text())
+                    self.__listLocalFiles(cwdWidget.text(),
+                                          localDevice=localDevice)
                 except Exception as exc:
                     E5MessageBox.critical(
                         self,
@@ -623,6 +675,7 @@
                             dirname, str(exc))
                     )
     
+    # TODO:
     @pyqtSlot()
     def __deleteLocalFile(self):
         """
@@ -649,7 +702,8 @@
                                 """ deleted.</p><p>Reason: {1}</p>""").format(
                             filename, str(exc))
                     )
-   
+    
+    # TODO: move to repl widget super menu
     @pyqtSlot()
     def __showLocalTime(self):
         """
@@ -704,46 +758,55 @@
         Note: This triggers a re-population of the device list for the new
         current directory.
         """
-        dirPath, ok = QInputDialog.getText(
-            self,
-            self.tr("Change Directory"),
-            self.tr("Enter the directory path on the device:"),
-            QLineEdit.Normal,
-            self.deviceCwd.text())
-        if ok and dirPath:
-            if not dirPath.startswith("/"):
-                dirPath = self.deviceCwd.text() + "/" + dirPath
-            self.__fileManager.cd(dirPath)
+        if self.__deviceWithLocalAccess:
+            self.__changeLocalDirectory(True)
+        else:
+            dirPath, ok = QInputDialog.getText(
+                self,
+                self.tr("Change Directory"),
+                self.tr("Enter the directory path on the device:"),
+                QLineEdit.Normal,
+                self.deviceCwd.text())
+            if ok and dirPath:
+                if not dirPath.startswith("/"):
+                    dirPath = self.deviceCwd.text() + "/" + dirPath
+                self.__fileManager.cd(dirPath)
     
     @pyqtSlot()
     def __createDeviceDirectory(self):
         """
         Private slot to create a directory on the device.
         """
-        dirPath, ok = QInputDialog.getText(
-            self,
-            self.tr("Create Directory"),
-            self.tr("Enter directory name:"),
-            QLineEdit.Normal)
-        if ok and dirPath:
-            self.__fileManager.mkdir(dirPath)
+        if self.__deviceWithLocalAccess:
+            self.__createLocalDirectory(True)
+        else:
+            dirPath, ok = QInputDialog.getText(
+                self,
+                self.tr("Create Directory"),
+                self.tr("Enter directory name:"),
+                QLineEdit.Normal)
+            if ok and dirPath:
+                self.__fileManager.mkdir(dirPath)
     
     @pyqtSlot()
     def __deleteDeviceDirectory(self):
         """
         Private slot to delete an empty directory on the device.
         """
-        if bool(len(self.deviceFileTreeWidget.selectedItems())):
-            name = self.deviceFileTreeWidget.selectedItems()[0].text(0)
-            dirname = self.deviceCwd.text() + "/" + name[:-1]
-            dlg = DeleteFilesConfirmationDialog(
-                self,
-                self.tr("Delete Directory"),
-                self.tr(
-                    "Do you really want to delete this directory?"),
-                [dirname])
-            if dlg.exec_() == QDialog.Accepted:
-                self.__fileManager.rmdir(dirname)
+        if self.__deviceWithLocalAccess:
+            self.__deleteLocalDirectoryTree(True)
+        else:
+            if bool(len(self.deviceFileTreeWidget.selectedItems())):
+                name = self.deviceFileTreeWidget.selectedItems()[0].text(0)
+                dirname = self.deviceCwd.text() + "/" + name[:-1]
+                dlg = DeleteFilesConfirmationDialog(
+                    self,
+                    self.tr("Delete Directory"),
+                    self.tr(
+                        "Do you really want to delete this directory?"),
+                    [dirname])
+                if dlg.exec_() == QDialog.Accepted:
+                    self.__fileManager.rmdir(dirname)
     
     @pyqtSlot()
     def __deleteDeviceDirectoryTree(self):
@@ -751,18 +814,22 @@
         Private slot to delete a directory and all its subdirectories
         recursively.
         """
-        if bool(len(self.deviceFileTreeWidget.selectedItems())):
-            name = self.deviceFileTreeWidget.selectedItems()[0].text(0)
-            dirname = self.deviceCwd.text() + "/" + name[:-1]
-            dlg = DeleteFilesConfirmationDialog(
-                self,
-                self.tr("Delete Directory Tree"),
-                self.tr(
-                    "Do you really want to delete this directory tree?"),
-                [dirname])
-            if dlg.exec_() == QDialog.Accepted:
-                self.__fileManager.rmdir(dirname, recursive=True)
+        if self.__deviceWithLocalAccess:
+            self.__deleteLocalDirectoryTree(True)
+        else:
+            if bool(len(self.deviceFileTreeWidget.selectedItems())):
+                name = self.deviceFileTreeWidget.selectedItems()[0].text(0)
+                dirname = self.deviceCwd.text() + "/" + name[:-1]
+                dlg = DeleteFilesConfirmationDialog(
+                    self,
+                    self.tr("Delete Directory Tree"),
+                    self.tr(
+                        "Do you really want to delete this directory tree?"),
+                    [dirname])
+                if dlg.exec_() == QDialog.Accepted:
+                    self.__fileManager.rmdir(dirname, recursive=True)
     
+    # TODO:
     @pyqtSlot()
     def __deleteDeviceFile(self):
         """
--- a/eric6/MicroPython/MicroPythonReplWidget.py	Sat Aug 10 20:05:50 2019 +0200
+++ b/eric6/MicroPython/MicroPythonReplWidget.py	Sat Aug 10 20:06:37 2019 +0200
@@ -820,6 +820,18 @@
             # return with device path prepended
             return "/dev/{0}".format(portName)
     
+    def getDeviceWorkspace(self):
+        """
+        Public method to get the workspace directory of the device.
+        
+        @return workspace directory of the device
+        @rtype str
+        """
+        if self.__device:
+            return self.__device.getWorkspace()
+        else:
+            return ""
+    
     def __connectToDevice(self):
         """
         Private method to connect to the selected device.
@@ -1030,7 +1042,8 @@
             if not self.__interface.isConnected():
                 self.__connectToDevice()
             self.__fileManagerWidget = MicroPythonFileManagerWidget(
-                self.__interface, self)
+                self.__interface, self.__device.supportsLocalFileAccess,
+                self)
             
             self.__ui.addSideWidget(self.__ui.BottomSide,
                                     self.__fileManagerWidget,

eric ide

mercurial