Beispiel #1
0
 def add_fs(self):
     """
     If the REPL is not active, add the file system navigator to the UI.
     """
     if self.repl is None and self.fs is None:
         # Check for micro:bit
         try:
             microfs.get_serial()
         except IOError:
             # abort
             message = _('Could not find an attached BBC micro:bit.')
             information = _("Please make sure the device is plugged "
                             "into this computer.\n\nThe device must "
                             "have MicroPython flashed onto it before "
                             "the file system will work.\n\n"
                             "Finally, press the device's reset button "
                             "and wait a few seconds before trying "
                             "again.")
             self.view.show_message(message, information)
             return
         self.file_manager_thread = QThread(self)
         self.file_manager = FileManager()
         self.file_manager.moveToThread(self.file_manager_thread)
         self.file_manager_thread.started.\
             connect(self.file_manager.on_start)
         self.fs = self.view.add_filesystem(self.workspace_dir(),
                                            self.file_manager)
         self.fs.set_message.connect(self.editor.show_status_message)
         self.fs.set_warning.connect(self.view.show_message)
         self.file_manager_thread.start()
Beispiel #2
0
 def add_fs(self):
     """
     Add the file system navigator to the UI.
     """
     # Check for micro:bit
     try:
         microfs.get_serial()
     except IOError:
         # abort
         message = _('Could not find an attached BBC micro:bit.')
         information = _("Please make sure the device is plugged "
                         "into this computer.\n\nThe device must "
                         "have MicroPython flashed onto it before "
                         "the file system will work.\n\n"
                         "Finally, press the device's reset button "
                         "and wait a few seconds before trying "
                         "again.")
         self.view.show_message(message, information)
         return
     self.file_manager_thread = QThread(self)
     self.file_manager = FileManager()
     self.file_manager.moveToThread(self.file_manager_thread)
     self.file_manager_thread.started.\
         connect(self.file_manager.on_start)
     self.fs = self.view.add_filesystem(self.workspace_dir(),
                                        self.file_manager)
     self.fs.set_message.connect(self.editor.show_status_message)
     self.fs.set_warning.connect(self.view.show_message)
     self.file_manager_thread.start()
Beispiel #3
0
 def copy_main(self):
     """
     If the attribute self.python_script contains any code, copy it onto the
     connected micro:bit as main.py, then restart the board (CTRL-D).
     """
     if self.python_script.strip():
         script = self.python_script
         logger.info('Copying main.py onto device')
         commands = [
             "fd = open('main.py', 'wb')",
             "f = fd.write",
         ]
         while script:
             line = script[:64]
             commands.append('f(' + repr(line) + ')')
             script = script[64:]
         commands.append('fd.close()')
         logger.info(commands)
         serial = microfs.get_serial()
         out, err = microfs.execute(commands, serial)
         logger.info((out, err))
         if err:
             raise IOError(microfs.clean_error(err))
         # Reset the device.
         serial.write(b'import microbit\r\n')
         serial.write(b'microbit.reset()\r\n')
         self.editor.show_status_message(_('Copied code onto micro:bit.'))
     self.python_script = ''
Beispiel #4
0
 def copy_main(self):
     """
     If the attribute self.python_script contains any code, copy it onto the
     connected micro:bit as main.py, then restart the board (CTRL-D).
     """
     if self.python_script.strip():
         script = self.python_script
         logger.info('Copying main.py onto device')
         commands = [
             "fd = open('main.py', 'wb')",
             "f = fd.write",
         ]
         while script:
             line = script[:64]
             commands.append('f(' + repr(line) + ')')
             script = script[64:]
         commands.append('fd.close()')
         logger.info(commands)
         serial = microfs.get_serial()
         out, err = microfs.execute(commands, serial)
         logger.info((out, err))
         if err:
             raise IOError(microfs.clean_error(err))
         # Send CTRL-D for soft restart.
         serial.write(b'\x04')
     self.python_script = ''
Beispiel #5
0
 def copy_main(self):
     """
     If the attribute self.python_script contains any code, copy it onto the
     connected micro:bit as main.py, then restart the board (CTRL-D).
     """
     if self.python_script.strip():
         script = self.python_script
         logger.info('Copying main.py onto device')
         commands = [
             "fd = open('main.py', 'wb')",
             "f = fd.write",
         ]
         while script:
             line = script[:64]
             commands.append('f(' + repr(line) + ')')
             script = script[64:]
         commands.append('fd.close()')
         logger.info(commands)
         serial = microfs.get_serial()
         out, err = microfs.execute(commands, serial)
         logger.info((out, err))
         if err:
             raise IOError(microfs.clean_error(err))
         # Reset the device.
         serial.write(b'import microbit\r\n')
         serial.write(b'microbit.reset()\r\n')
         self.editor.show_status_message(_('Copied code onto micro:bit.'))
     self.python_script = ''
