eric6/MicroPython/GenericMicroPythonDevices.py

changeset 8082
2242a6a1d786
child 8099
522946e53835
equal deleted inserted replaced
8081:e0087e542717 8082:2242a6a1d786
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2021 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing the device interface class for generic MicroPython devices
8 (i.e. those devices not specifically supported yet).
9 """
10
11 import os
12
13 from E5Gui import E5MessageBox
14
15 from .MicroPythonDevices import MicroPythonDevice
16 from .MicroPythonWidget import HAS_QTCHART
17
18 import Preferences
19 import Utilities
20
21
22 class GenericMicroPythonDevice(MicroPythonDevice):
23 """
24 Class implementing the device interface for generic MicroPython boards.
25 """
26 def __init__(self, microPythonWidget, vid, pid, parent=None):
27 """
28 Constructor
29
30 @param microPythonWidget reference to the main MicroPython widget
31 @type MicroPythonWidget
32 @param vid vendor ID
33 @type int
34 @param pid product ID
35 @type int
36 @param parent reference to the parent object
37 @type QObject
38 """
39 super(GenericMicroPythonDevice, self).__init__(microPythonWidget,
40 parent)
41
42 self.__directAccess = False
43 self.__deviceVolumeName = ""
44 self.__workspace = ""
45 self.__deviceName = ""
46
47 for deviceData in Preferences.getMicroPython("ManualDevices"):
48 if (
49 deviceData["vid"] == vid and
50 deviceData["pid"] == pid
51 ):
52 self.__deviceVolumeName = deviceData["data_volume"]
53 self.__directAccess = bool(deviceData["data_volume"])
54 self.__deviceName = deviceData["description"]
55
56 self.__workspace = self.__findWorkspace()
57
58 def setButtons(self):
59 """
60 Public method to enable the supported action buttons.
61 """
62 super(GenericMicroPythonDevice, self).setButtons()
63 self.microPython.setActionButtons(
64 run=True, repl=True, files=True, chart=HAS_QTCHART)
65
66 if self.__directAccess and self.__deviceVolumeMounted():
67 self.microPython.setActionButtons(open=True, save=True)
68
69 def deviceName(self):
70 """
71 Public method to get the name of the device.
72
73 @return name of the device
74 @rtype str
75 """
76 return self.__deviceName
77
78 def canStartRepl(self):
79 """
80 Public method to determine, if a REPL can be started.
81
82 @return tuple containing a flag indicating it is safe to start a REPL
83 and a reason why it cannot.
84 @rtype tuple of (bool, str)
85 """
86 return True, ""
87
88 def canStartPlotter(self):
89 """
90 Public method to determine, if a Plotter can be started.
91
92 @return tuple containing a flag indicating it is safe to start a
93 Plotter and a reason why it cannot.
94 @rtype tuple of (bool, str)
95 """
96 return True, ""
97
98 def canRunScript(self):
99 """
100 Public method to determine, if a script can be executed.
101
102 @return tuple containing a flag indicating it is safe to start a
103 Plotter and a reason why it cannot.
104 @rtype tuple of (bool, str)
105 """
106 return True, ""
107
108 def runScript(self, script):
109 """
110 Public method to run the given Python script.
111
112 @param script script to be executed
113 @type str
114 """
115 pythonScript = script.split("\n")
116 self.sendCommands(pythonScript)
117
118 def canStartFileManager(self):
119 """
120 Public method to determine, if a File Manager can be started.
121
122 @return tuple containing a flag indicating it is safe to start a
123 File Manager and a reason why it cannot.
124 @rtype tuple of (bool, str)
125 """
126 return True, ""
127
128 def supportsLocalFileAccess(self):
129 """
130 Public method to indicate file access via a local directory.
131
132 @return flag indicating file access via local directory
133 @rtype bool
134 """
135 return self.__deviceVolumeMounted()
136
137 def __deviceVolumeMounted(self):
138 """
139 Private method to check, if the device volume is mounted.
140
141 @return flag indicated a mounted device
142 @rtype bool
143 """
144 if self.__workspace and not os.path.exists(self.__workspace):
145 self.__workspace = "" # reset
146
147 return (
148 self.__directAccess and
149 self.__deviceVolumeName in self.getWorkspace(silent=True)
150 )
151
152 def getWorkspace(self, silent=False):
153 """
154 Public method to get the workspace directory.
155
156 @param silent flag indicating silent operations
157 @type bool
158 @return workspace directory used for saving files
159 @rtype str
160 """
161 if self.__directAccess:
162 if self.__workspace:
163 # return cached entry
164 return self.__workspace
165 else:
166 self.__workspace = self.__findWorkspace(silent=silent)
167 return self.__workspace
168 else:
169 super(GenericMicroPythonDevice, self).getWorkspace()
170
171 def __findWorkspace(self, silent=False):
172 """
173 Private method to find the workspace directory.
174
175 @param silent flag indicating silent operations
176 @type bool
177 @return workspace directory used for saving files
178 @rtype str
179 """
180 # Attempts to find the path on the filesystem that represents the
181 # plugged in PyBoard board.
182 deviceDirectories = Utilities.findVolume(self.__deviceVolumeName,
183 findAll=True)
184
185 if deviceDirectories:
186 if len(deviceDirectories) == 1:
187 return deviceDirectories[0]
188 else:
189 return self.selectDeviceDirectory(deviceDirectories)
190 else:
191 # return the default workspace and give the user a warning (unless
192 # silent mode is selected)
193 if not silent:
194 E5MessageBox.warning(
195 self.microPython,
196 self.tr("Workspace Directory"),
197 self.tr("Python files for this generic board can be"
198 " edited in place, if the device volume is locally"
199 " available. A volume named '{0}' was not found."
200 " In place editing will not be available."
201 ).format(self.__deviceVolumeName)
202 )
203
204 return super(GenericMicroPythonDevice, self).getWorkspace()

eric ide

mercurial