src/eric7/MicroPython/Devices/DeviceBase.py

branch
mpy_network
changeset 9827
21803aa6c3e2
parent 9816
4aeaf6df7283
child 9829
cafb132fe3bb
equal deleted inserted replaced
9826:9340ce7fb12f 9827:21803aa6c3e2
93 self._deviceType = deviceType 93 self._deviceType = deviceType
94 self._interface = microPythonWidget.deviceInterface() 94 self._interface = microPythonWidget.deviceInterface()
95 self.microPython = microPythonWidget 95 self.microPython = microPythonWidget
96 self._deviceData = {} # dictionary with essential device data 96 self._deviceData = {} # dictionary with essential device data
97 97
98 self.submitMode = "raw" # default is 'raw' mode to submit commands 98 self._submitMode = "raw" # default is 'raw' mode to submit commands
99 99
100 def setConnected(self, connected): 100 def setConnected(self, connected):
101 """ 101 """
102 Public method to set the connection state. 102 Public method to set the connection state.
103 103
177 return ( 177 return (
178 self.checkDeviceData() 178 self.checkDeviceData()
179 and self._deviceData["mpy_name"].lower() == "circuitpython" 179 and self._deviceData["mpy_name"].lower() == "circuitpython"
180 ) 180 )
181 181
182 def submitMode(self):
183 """
184 Public method to get the submit mode of the device.
185
186 @return submit mode
187 @rtype str (one of 'raw', 'paste')
188 """
189 return self._submitMode
190
182 def setButtons(self): 191 def setButtons(self):
183 """ 192 """
184 Public method to enable the supported action buttons. 193 Public method to enable the supported action buttons.
185 """ 194 """
186 self.microPython.setActionButtons( 195 self.microPython.setActionButtons(
333 Public method to send a list of commands to the device. 342 Public method to send a list of commands to the device.
334 343
335 @param commandsList list of commands to be sent to the device 344 @param commandsList list of commands to be sent to the device
336 @type list of str 345 @type list of str
337 """ 346 """
338 if self.submitMode == "raw": 347 if self._submitMode == "raw":
339 rawOn = [ # sequence of commands to enter raw mode 348 rawOn = [ # sequence of commands to enter raw mode
340 b"\x02", # Ctrl-B: exit raw repl (just in case) 349 b"\x02", # Ctrl-B: exit raw repl (just in case)
341 b"\r\x03\x03\x03", # Ctrl-C three times: interrupt any running program 350 b"\r\x03\x03\x03", # Ctrl-C three times: interrupt any running program
342 b"\r\x01", # Ctrl-A: enter raw REPL 351 b"\r\x01", # Ctrl-A: enter raw REPL
343 ] 352 ]
348 commands.append(b"\r") 357 commands.append(b"\r")
349 commands.append(b"\x04") 358 commands.append(b"\x04")
350 rawOff = [b"\x02", b"\x02"] 359 rawOff = [b"\x02", b"\x02"]
351 commandSequence = rawOn + newLine + commands + rawOff 360 commandSequence = rawOn + newLine + commands + rawOff
352 self._interface.executeAsync(commandSequence) 361 self._interface.executeAsync(commandSequence)
353 elif self.submitMode == "paste": 362 elif self._submitMode == "paste":
354 commands = b"\n".join([c.encode("utf-8)") for c in commandsList]) 363 commands = b"\n".join([c.encode("utf-8)") for c in commandsList])
355 commandSequence = ["@PasteOn@", commands] 364 commandSequence = ["@PasteOn@", commands]
356 self._interface.executeAsyncPaste(commandSequence) 365 self._interface.executeAsyncPaste(commandSequence)
357 366
358 @pyqtSlot() 367 @pyqtSlot()
484 print(__os_.listdir('{0}')) 493 print(__os_.listdir('{0}'))
485 del __os_ 494 del __os_
486 """.format( 495 """.format(
487 dirname 496 dirname
488 ) 497 )
489 out, err = self._interface.execute(command, mode=self.submitMode) 498 out, err = self._interface.execute(command, mode=self._submitMode)
490 if err: 499 if err:
491 raise OSError(self._shortError(err)) 500 raise OSError(self._shortError(err))
492 return ast.literal_eval(out.decode("utf-8")) 501 return ast.literal_eval(out.decode("utf-8"))
493 502
494 def lls(self, dirname="", fullstat=False, showHidden=False): 503 def lls(self, dirname="", fullstat=False, showHidden=False):
536 print(listdir_stat('{0}', {1})) 545 print(listdir_stat('{0}', {1}))
537 del __os_, stat, listdir_stat, is_visible 546 del __os_, stat, listdir_stat, is_visible
538 """.format( 547 """.format(
539 dirname, showHidden 548 dirname, showHidden
540 ) 549 )
541 out, err = self._interface.execute(command, mode=self.submitMode) 550 out, err = self._interface.execute(command, mode=self._submitMode)
542 if err: 551 if err:
543 raise OSError(self._shortError(err)) 552 raise OSError(self._shortError(err))
544 fileslist = ast.literal_eval(out.decode("utf-8")) 553 fileslist = ast.literal_eval(out.decode("utf-8"))
545 if fileslist is None: 554 if fileslist is None:
546 return None 555 return None
564 __os_.chdir('{0}') 573 __os_.chdir('{0}')
565 del __os_ 574 del __os_
566 """.format( 575 """.format(
567 dirname 576 dirname
568 ) 577 )
569 out, err = self._interface.execute(command, mode=self.submitMode) 578 out, err = self._interface.execute(command, mode=self._submitMode)
570 if err: 579 if err:
571 raise OSError(self._shortError(err)) 580 raise OSError(self._shortError(err))
572 581
573 def pwd(self): 582 def pwd(self):
574 """ 583 """
581 command = """ 590 command = """
582 import os as __os_ 591 import os as __os_
583 print(__os_.getcwd()) 592 print(__os_.getcwd())
584 del __os_ 593 del __os_
585 """ 594 """
586 out, err = self._interface.execute(command, mode=self.submitMode) 595 out, err = self._interface.execute(command, mode=self._submitMode)
587 if err: 596 if err:
588 raise OSError(self._shortError(err)) 597 raise OSError(self._shortError(err))
589 return out.decode("utf-8").strip() 598 return out.decode("utf-8").strip()
590 599
591 def rm(self, filename): 600 def rm(self, filename):
606 raise err 615 raise err
607 del __os_ 616 del __os_
608 """.format( 617 """.format(
609 filename 618 filename
610 ) 619 )
611 out, err = self._interface.execute(command, mode=self.submitMode) 620 out, err = self._interface.execute(command, mode=self._submitMode)
612 if err: 621 if err:
613 raise OSError(self._shortError(err)) 622 raise OSError(self._shortError(err))
614 623
615 def rmrf(self, name, recursive=False, force=False): 624 def rmrf(self, name, recursive=False, force=False):
616 """ 625 """
653 print(remove_file('{0}', {1}, {2})) 662 print(remove_file('{0}', {1}, {2}))
654 del __os_, remove_file 663 del __os_, remove_file
655 """.format( 664 """.format(
656 name, recursive, force 665 name, recursive, force
657 ) 666 )
658 out, err = self._interface.execute(command, mode=self.submitMode) 667 out, err = self._interface.execute(command, mode=self._submitMode)
659 if err: 668 if err:
660 raise OSError(self._shortError(err)) 669 raise OSError(self._shortError(err))
661 return ast.literal_eval(out.decode("utf-8")) 670 return ast.literal_eval(out.decode("utf-8"))
662 671
663 return False 672 return False
676 __os_.mkdir('{0}') 685 __os_.mkdir('{0}')
677 del __os_ 686 del __os_
678 """.format( 687 """.format(
679 dirname 688 dirname
680 ) 689 )
681 out, err = self._interface.execute(command, mode=self.submitMode) 690 out, err = self._interface.execute(command, mode=self._submitMode)
682 if err: 691 if err:
683 raise OSError(self._shortError(err)) 692 raise OSError(self._shortError(err))
684 693
685 def rmdir(self, dirname): 694 def rmdir(self, dirname):
686 """ 695 """
703 raise 712 raise
704 del __os_ 713 del __os_
705 """.format( 714 """.format(
706 dirname 715 dirname
707 ) 716 )
708 out, err = self._interface.execute(command, mode=self.submitMode) 717 out, err = self._interface.execute(command, mode=self._submitMode)
709 if err: 718 if err:
710 raise OSError(self._shortError(err)) 719 raise OSError(self._shortError(err))
711 720
712 def put(self, hostFileName, deviceFileName=None): 721 def put(self, hostFileName, deviceFileName=None):
713 """ 722 """
765 "del f, fd", 774 "del f, fd",
766 ] 775 ]
767 ) 776 )
768 command = "\n".join(commands) 777 command = "\n".join(commands)
769 778
770 out, err = self._interface.execute(command, mode=self.submitMode) 779 out, err = self._interface.execute(command, mode=self._submitMode)
771 if err: 780 if err:
772 raise OSError(self._shortError(err)) 781 raise OSError(self._shortError(err))
773 return True 782 return True
774 783
775 def get(self, deviceFileName, hostFileName=None): 784 def get(self, deviceFileName, hostFileName=None):
834 send_data() 843 send_data()
835 del send_data 844 del send_data
836 """.format( 845 """.format(
837 deviceFileName 846 deviceFileName
838 ) 847 )
839 out, err = self._interface.execute(command, mode=self.submitMode) 848 out, err = self._interface.execute(command, mode=self._submitMode)
840 if err: 849 if err:
841 raise OSError(self._shortError(err)) 850 raise OSError(self._shortError(err))
842 851
843 # write the received bytes to the local file 852 # write the received bytes to the local file
844 # convert eol to "\n" 853 # convert eol to "\n"
873 return infolist 882 return infolist
874 883
875 print(fsinfo()) 884 print(fsinfo())
876 del __os_, fsinfo 885 del __os_, fsinfo
877 """ 886 """
878 out, err = self._interface.execute(command, mode=self.submitMode) 887 out, err = self._interface.execute(command, mode=self._submitMode)
879 if err: 888 if err:
880 raise OSError(self._shortError(err)) 889 raise OSError(self._shortError(err))
881 infolist = ast.literal_eval(out.decode("utf-8")) 890 infolist = ast.literal_eval(out.decode("utf-8"))
882 if infolist is None: 891 if infolist is None:
883 return None 892 return None
946 res['mpy_variant_version'] = '' 955 res['mpy_variant_version'] = ''
947 956
948 print(res) 957 print(res)
949 del res, uname, __os_, __sys_ 958 del res, uname, __os_, __sys_
950 """ 959 """
951 out, err = self._interface.execute(command, mode=self.submitMode) 960 out, err = self._interface.execute(command, mode=self._submitMode)
952 if err: 961 if err:
953 raise OSError(self._shortError(err)) 962 raise OSError(self._shortError(err))
954 return ast.literal_eval(out.decode("utf-8")) 963 return ast.literal_eval(out.decode("utf-8"))
955 964
956 def getBoardInformation(self): 965 def getBoardInformation(self):
1055 res['ulab'] = None 1064 res['ulab'] = None
1056 1065
1057 print(res) 1066 print(res)
1058 del res, __os_, __sys_, uname 1067 del res, __os_, __sys_, uname
1059 """ 1068 """
1060 out, err = self._interface.execute(command, mode=self.submitMode) 1069 out, err = self._interface.execute(command, mode=self._submitMode)
1061 if err: 1070 if err:
1062 raise OSError(self._shortError(err)) 1071 raise OSError(self._shortError(err))
1063 return ast.literal_eval(out.decode("utf-8")) 1072 return ast.literal_eval(out.decode("utf-8"))
1064 1073
1065 def getModules(self): 1074 def getModules(self):
1069 @return list of builtin modules 1078 @return list of builtin modules
1070 @rtype list of str 1079 @rtype list of str
1071 @exception OSError raised to indicate an issue with the device 1080 @exception OSError raised to indicate an issue with the device
1072 """ 1081 """
1073 commands = ["help('modules')"] 1082 commands = ["help('modules')"]
1074 out, err = self._interface.execute(commands, mode=self.submitMode) 1083 out, err = self._interface.execute(commands, mode=self._submitMode)
1075 if err: 1084 if err:
1076 raise OSError(self._shortError(err)) 1085 raise OSError(self._shortError(err))
1077 1086
1078 modules = [] 1087 modules = []
1079 for line in out.decode("utf-8").splitlines()[:-1]: 1088 for line in out.decode("utf-8").splitlines()[:-1]:
1111 .format(tm[0], tm[1], tm[2], tm[3], tm[4], tm[5]) 1120 .format(tm[0], tm[1], tm[2], tm[3], tm[4], tm[5])
1112 ) 1121 )
1113 del tm 1122 del tm
1114 del __time_ 1123 del __time_
1115 """ 1124 """
1116 out, err = self._interface.execute(command, mode=self.submitMode) 1125 out, err = self._interface.execute(command, mode=self._submitMode)
1117 if err: 1126 if err:
1118 if b"NotImplementedError" in err: 1127 if b"NotImplementedError" in err:
1119 return "<unsupported> <unsupported>" 1128 return "<unsupported> <unsupported>"
1120 raise OSError(self._shortError(err)) 1129 raise OSError(self._shortError(err))
1121 return out.decode("utf-8").strip() 1130 return out.decode("utf-8").strip()
1183 now.tm_sec, 1192 now.tm_sec,
1184 now.tm_yday, 1193 now.tm_yday,
1185 now.tm_isdst, 1194 now.tm_isdst,
1186 ), 1195 ),
1187 ) 1196 )
1188 out, err = self._interface.execute(command, mode=self.submitMode) 1197 out, err = self._interface.execute(command, mode=self._submitMode)
1189 if err: 1198 if err:
1190 raise OSError(self._shortError(err)) 1199 raise OSError(self._shortError(err))
1191 1200
1192 ################################################################## 1201 ##################################################################
1193 ## Methods below implement WiFi related methods 1202 ## Methods below implement WiFi related methods

eric ide

mercurial