Beispiel #6
0
 def dropEvent(self, event):
     source = event.source()
     self.disable(source)
     if isinstance(source, MicrobitFileList):
         file_exists = self.findItems(source.currentItem().text(),
                                      Qt.MatchExactly)
         if not file_exists or \
                 file_exists and self.show_confirm_overwrite_dialog():
             microbit_filename = source.currentItem().text()
             local_filename = os.path.join(self.home, microbit_filename)
             logger.debug("Getting {} to {}".format(microbit_filename,
                                                    local_filename))
             try:
                 with microfs.get_serial() as serial:
                     logger.info(serial.port)
                     # microfs.get(serial, microbit_filename, local_filename)
                     board = pyboard.Pyboard(serial.port)
                     board_files = files.Files(board)
                     fileBytes = board_files.get(microbit_filename)
                     with open(local_filename, 'wb') as local:
                         local.write(fileBytes)
                 super().dropEvent(event)
             except Exception as ex:
                 logger.error(ex)
     self.enable(source)
     if self.parent() is not None:
         self.parent().ls()
Beispiel #7
0
 def ls(self):
     """
     List the files on the micro:bit. Emit the resulting tuple of filenames
     or emit a failure signal.
     """
     try:
         result = tuple(microfs.ls(microfs.get_serial()))
         self.on_list_files.emit(result)
     except Exception as ex:
         logger.exception(ex)
         self.on_list_fail.emit()
Beispiel #8
0
 def ls(self):
     """
     List the files on the micro:bit. Emit the resulting tuple of filenames
     or emit a failure signal.
     """
     try:
         result = tuple(microfs.ls(microfs.get_serial()))
         self.on_list_files.emit(result)
     except Exception as ex:
         logger.exception(ex)
         self.on_list_fail.emit()
Beispiel #9
0
Datei: logic.py Projekt: ntoll/mu
 def add_fs(self):
     """
     If the REPL is not active, add the file system navigator to the UI.
     """
     if self.repl is None:
         if self.fs is None:
             try:
                 microfs.get_serial()
                 self._view.add_filesystem(home=PYTHON_DIRECTORY)
                 self.fs = True
             except IOError:
                 message = 'Could not find an attached BBC micro:bit.'
                 information = ("Please make sure the device is plugged "
                                "into this computer.\n\nThe device must "
                                "have MicroPython flashed onto it before "
                                "the file system will work.\n\n"
                                "Finally, press the device's reset button "
                                "and wait a few seconds before trying "
                                "again.")
                 self._view.show_message(message, information)
Beispiel #10
0
 def add_fs(self):
     """
     If the REPL is not active, add the file system navigator to the UI.
     """
     if self.repl is None:
         if self.fs is None:
             try:
                 microfs.get_serial()
                 self._view.add_filesystem(home=get_workspace_dir())
                 self.fs = True
             except IOError:
                 message = 'Could not find an attached BBC micro:bit.'
                 information = ("Please make sure the device is plugged "
                                "into this computer.\n\nThe device must "
                                "have MicroPython flashed onto it before "
                                "the file system will work.\n\n"
                                "Finally, press the device's reset button "
                                "and wait a few seconds before trying "
                                "again.")
                 self._view.show_message(message, information)
Beispiel #11
0
 def delete(self, microbit_filename):
     """
     Delete the referenced file on the micro:bit's filesystem. Emit the name
     of the file when complete, or emit a failure signal.
     """
     try:
         with microfs.get_serial() as serial:
             microfs.rm(microbit_filename, serial)
         self.on_delete_file.emit(microbit_filename)
     except Exception as ex:
         logger.error(ex)
         self.on_delete_fail(microbit_filename)
Beispiel #12
0
 def delete(self, microbit_filename):
     """
     Delete the referenced file on the micro:bit's filesystem. Emit the name
     of the file when complete, or emit a failure signal.
     """
     try:
         with microfs.get_serial() as serial:
             microfs.rm(microbit_filename, serial)
         self.on_delete_file.emit(microbit_filename)
     except Exception as ex:
         logger.error(ex)
         self.on_delete_fail.emit(microbit_filename)
Beispiel #13
0
 def put(self, local_filename):
     """
     Put the referenced local file onto the filesystem on the micro:bit.
     Emit the name of the file on the micro:bit when complete, or emit
     a failure signal.
     """
     try:
         with microfs.get_serial() as serial:
             microfs.put(local_filename, serial)
         self.on_put_file.emit(os.path.basename(local_filename))
     except Exception as ex:
         logger.error(ex)
         self.on_put_fail.emit(local_filename)
