319 return decodedError.split["\r\n"][-2] |
319 return decodedError.split["\r\n"][-2] |
320 except Exception: |
320 except Exception: |
321 return decodedError |
321 return decodedError |
322 return self.tr("Detected an error without indications.") |
322 return self.tr("Detected an error without indications.") |
323 |
323 |
|
324 # TODO: move these methods to the devices. |
324 ################################################################## |
325 ################################################################## |
325 ## Methods below implement the file system commands |
326 ## Methods below implement the file system commands |
326 ################################################################## |
327 ################################################################## |
327 |
328 |
328 def ls(self, dirname=""): |
329 def ls(self, dirname=""): |
609 @exception OSError raised to indicate an issue with the device |
610 @exception OSError raised to indicate an issue with the device |
610 """ |
611 """ |
611 if not os.path.isfile(hostFileName): |
612 if not os.path.isfile(hostFileName): |
612 raise OSError("No such file: {0}".format(hostFileName)) |
613 raise OSError("No such file: {0}".format(hostFileName)) |
613 |
614 |
|
615 if not deviceFileName: |
|
616 deviceFileName = os.path.basename(hostFileName) |
|
617 |
614 with open(hostFileName, "rb") as hostFile: |
618 with open(hostFileName, "rb") as hostFile: |
615 content = hostFile.read() |
619 content = hostFile.read() |
616 # convert eol '\r' |
620 |
617 content = content.replace(b"\r\n", b"\r") |
621 return self.putData(deviceFileName, content) |
618 content = content.replace(b"\n", b"\r") |
622 |
619 |
623 def putData(self, deviceFileName, content): |
|
624 """ |
|
625 Public method to write the given data to the connected device. |
|
626 |
|
627 @param deviceFileName name of the file to write to |
|
628 @type str |
|
629 @param content data to write |
|
630 @type bytes |
|
631 @return flag indicating success |
|
632 @rtype bool |
|
633 @exception OSError raised to indicate an issue with the device |
|
634 """ |
620 if not deviceFileName: |
635 if not deviceFileName: |
621 deviceFileName = os.path.basename(hostFileName) |
636 raise OSError("Missing device file name") |
|
637 |
|
638 # convert eol '\r' |
|
639 content = content.replace(b"\r\n", b"\r") |
|
640 content = content.replace(b"\n", b"\r") |
622 |
641 |
623 commands = [ |
642 commands = [ |
624 "fd = open('{0}', 'wb')".format(deviceFileName), |
643 "fd = open('{0}', 'wb')".format(deviceFileName), |
625 "f = fd.write", |
644 "f = fd.write", |
626 ] |
645 ] |
650 @type str |
669 @type str |
651 @return flag indicating success |
670 @return flag indicating success |
652 @rtype bool |
671 @rtype bool |
653 @exception OSError raised to indicate an issue with the device |
672 @exception OSError raised to indicate an issue with the device |
654 """ |
673 """ |
|
674 if not deviceFileName: |
|
675 raise OSError("Missing device file name") |
|
676 |
655 if not hostFileName: |
677 if not hostFileName: |
656 hostFileName = deviceFileName |
678 hostFileName = deviceFileName |
|
679 |
|
680 out = self.getData(deviceFileName) |
|
681 with open(hostFileName, "wb") as hostFile: |
|
682 hostFile.write(out) |
|
683 |
|
684 return True |
|
685 |
|
686 def getData(self, deviceFileName): |
|
687 """ |
|
688 Public method to read data from the connected device. |
|
689 |
|
690 @param deviceFileName name of the file to read from |
|
691 @type str |
|
692 @return data read from the device |
|
693 @rtype bytes |
|
694 @exception OSError raised to indicate an issue with the device |
|
695 """ |
|
696 if not deviceFileName: |
|
697 raise OSError("Missing device file name") |
657 |
698 |
658 commands = [ |
699 commands = [ |
659 "\n".join( |
700 "\n".join( |
660 [ |
701 [ |
661 "def send_data():", |
702 "def send_data():", |
662 " try:", |
703 " try:", |
663 " from microbit import uart as u", |
704 " from microbit import uart as u", |
664 " except ImportError:", |
705 " except ImportError:", |
665 " try:", |
706 " try:", |
666 " from machine import UART", |
707 " from sys import stdout as u", |
667 " u = UART(0, {0})".format(115200), |
708 " except ImportError:", |
668 " except Exception:", |
|
669 " try:", |
709 " try:", |
670 " from sys import stdout as u", |
710 " from machine import UART", |
|
711 " u = UART(0, {0})".format(115200), |
671 " except Exception:", |
712 " except Exception:", |
672 " raise Exception('Could not find UART module" |
713 " raise Exception('Could not find UART module" |
673 " in device.')", |
714 " in device.')", |
674 " f = open('{0}', 'rb')".format(deviceFileName), |
715 " f = open('{0}', 'rb')".format(deviceFileName), |
675 " r = f.read", |
716 " r = f.read", |
689 |
730 |
690 # write the received bytes to the local file |
731 # write the received bytes to the local file |
691 # convert eol to "\n" |
732 # convert eol to "\n" |
692 out = out.replace(b"\r\n", b"\n") |
733 out = out.replace(b"\r\n", b"\n") |
693 out = out.replace(b"\r", b"\n") |
734 out = out.replace(b"\r", b"\n") |
694 with open(hostFileName, "wb") as hostFile: |
735 |
695 hostFile.write(out) |
736 return out |
696 return True |
|
697 |
737 |
698 def fileSystemInfo(self): |
738 def fileSystemInfo(self): |
699 """ |
739 """ |
700 Public method to obtain information about the currently mounted file |
740 Public method to obtain information about the currently mounted file |
701 systems. |
741 systems. |