eric6/MicroPython/MicroPythonCommandsInterface.py

changeset 7836
2f0d208b8137
parent 7780
41420f82c0ac
child 7923
91e843545d9a
equal deleted inserted replaced
7835:0835ed67714b 7836:2f0d208b8137
287 287
288 @param dirname name of the directory to be listed 288 @param dirname name of the directory to be listed
289 @type str 289 @type str
290 @return tuple containg the directory listing 290 @return tuple containg the directory listing
291 @rtype tuple of str 291 @rtype tuple of str
292 @exception IOError raised to indicate an issue with the device 292 @exception OSError raised to indicate an issue with the device
293 """ 293 """
294 if self.__repl.isMicrobit(): 294 if self.__repl.isMicrobit():
295 # BBC micro:bit does not support directories 295 # BBC micro:bit does not support directories
296 commands = [ 296 commands = [
297 "import os as __os_", 297 "import os as __os_",
304 "print(__os_.listdir('{0}'))".format(dirname), 304 "print(__os_.listdir('{0}'))".format(dirname),
305 "del __os_", 305 "del __os_",
306 ] 306 ]
307 out, err = self.execute(commands) 307 out, err = self.execute(commands)
308 if err: 308 if err:
309 raise IOError(self.__shortError(err)) 309 raise OSError(self.__shortError(err))
310 return ast.literal_eval(out.decode("utf-8")) 310 return ast.literal_eval(out.decode("utf-8"))
311 311
312 def lls(self, dirname="", fullstat=False, showHidden=False): 312 def lls(self, dirname="", fullstat=False, showHidden=False):
313 """ 313 """
314 Public method to get a long directory listing of the connected device 314 Public method to get a long directory listing of the connected device
323 @return list containing the directory listing with tuple entries of 323 @return list containing the directory listing with tuple entries of
324 the name and and a tuple of mode, size and time (if fullstat is 324 the name and and a tuple of mode, size and time (if fullstat is
325 false) or the complete stat() tuple. 'None' is returned in case the 325 false) or the complete stat() tuple. 'None' is returned in case the
326 directory doesn't exist. 326 directory doesn't exist.
327 @rtype tuple of (str, tuple) 327 @rtype tuple of (str, tuple)
328 @exception IOError raised to indicate an issue with the device 328 @exception OSError raised to indicate an issue with the device
329 """ 329 """
330 if self.__repl.isMicrobit(): 330 if self.__repl.isMicrobit():
331 # BBC micro:bit does not support directories 331 # BBC micro:bit does not support directories
332 commands = [ 332 commands = [
333 "import os as __os_", 333 "import os as __os_",
381 "print(listdir_stat('{0}', {1}))".format(dirname, showHidden), 381 "print(listdir_stat('{0}', {1}))".format(dirname, showHidden),
382 "del __os_, stat, listdir_stat, is_visible", 382 "del __os_, stat, listdir_stat, is_visible",
383 ] 383 ]
384 out, err = self.execute(commands) 384 out, err = self.execute(commands)
385 if err: 385 if err:
386 raise IOError(self.__shortError(err)) 386 raise OSError(self.__shortError(err))
387 fileslist = ast.literal_eval(out.decode("utf-8")) 387 fileslist = ast.literal_eval(out.decode("utf-8"))
388 if fileslist is None: 388 if fileslist is None:
389 return None 389 return None
390 else: 390 else:
391 if fullstat: 391 if fullstat:
397 """ 397 """
398 Public method to change the current directory on the connected device. 398 Public method to change the current directory on the connected device.
399 399
400 @param dirname directory to change to 400 @param dirname directory to change to
401 @type str 401 @type str
402 @exception IOError raised to indicate an issue with the device 402 @exception OSError raised to indicate an issue with the device
403 """ 403 """
404 if dirname: 404 if dirname:
405 commands = [ 405 commands = [
406 "import os as __os_", 406 "import os as __os_",
407 "__os_.chdir('{0}')".format(dirname), 407 "__os_.chdir('{0}')".format(dirname),
408 "del __os_", 408 "del __os_",
409 ] 409 ]
410 out, err = self.execute(commands) 410 out, err = self.execute(commands)
411 if err: 411 if err:
412 raise IOError(self.__shortError(err)) 412 raise OSError(self.__shortError(err))
413 413
414 def pwd(self): 414 def pwd(self):
415 """ 415 """
416 Public method to get the current directory of the connected device. 416 Public method to get the current directory of the connected device.
417 417
418 @return current directory 418 @return current directory
419 @rtype str 419 @rtype str
420 @exception IOError raised to indicate an issue with the device 420 @exception OSError raised to indicate an issue with the device
421 """ 421 """
422 if self.__repl.isMicrobit(): 422 if self.__repl.isMicrobit():
423 # BBC micro:bit does not support directories 423 # BBC micro:bit does not support directories
424 return "" 424 return ""
425 425
428 "print(__os_.getcwd())", 428 "print(__os_.getcwd())",
429 "del __os_", 429 "del __os_",
430 ] 430 ]
431 out, err = self.execute(commands) 431 out, err = self.execute(commands)
432 if err: 432 if err:
433 raise IOError(self.__shortError(err)) 433 raise OSError(self.__shortError(err))
434 return out.decode("utf-8").strip() 434 return out.decode("utf-8").strip()
435 435
436 def rm(self, filename): 436 def rm(self, filename):
437 """ 437 """
438 Public method to remove a file from the connected device. 438 Public method to remove a file from the connected device.
439 439
440 @param filename name of the file to be removed 440 @param filename name of the file to be removed
441 @type str 441 @type str
442 @exception IOError raised to indicate an issue with the device 442 @exception OSError raised to indicate an issue with the device
443 """ 443 """
444 if filename: 444 if filename:
445 commands = [ 445 commands = [
446 "import os as __os_", 446 "import os as __os_",
447 "__os_.remove('{0}')".format(filename), 447 "__os_.remove('{0}')".format(filename),
448 "del __os_", 448 "del __os_",
449 ] 449 ]
450 out, err = self.execute(commands) 450 out, err = self.execute(commands)
451 if err: 451 if err:
452 raise IOError(self.__shortError(err)) 452 raise OSError(self.__shortError(err))
453 453
454 def rmrf(self, name, recursive=False, force=False): 454 def rmrf(self, name, recursive=False, force=False):
455 """ 455 """
456 Public method to remove a file or directory recursively. 456 Public method to remove a file or directory recursively.
457 457
461 @type bool 461 @type bool
462 @param force flag indicating to ignore errors 462 @param force flag indicating to ignore errors
463 @type bool 463 @type bool
464 @return flag indicating success 464 @return flag indicating success
465 @rtype bool 465 @rtype bool
466 @exception IOError raised to indicate an issue with the device 466 @exception OSError raised to indicate an issue with the device
467 """ 467 """
468 if name: 468 if name:
469 commands = [ 469 commands = [
470 "import os as __os_", 470 "import os as __os_",
471 "\n".join([ 471 "\n".join([
494 force), 494 force),
495 "del __os_, remove_file", 495 "del __os_, remove_file",
496 ] 496 ]
497 out, err = self.execute(commands) 497 out, err = self.execute(commands)
498 if err: 498 if err:
499 raise IOError(self.__shortError(err)) 499 raise OSError(self.__shortError(err))
500 return ast.literal_eval(out.decode("utf-8")) 500 return ast.literal_eval(out.decode("utf-8"))
501 501
502 return False 502 return False
503 503
504 def mkdir(self, dirname): 504 def mkdir(self, dirname):
505 """ 505 """
506 Public method to create a new directory. 506 Public method to create a new directory.
507 507
508 @param dirname name of the directory to create 508 @param dirname name of the directory to create
509 @type str 509 @type str
510 @exception IOError raised to indicate an issue with the device 510 @exception OSError raised to indicate an issue with the device
511 """ 511 """
512 if dirname: 512 if dirname:
513 commands = [ 513 commands = [
514 "import os as __os_", 514 "import os as __os_",
515 "__os_.mkdir('{0}')".format(dirname), 515 "__os_.mkdir('{0}')".format(dirname),
516 "del __os_", 516 "del __os_",
517 ] 517 ]
518 out, err = self.execute(commands) 518 out, err = self.execute(commands)
519 if err: 519 if err:
520 raise IOError(self.__shortError(err)) 520 raise OSError(self.__shortError(err))
521 521
522 def rmdir(self, dirname): 522 def rmdir(self, dirname):
523 """ 523 """
524 Public method to remove a directory. 524 Public method to remove a directory.
525 525
526 @param dirname name of the directory to be removed 526 @param dirname name of the directory to be removed
527 @type str 527 @type str
528 @exception IOError raised to indicate an issue with the device 528 @exception OSError raised to indicate an issue with the device
529 """ 529 """
530 if dirname: 530 if dirname:
531 commands = [ 531 commands = [
532 "import os as __os_", 532 "import os as __os_",
533 "__os_.rmdir('{0}')".format(dirname), 533 "__os_.rmdir('{0}')".format(dirname),
534 "del __os_", 534 "del __os_",
535 ] 535 ]
536 out, err = self.execute(commands) 536 out, err = self.execute(commands)
537 if err: 537 if err:
538 raise IOError(self.__shortError(err)) 538 raise OSError(self.__shortError(err))
539 539
540 def put(self, hostFileName, deviceFileName=None): 540 def put(self, hostFileName, deviceFileName=None):
541 """ 541 """
542 Public method to copy a local file to the connected device. 542 Public method to copy a local file to the connected device.
543 543
545 @type str 545 @type str
546 @param deviceFileName name of the file to copy to 546 @param deviceFileName name of the file to copy to
547 @type str 547 @type str
548 @return flag indicating success 548 @return flag indicating success
549 @rtype bool 549 @rtype bool
550 @exception IOError raised to indicate an issue with the device 550 @exception OSError raised to indicate an issue with the device
551 """ 551 """
552 if not os.path.isfile(hostFileName): 552 if not os.path.isfile(hostFileName):
553 raise IOError("No such file: {0}".format(hostFileName)) 553 raise OSError("No such file: {0}".format(hostFileName))
554 554
555 with open(hostFileName, "rb") as hostFile: 555 with open(hostFileName, "rb") as hostFile:
556 content = hostFile.read() 556 content = hostFile.read()
557 # convert eol '\r' 557 # convert eol '\r'
558 content = content.replace(b"\r\n", b"\r") 558 content = content.replace(b"\r\n", b"\r")
574 "del f, fd", 574 "del f, fd",
575 ]) 575 ])
576 576
577 out, err = self.execute(commands) 577 out, err = self.execute(commands)
578 if err: 578 if err:
579 raise IOError(self.__shortError(err)) 579 raise OSError(self.__shortError(err))
580 return True 580 return True
581 581
582 def get(self, deviceFileName, hostFileName=None): 582 def get(self, deviceFileName, hostFileName=None):
583 """ 583 """
584 Public method to copy a file from the connected device. 584 Public method to copy a file from the connected device.
587 @type str 587 @type str
588 @param hostFileName name of the file to copy to 588 @param hostFileName name of the file to copy to
589 @type str 589 @type str
590 @return flag indicating success 590 @return flag indicating success
591 @rtype bool 591 @rtype bool
592 @exception IOError raised to indicate an issue with the device 592 @exception OSError raised to indicate an issue with the device
593 """ 593 """
594 if not hostFileName: 594 if not hostFileName:
595 hostFileName = deviceFileName 595 hostFileName = deviceFileName
596 596
597 commands = [ 597 commands = [
620 ]), 620 ]),
621 "send_data()", 621 "send_data()",
622 ] 622 ]
623 out, err = self.execute(commands) 623 out, err = self.execute(commands)
624 if err: 624 if err:
625 raise IOError(self.__shortError(err)) 625 raise OSError(self.__shortError(err))
626 626
627 # write the received bytes to the local file 627 # write the received bytes to the local file
628 # convert eol to "\n" 628 # convert eol to "\n"
629 out = out.replace(b"\r\n", b"\n") 629 out = out.replace(b"\r\n", b"\n")
630 out = out.replace(b"\r", b"\n") 630 out = out.replace(b"\r", b"\n")
638 systems. 638 systems.
639 639
640 @return tuple of tuples containing the file system name, the total 640 @return tuple of tuples containing the file system name, the total
641 size, the used size and the free size 641 size, the used size and the free size
642 @rtype tuple of tuples of (str, int, int, int) 642 @rtype tuple of tuples of (str, int, int, int)
643 @exception IOError raised to indicate an issue with the device 643 @exception OSError raised to indicate an issue with the device
644 """ 644 """
645 commands = [ 645 commands = [
646 "import os as __os_", 646 "import os as __os_",
647 "\n".join([ 647 "\n".join([
648 "def fsinfo():", 648 "def fsinfo():",
661 "print(fsinfo())", 661 "print(fsinfo())",
662 "del __os_, fsinfo", 662 "del __os_, fsinfo",
663 ] 663 ]
664 out, err = self.execute(commands) 664 out, err = self.execute(commands)
665 if err: 665 if err:
666 raise IOError(self.__shortError(err)) 666 raise OSError(self.__shortError(err))
667 infolist = ast.literal_eval(out.decode("utf-8")) 667 infolist = ast.literal_eval(out.decode("utf-8"))
668 if infolist is None: 668 if infolist is None:
669 return None 669 return None
670 else: 670 else:
671 filesystemInfos = [] 671 filesystemInfos = []
686 Public method to get the MicroPython version information of the 686 Public method to get the MicroPython version information of the
687 connected device. 687 connected device.
688 688
689 @return dictionary containing the version information 689 @return dictionary containing the version information
690 @rtype dict 690 @rtype dict
691 @exception IOError raised to indicate an issue with the device 691 @exception OSError raised to indicate an issue with the device
692 """ 692 """
693 commands = [ 693 commands = [
694 "import os as __os_", 694 "import os as __os_",
695 "print(__os_.uname())", 695 "print(__os_.uname())",
696 "del __os_", 696 "del __os_",
697 ] 697 ]
698 out, err = self.execute(commands) 698 out, err = self.execute(commands)
699 if err: 699 if err:
700 raise IOError(self.__shortError(err)) 700 raise OSError(self.__shortError(err))
701 701
702 rawOutput = out.decode("utf-8").strip() 702 rawOutput = out.decode("utf-8").strip()
703 rawOutput = rawOutput[1:-1] 703 rawOutput = rawOutput[1:-1]
704 items = rawOutput.split(",") 704 items = rawOutput.split(",")
705 result = {} 705 result = {}
713 Public method to get some implementation information of the connected 713 Public method to get some implementation information of the connected
714 device. 714 device.
715 715
716 @return dictionary containing the implementation information 716 @return dictionary containing the implementation information
717 @rtype dict 717 @rtype dict
718 @exception IOError raised to indicate an issue with the device 718 @exception OSError raised to indicate an issue with the device
719 """ 719 """
720 commands = [ 720 commands = [
721 "import sys as __sys_", 721 "import sys as __sys_",
722 "res = {}", # __IGNORE_WARNING_M613__ 722 "res = {}", # __IGNORE_WARNING_M613__
723 "\n".join([ 723 "\n".join([
736 "print(res)", 736 "print(res)",
737 "del res, __sys_", 737 "del res, __sys_",
738 ] 738 ]
739 out, err = self.execute(commands) 739 out, err = self.execute(commands)
740 if err: 740 if err:
741 raise IOError(self.__shortError(err)) 741 raise OSError(self.__shortError(err))
742 return ast.literal_eval(out.decode("utf-8")) 742 return ast.literal_eval(out.decode("utf-8"))
743 743
744 def syncTime(self): 744 def syncTime(self):
745 """ 745 """
746 Public method to set the time of the connected device to the local 746 Public method to set the time of the connected device to the local
747 computer's time. 747 computer's time.
748 748
749 @exception IOError raised to indicate an issue with the device 749 @exception OSError raised to indicate an issue with the device
750 """ 750 """
751 now = time.localtime(time.time()) 751 now = time.localtime(time.time())
752 commands = [ 752 commands = [
753 "\n".join([ 753 "\n".join([
754 "def set_time(rtc_time):", 754 "def set_time(rtc_time):",
793 now.tm_wday)), 793 now.tm_wday)),
794 "del set_time", 794 "del set_time",
795 ] 795 ]
796 out, err = self.execute(commands) 796 out, err = self.execute(commands)
797 if err: 797 if err:
798 raise IOError(self.__shortError(err)) 798 raise OSError(self.__shortError(err))
799 799
800 def getTime(self): 800 def getTime(self):
801 """ 801 """
802 Public method to get the current time of the device. 802 Public method to get the current time of the device.
803 803
804 @return time of the device 804 @return time of the device
805 @rtype str 805 @rtype str
806 @exception IOError raised to indicate an issue with the device 806 @exception OSError raised to indicate an issue with the device
807 """ 807 """
808 commands = [ 808 commands = [
809 "import time as __time_", 809 "import time as __time_",
810 "\n".join([ 810 "\n".join([
811 "try:", 811 "try:",
820 ]), 820 ]),
821 "del __time_" 821 "del __time_"
822 ] 822 ]
823 out, err = self.execute(commands) 823 out, err = self.execute(commands)
824 if err: 824 if err:
825 raise IOError(self.__shortError(err)) 825 raise OSError(self.__shortError(err))
826 return out.decode("utf-8").strip() 826 return out.decode("utf-8").strip()

eric ide

mercurial