Beispiel #14
0
 def get(self, microbit_filename, local_filename):
     """
     Get the referenced micro:bit filename and save it to the local
     filename. Emit the name of the filename when complete or emit a
     failure signal.
     """
     try:
         with microfs.get_serial() as serial:
             microfs.get(microbit_filename, local_filename, serial)
         self.on_get_file.emit(microbit_filename)
     except Exception as ex:
         logger.error(ex)
         self.on_get_fail.emit(microbit_filename)
Beispiel #15
0
 def get(self, microbit_filename, local_filename):
     """
     Get the referenced micro:bit filename and save it to the local
     filename. Emit the name of the local filename when complete or emit a
     failure signal.
     """
     try:
         with microfs.get_serial() as serial:
             microfs.get(microbit_filename, local_filename, serial)
         self.on_get_file.emit(microbit_filename)
     except Exception as ex:
         logger.error(ex)
         self.on_get_fail(microbit_filename)
Beispiel #16
0
 def put(self, local_filename):
     """
     Put the referenced local file onto the filesystem on the micro:bit.
     Emit the name of the file on the micro:bit when complete, or emit
     a failure signal.
     """
     try:
         with microfs.get_serial() as serial:
             microfs.put(local_filename, serial)
         self.on_put_file.emit(os.path.basename(local_filename))
     except Exception as ex:
         logger.error(ex)
         self.on_put_fail(local_filename)
Beispiel #17
0
 def flash(self):
     """
     Takes the currently active tab, compiles the Python script therein into
     a hex file and flashes it all onto the connected device.
     """
     logger.info('Flashing script')
     # Grab the Python script.
     tab = self._view.current_tab
     if tab is None:
         # There is no active text editor.
         return
     self.save()  # save current script to disk
     logger.debug('Python script file:')
     logger.debug(tab.path)
     microfs.put(microfs.get_serial(), tab.path)
Beispiel #18
0
 def dropEvent(self, event):
     source = event.source()
     self.disable(source)
     if isinstance(source, LocalFileList):
         local_filename = os.path.join(self.home,
                                       source.currentItem().text())
         logger.info("Putting {}".format(local_filename))
         try:
             with microfs.get_serial() as serial:
                 logger.info(serial.port)
                 microfs.put(serial, local_filename)
             super().dropEvent(event)
         except Exception as ex:
             logger.error(ex)
     self.enable(source)
Beispiel #19
0
 def dropEvent(self, event):
     source = event.source()
     self.disable(source)
     if isinstance(source, LocalFileList):
         local_filename = os.path.join(self.home,
                                       source.currentItem().text())
         logger.info("Putting {}".format(local_filename))
         try:
             with microfs.get_serial() as serial:
                 logger.info(serial.port)
                 microfs.put(serial, local_filename)
             super().dropEvent(event)
         except Exception as ex:
             logger.error(ex)
     self.enable(source)
Beispiel #20
0
    def ls(self):
        """
        Gets a list of the files on the micro:bit.

        Naive implementation for simplicity's sake.
        """
        self.microbit_fs.clear()
        self.local_fs.clear()
        microbit_files = microfs.ls(microfs.get_serial())
        for f in microbit_files:
            self.microbit_fs.addItem(f)
        local_files = [f for f in os.listdir(self.home)
                       if os.path.isfile(os.path.join(self.home, f))]
        local_files.sort()
        for f in local_files:
            self.local_fs.addItem(f)
Beispiel #21
0
 def dropEvent(self, event):
     source = event.source()
     self.disable(source)
     if isinstance(source, MicrobitFileList):
         microbit_filename = source.currentItem().text()
         local_filename = os.path.join(self.home, microbit_filename)
         logger.debug("Getting {} to {}".format(microbit_filename,
                                                local_filename))
         try:
             with microfs.get_serial() as serial:
                 logger.info(serial.port)
                 microfs.get(serial, microbit_filename, local_filename)
             super().dropEvent(event)
         except Exception as ex:
             logger.error(ex)
     self.enable(source)
Beispiel #22
0
    def ls(self):
        """
        Gets a list of the files on the micro:bit.

        Naive implementation for simplicity's sake.
        """
        self.microbit_fs.clear()
        self.local_fs.clear()
        microbit_files = microfs.ls(microfs.get_serial())
        for f in microbit_files:
            self.microbit_fs.addItem(f)
        local_files = [f for f in os.listdir(self.home)
                       if os.path.isfile(os.path.join(self.home, f))]
        local_files.sort()
        for f in local_files:
            self.local_fs.addItem(f)
