eric6/MicroPython/MicroPythonCommandsInterface.py

changeset 7628
f904d0eef264
parent 7360
9190402e4505
child 7780
41420f82c0ac
equal deleted inserted replaced
7626:7f643d41464e 7628:f904d0eef264
400 400
401 @param dirname directory to change to 401 @param dirname directory to change to
402 @type str 402 @type str
403 @exception IOError raised to indicate an issue with the device 403 @exception IOError raised to indicate an issue with the device
404 """ 404 """
405 assert dirname 405 if dirname:
406 406 commands = [
407 commands = [ 407 "import os as __os_",
408 "import os as __os_", 408 "__os_.chdir('{0}')".format(dirname),
409 "__os_.chdir('{0}')".format(dirname), 409 "del __os_",
410 "del __os_", 410 ]
411 ] 411 out, err = self.execute(commands)
412 out, err = self.execute(commands) 412 if err:
413 if err: 413 raise IOError(self.__shortError(err))
414 raise IOError(self.__shortError(err))
415 414
416 def pwd(self): 415 def pwd(self):
417 """ 416 """
418 Public method to get the current directory of the connected device. 417 Public method to get the current directory of the connected device.
419 418
441 440
442 @param filename name of the file to be removed 441 @param filename name of the file to be removed
443 @type str 442 @type str
444 @exception IOError raised to indicate an issue with the device 443 @exception IOError raised to indicate an issue with the device
445 """ 444 """
446 assert filename 445 if filename:
447 446 commands = [
448 commands = [ 447 "import os as __os_",
449 "import os as __os_", 448 "__os_.remove('{0}')".format(filename),
450 "__os_.remove('{0}')".format(filename), 449 "del __os_",
451 "del __os_", 450 ]
452 ] 451 out, err = self.execute(commands)
453 out, err = self.execute(commands) 452 if err:
454 if err: 453 raise IOError(self.__shortError(err))
455 raise IOError(self.__shortError(err))
456 454
457 def rmrf(self, name, recursive=False, force=False): 455 def rmrf(self, name, recursive=False, force=False):
458 """ 456 """
459 Public method to remove a file or directory recursively. 457 Public method to remove a file or directory recursively.
460 458
466 @type bool 464 @type bool
467 @return flag indicating success 465 @return flag indicating success
468 @rtype bool 466 @rtype bool
469 @exception IOError raised to indicate an issue with the device 467 @exception IOError raised to indicate an issue with the device
470 """ 468 """
471 assert name 469 if name:
472 470 commands = [
473 commands = [ 471 "import os as __os_",
474 "import os as __os_", 472 "\n".join([
475 "\n".join([ 473 "def remove_file(name, recursive=False, force=False):",
476 "def remove_file(name, recursive=False, force=False):", 474 " try:",
477 " try:", 475 " mode = __os_.stat(name)[0]",
478 " mode = __os_.stat(name)[0]", 476 " if mode & 0x4000 != 0:",
479 " if mode & 0x4000 != 0:", 477 " if recursive:",
480 " if recursive:", 478 " for file in __os_.listdir(name):",
481 " for file in __os_.listdir(name):", 479 " success = remove_file("
482 " success = remove_file(name + '/' + file," 480 "name + '/' + file, recursive, force)",
483 " recursive, force)", 481 " if not success and not force:",
484 " if not success and not force:", 482 " return False",
485 " return False", 483 " __os_.rmdir(name)",
486 " __os_.rmdir(name)", 484 " else:",
487 " else:", 485 " if not force:",
488 " if not force:", 486 " return False",
489 " return False", 487 " else:",
490 " else:", 488 " __os_.remove(name)",
491 " __os_.remove(name)", 489 " except:",
492 " except:", 490 " if not force:",
493 " if not force:", 491 " return False",
494 " return False", 492 " return True",
495 " return True", 493 ]),
496 ]), 494 "print(remove_file('{0}', {1}, {2}))".format(name, recursive,
497 "print(remove_file('{0}', {1}, {2}))".format(name, recursive, 495 force),
498 force), 496 "del __os_, remove_file",
499 "del __os_, remove_file", 497 ]
500 ] 498 out, err = self.execute(commands)
501 out, err = self.execute(commands) 499 if err:
502 if err: 500 raise IOError(self.__shortError(err))
503 raise IOError(self.__shortError(err)) 501 return ast.literal_eval(out.decode("utf-8"))
504 return ast.literal_eval(out.decode("utf-8")) 502
503 return False
505 504
506 def mkdir(self, dirname): 505 def mkdir(self, dirname):
507 """ 506 """
508 Public method to create a new directory. 507 Public method to create a new directory.
509 508
510 @param dirname name of the directory to create 509 @param dirname name of the directory to create
511 @type str 510 @type str
512 @exception IOError raised to indicate an issue with the device 511 @exception IOError raised to indicate an issue with the device
513 """ 512 """
514 assert dirname 513 if dirname:
515 514 commands = [
516 commands = [ 515 "import os as __os_",
517 "import os as __os_", 516 "__os_.mkdir('{0}')".format(dirname),
518 "__os_.mkdir('{0}')".format(dirname), 517 "del __os_",
519 "del __os_", 518 ]
520 ] 519 out, err = self.execute(commands)
521 out, err = self.execute(commands) 520 if err:
522 if err: 521 raise IOError(self.__shortError(err))
523 raise IOError(self.__shortError(err))
524 522
525 def rmdir(self, dirname): 523 def rmdir(self, dirname):
526 """ 524 """
527 Public method to remove a directory. 525 Public method to remove a directory.
528 526
529 @param dirname name of the directory to be removed 527 @param dirname name of the directory to be removed
530 @type str 528 @type str
531 @exception IOError raised to indicate an issue with the device 529 @exception IOError raised to indicate an issue with the device
532 """ 530 """
533 assert dirname 531 if dirname:
534 532 commands = [
535 commands = [ 533 "import os as __os_",
536 "import os as __os_", 534 "__os_.rmdir('{0}')".format(dirname),
537 "__os_.rmdir('{0}')".format(dirname), 535 "del __os_",
538 "del __os_", 536 ]
539 ] 537 out, err = self.execute(commands)
540 out, err = self.execute(commands) 538 if err:
541 if err: 539 raise IOError(self.__shortError(err))
542 raise IOError(self.__shortError(err))
543 540
544 def put(self, hostFileName, deviceFileName=None): 541 def put(self, hostFileName, deviceFileName=None):
545 """ 542 """
546 Public method to copy a local file to the connected device. 543 Public method to copy a local file to the connected device.
547 544

eric ide

mercurial