eric6/MicroPython/CircuitPythonDevices.py

branch
micropython
changeset 7138
a2a53535d855
parent 7134
21d23ca51680
child 7145
ceb3e8b242c1
--- a/eric6/MicroPython/CircuitPythonDevices.py	Tue Aug 13 17:52:58 2019 +0200
+++ b/eric6/MicroPython/CircuitPythonDevices.py	Thu Aug 15 17:04:54 2019 +0200
@@ -10,11 +10,12 @@
 from __future__ import unicode_literals
 
 import shutil
+import os
 
 from PyQt5.QtCore import pyqtSlot
 from PyQt5.QtWidgets import QDialog
 
-from E5Gui import E5MessageBox
+from E5Gui import E5MessageBox, E5FileDialog
 
 from .MicroPythonDevices import MicroPythonDevice
 from .MicroPythonWidget import HAS_QTCHART
@@ -174,9 +175,10 @@
         act = menu.addAction(self.tr("Flash CircuitPython Firmware"),
                              self.__flashCircuitPython)
         act.setEnabled(not connected)
-        # TODO: add menu entry to copy a .mpy or .py file to the
-        #       device library (creating the /lib folder if it doesn't
-        #       exist already)
+        menu.addSeparator()
+        act = menu.addAction(self.tr("Install Library Files"),
+                             self.__installLibraryFiles)
+        act.setEnabled(self.__deviceVolumeMounted())
     
     @pyqtSlot()
     def __flashCircuitPython(self):
@@ -198,3 +200,33 @@
             if dlg.exec_() == QDialog.Accepted:
                 cpyPath, devicePath = dlg.getData()
                 shutil.copy2(cpyPath, devicePath)
+    
+    @pyqtSlot()
+    def __installLibraryFiles(self):
+        """
+        Private slot to install Python files into the onboard library.
+        """
+        if not self.__deviceVolumeMounted():
+            E5MessageBox.critical(
+                self.microPython,
+                self.tr("Install Library Files"),
+                self.tr("""The device volume "<b>{0}</b>" is not available."""
+                        """ Ensure it is mounted properly and try again."""))
+            return
+        
+        target = os.path.join(self.getWorkspace(), "lib")
+        # ensure that the library directory exists on the device
+        if not os.path.isdir(target):
+            os.makedirs(target)
+        
+        libraryFiles = E5FileDialog.getOpenFileNames(
+            self.microPython,
+            self.tr("Install Library Files"),
+            os.path.expanduser("~"),
+            self.tr("Compiled Python Files (*.mpy);;"
+                    "Python Files (*.py);;"
+                    "All Files (*)"))
+        
+        for libraryFile in libraryFiles:
+            if os.path.exists(libraryFile):
+                shutil.copy2(libraryFile, target)

eric ide

mercurial