Beispiel #23
0
 def dropEvent(self, event):
     source = event.source()
     self.disable(source)
     if isinstance(source, MicrobitFileList):
         microbit_filename = source.currentItem().text()
         local_filename = os.path.join(self.home,
                                       microbit_filename)
         logger.debug("Getting {} to {}".format(microbit_filename,
                                                local_filename))
         try:
             with microfs.get_serial() as serial:
                 logger.info(serial.port)
                 microfs.get(serial, microbit_filename, local_filename)
             super().dropEvent(event)
         except Exception as ex:
             logger.error(ex)
     self.enable(source)
Beispiel #24
0
 def dropEvent(self, event):
     source = event.source()
     self.disable(source)
     if isinstance(source, LocalFileList):
         if self.findItems(source.currentItem().text(), Qt.MatchExactly) \
                  and self.show_confirm_overwrite_dialog():
             local_filename = os.path.join(self.home,
                                           source.currentItem().text())
             logger.info("Putting {}".format(local_filename))
             try:
                 with microfs.get_serial() as serial:
                     logger.info(serial.port)
                     microfs.put(serial, local_filename)
                 super().dropEvent(event)
             except Exception as ex:
                 logger.error(ex)
     self.enable(source)
     self.parent().ls()
Beispiel #25
0
 def contextMenuEvent(self, event):
     menu = QMenu(self)
     delete_action = menu.addAction("Delete (cannot be undone)")
     action = menu.exec_(self.mapToGlobal(event.pos()))
     if action == delete_action:
         self.setDisabled(True)
         self.setAcceptDrops(False)
         microbit_filename = self.currentItem().text()
         logger.info("Deleting {}".format(microbit_filename))
         try:
             with microfs.get_serial() as serial:
                 logger.info(serial.port)
                 microfs.rm(serial, microbit_filename)
             self.takeItem(self.currentRow())
         except Exception as ex:
             logger.error(ex)
         self.setDisabled(False)
         self.setAcceptDrops(True)
Beispiel #26
0
 def dropEvent(self, event):
     source = event.source()
     self.disable(source)
     if isinstance(source, LocalFileList):
         if self.findItems(source.currentItem().text(), Qt.MatchExactly) \
                  and self.show_confirm_overwrite_dialog():
             local_filename = os.path.join(self.home,
                                           source.currentItem().text())
             logger.info("Putting {}".format(local_filename))
             try:
                 with microfs.get_serial() as serial:
                     logger.info(serial.port)
                     microfs.put(serial, local_filename)
                 super().dropEvent(event)
             except Exception as ex:
                 logger.error(ex)
     self.enable(source)
     self.parent().ls()
Beispiel #27
0
 def contextMenuEvent(self, event):
     menu = QMenu(self)
     delete_action = menu.addAction("Delete (cannot be undone)")
     action = menu.exec_(self.mapToGlobal(event.pos()))
     if action == delete_action:
         self.setDisabled(True)
         self.setAcceptDrops(False)
         microbit_filename = self.currentItem().text()
         logger.info("Deleting {}".format(microbit_filename))
         try:
             with microfs.get_serial() as serial:
                 logger.info(serial.port)
                 microfs.rm(serial, microbit_filename)
             self.takeItem(self.currentRow())
         except Exception as ex:
             logger.error(ex)
         self.setDisabled(False)
         self.setAcceptDrops(True)
Beispiel #28
0
 def dropEvent(self, event):
     source = event.source()
     self.disable(source)
     if isinstance(source, MicrobitFileList):
         file_exists = self.findItems(source.currentItem().text(),
                                      Qt.MatchExactly)
         if not file_exists or \
                 file_exists and self.show_confirm_overwrite_dialog():
             microbit_filename = source.currentItem().text()
             local_filename = os.path.join(self.home,
                                           microbit_filename)
             logger.debug("Getting {} to {}".format(microbit_filename,
                                                    local_filename))
             try:
                 with microfs.get_serial() as serial:
                     logger.info(serial.port)
                     microfs.get(serial, microbit_filename, local_filename)
                 super().dropEvent(event)
             except Exception as ex:
                 logger.error(ex)
     self.enable(source)
     if self.parent() is not None:
         self.parent().ls()
Beispiel #29
0
 def copy_main(self, script):
     """
     If script argument contains any code, copy it onto the
     connected micro:bit as main.py, then restart the board (CTRL-D).
     """
     if script.strip():
         logger.info("Copying main.py onto device")
         commands = ["fd = open('main.py', 'wb')", "f = fd.write"]
         while script:
             line = script[:64]
             commands.append("f(" + repr(line) + ")")
             script = script[64:]
         commands.append("fd.close()")
         logger.info(commands)
         serial = microfs.get_serial()
         out, err = microfs.execute(commands, serial)
         logger.info((out, err))
         if err:
             raise IOError(microfs.clean_error(err))
         # Reset the device.
         serial.write(b"import microbit\r\n")
         serial.write(b"microbit.reset()\r\n")
         self.editor.show_status_message(_("Copied code onto micro:bit."))