|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2017 - 2019 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the a class used to display the protocols (protobuf) part |
|
8 of the project. |
|
9 """ |
|
10 |
|
11 from __future__ import unicode_literals |
|
12 try: |
|
13 str = unicode |
|
14 except NameError: |
|
15 pass |
|
16 |
|
17 import os |
|
18 import glob |
|
19 import sys |
|
20 |
|
21 from PyQt5.QtCore import QThread, pyqtSignal, QProcess |
|
22 from PyQt5.QtWidgets import QDialog, QApplication, QMenu |
|
23 |
|
24 from E5Gui.E5Application import e5App |
|
25 from E5Gui import E5MessageBox |
|
26 from E5Gui.E5ProgressDialog import E5ProgressDialog |
|
27 |
|
28 from .ProjectBrowserModel import ProjectBrowserFileItem, \ |
|
29 ProjectBrowserSimpleDirectoryItem, ProjectBrowserDirectoryItem, \ |
|
30 ProjectBrowserProtocolsType |
|
31 from .ProjectBaseBrowser import ProjectBaseBrowser |
|
32 |
|
33 from UI.BrowserModel import BrowserFileItem, BrowserClassItem, \ |
|
34 BrowserMethodItem, BrowserClassAttributeItem |
|
35 import UI.PixmapCache |
|
36 |
|
37 import Preferences |
|
38 import Utilities |
|
39 |
|
40 |
|
41 class ProjectProtocolsBrowser(ProjectBaseBrowser): |
|
42 """ |
|
43 A class used to display the protocols (protobuf) part of the project. |
|
44 |
|
45 @signal appendStdout(str) emitted after something was received from |
|
46 a QProcess on stdout |
|
47 @signal appendStderr(str) emitted after something was received from |
|
48 a QProcess on stderr |
|
49 @signal showMenu(str, QMenu) emitted when a menu is about to be shown. |
|
50 The name of the menu and a reference to the menu are given. |
|
51 """ |
|
52 appendStdout = pyqtSignal(str) |
|
53 appendStderr = pyqtSignal(str) |
|
54 showMenu = pyqtSignal(str, QMenu) |
|
55 |
|
56 def __init__(self, project, parent=None): |
|
57 """ |
|
58 Constructor |
|
59 |
|
60 @param project reference to the project object |
|
61 @type Project |
|
62 @param parent parent widget of this browser |
|
63 @type QWidget |
|
64 """ |
|
65 ProjectBaseBrowser.__init__(self, project, |
|
66 ProjectBrowserProtocolsType, parent) |
|
67 |
|
68 self.selectedItemsFilter = \ |
|
69 [ProjectBrowserFileItem, ProjectBrowserSimpleDirectoryItem] |
|
70 |
|
71 self.setWindowTitle(self.tr('Protocols (protobuf)')) |
|
72 |
|
73 self.setWhatsThis(self.tr( |
|
74 """<b>Project Protocols Browser</b>""" |
|
75 """<p>This allows to easily see all protocols (protobuf files)""" |
|
76 """ contained in the current project. Several actions can be""" |
|
77 """ executed via the context menu.</p>""" |
|
78 )) |
|
79 |
|
80 project.prepareRepopulateItem.connect(self._prepareRepopulateItem) |
|
81 project.completeRepopulateItem.connect(self._completeRepopulateItem) |
|
82 |
|
83 def _createPopupMenus(self): |
|
84 """ |
|
85 Protected overloaded method to generate the popup menu. |
|
86 """ |
|
87 self.menuActions = [] |
|
88 self.multiMenuActions = [] |
|
89 self.dirMenuActions = [] |
|
90 self.dirMultiMenuActions = [] |
|
91 |
|
92 self.sourceMenu = QMenu(self) |
|
93 self.sourceMenu.addAction( |
|
94 self.tr('Compile protocol'), self.__compileProtocol) |
|
95 self.sourceMenu.addAction( |
|
96 self.tr('Compile all protocols'), |
|
97 self.__compileAllProtocols) |
|
98 self.sourceMenu.addSeparator() |
|
99 self.sourceMenu.addAction( |
|
100 self.tr('Compile protocol as gRPC'), |
|
101 lambda: self.__compileProtocol(grpc=True)) |
|
102 self.sourceMenu.addAction( |
|
103 self.tr('Compile all protocols as gRPC'), |
|
104 lambda: self.__compileAllProtocols(grpc=True)) |
|
105 self.sourceMenu.addSeparator() |
|
106 self.sourceMenu.addAction(self.tr('Open'), self._openItem) |
|
107 self.sourceMenu.addSeparator() |
|
108 act = self.sourceMenu.addAction( |
|
109 self.tr('Rename file'), self._renameFile) |
|
110 self.menuActions.append(act) |
|
111 act = self.sourceMenu.addAction( |
|
112 self.tr('Remove from project'), self._removeFile) |
|
113 self.menuActions.append(act) |
|
114 act = self.sourceMenu.addAction( |
|
115 self.tr('Delete'), self.__deleteFile) |
|
116 self.menuActions.append(act) |
|
117 self.sourceMenu.addSeparator() |
|
118 self.sourceMenu.addAction( |
|
119 self.tr('Add protocols...'), self.__addProtocolFiles) |
|
120 self.sourceMenu.addAction( |
|
121 self.tr('Add protocols directory...'), |
|
122 self.__addProtocolsDirectory) |
|
123 self.sourceMenu.addSeparator() |
|
124 self.sourceMenu.addAction( |
|
125 self.tr('Copy Path to Clipboard'), self._copyToClipboard) |
|
126 self.sourceMenu.addSeparator() |
|
127 self.sourceMenu.addAction( |
|
128 self.tr('Expand all directories'), self._expandAllDirs) |
|
129 self.sourceMenu.addAction( |
|
130 self.tr('Collapse all directories'), self._collapseAllDirs) |
|
131 self.sourceMenu.addSeparator() |
|
132 self.sourceMenu.addAction(self.tr('Configure...'), self._configure) |
|
133 self.sourceMenu.addAction( |
|
134 self.tr('Configure Protobuf...'), self.__configureProtobuf) |
|
135 |
|
136 self.menu = QMenu(self) |
|
137 self.menu.addAction( |
|
138 self.tr('Compile protocol'), self.__compileProtocol) |
|
139 self.menu.addAction( |
|
140 self.tr('Compile all protocols'), |
|
141 self.__compileAllProtocols) |
|
142 self.menu.addSeparator() |
|
143 self.menu.addAction( |
|
144 self.tr('Compile protocol as gRPC'), |
|
145 lambda: self.__compileProtocol(grpc=True)) |
|
146 self.menu.addAction( |
|
147 self.tr('Compile all protocols as gRPC'), |
|
148 lambda: self.__compileAllProtocols(grpc=True)) |
|
149 self.menu.addSeparator() |
|
150 self.menu.addAction(self.tr('Open'), self._openItem) |
|
151 self.menu.addSeparator() |
|
152 self.menu.addAction( |
|
153 self.tr('Add protocols...'), self.__addProtocolFiles) |
|
154 self.menu.addAction( |
|
155 self.tr('Add protocols directory...'), |
|
156 self.__addProtocolsDirectory) |
|
157 self.menu.addSeparator() |
|
158 self.menu.addAction( |
|
159 self.tr('Expand all directories'), self._expandAllDirs) |
|
160 self.menu.addAction( |
|
161 self.tr('Collapse all directories'), self._collapseAllDirs) |
|
162 self.menu.addSeparator() |
|
163 self.menu.addAction(self.tr('Configure...'), self._configure) |
|
164 self.menu.addAction( |
|
165 self.tr('Configure Protobuf...'), self.__configureProtobuf) |
|
166 |
|
167 self.backMenu = QMenu(self) |
|
168 self.backMenu.addAction( |
|
169 self.tr('Compile all protocols'), |
|
170 self.__compileAllProtocols) |
|
171 self.backMenu.addSeparator() |
|
172 self.backMenu.addAction( |
|
173 self.tr('Compile all protocols as gRPC'), |
|
174 lambda: self.__compileAllProtocols(grpc=True)) |
|
175 self.backMenu.addSeparator() |
|
176 self.backMenu.addAction( |
|
177 self.tr('Add protocols...'), self.project.addProtoFiles) |
|
178 self.backMenu.addAction( |
|
179 self.tr('Add protocols directory...'), self.project.addProtoDir) |
|
180 self.backMenu.addSeparator() |
|
181 self.backMenu.addAction( |
|
182 self.tr('Expand all directories'), self._expandAllDirs) |
|
183 self.backMenu.addAction( |
|
184 self.tr('Collapse all directories'), self._collapseAllDirs) |
|
185 self.backMenu.addSeparator() |
|
186 self.backMenu.addAction(self.tr('Configure...'), self._configure) |
|
187 self.backMenu.addAction( |
|
188 self.tr('Configure Protobuf...'), self.__configureProtobuf) |
|
189 self.backMenu.setEnabled(False) |
|
190 |
|
191 # create the menu for multiple selected files |
|
192 self.multiMenu = QMenu(self) |
|
193 self.multiMenu.addAction( |
|
194 self.tr('Compile protocols'), |
|
195 self.__compileSelectedProtocols) |
|
196 self.multiMenu.addSeparator() |
|
197 self.multiMenu.addAction( |
|
198 self.tr('Compile protocols as gRPC'), |
|
199 lambda: self.__compileSelectedProtocols(grpc=True)) |
|
200 self.multiMenu.addSeparator() |
|
201 self.multiMenu.addAction(self.tr('Open'), self._openItem) |
|
202 self.multiMenu.addSeparator() |
|
203 act = self.multiMenu.addAction( |
|
204 self.tr('Remove from project'), self._removeFile) |
|
205 self.multiMenuActions.append(act) |
|
206 act = self.multiMenu.addAction( |
|
207 self.tr('Delete'), self.__deleteFile) |
|
208 self.multiMenuActions.append(act) |
|
209 self.multiMenu.addSeparator() |
|
210 self.multiMenu.addAction( |
|
211 self.tr('Expand all directories'), self._expandAllDirs) |
|
212 self.multiMenu.addAction( |
|
213 self.tr('Collapse all directories'), self._collapseAllDirs) |
|
214 self.multiMenu.addSeparator() |
|
215 self.multiMenu.addAction(self.tr('Configure...'), self._configure) |
|
216 self.multiMenu.addAction( |
|
217 self.tr('Configure Protobuf...'), self.__configureProtobuf) |
|
218 |
|
219 self.dirMenu = QMenu(self) |
|
220 self.dirMenu.addAction( |
|
221 self.tr('Compile all protocols'), |
|
222 self.__compileAllProtocols) |
|
223 self.dirMenu.addSeparator() |
|
224 self.dirMenu.addAction( |
|
225 self.tr('Compile all protocols as gRPC'), |
|
226 lambda: self.__compileAllProtocols(grpc=True)) |
|
227 act = self.dirMenu.addAction( |
|
228 self.tr('Remove from project'), self._removeFile) |
|
229 self.dirMenuActions.append(act) |
|
230 act = self.dirMenu.addAction( |
|
231 self.tr('Delete'), self._deleteDirectory) |
|
232 self.dirMenuActions.append(act) |
|
233 self.dirMenu.addSeparator() |
|
234 self.dirMenu.addAction( |
|
235 self.tr('Add protocols...'), self.__addProtocolFiles) |
|
236 self.dirMenu.addAction( |
|
237 self.tr('Add protocols directory...'), |
|
238 self.__addProtocolsDirectory) |
|
239 self.dirMenu.addSeparator() |
|
240 self.dirMenu.addAction( |
|
241 self.tr('Copy Path to Clipboard'), self._copyToClipboard) |
|
242 self.dirMenu.addSeparator() |
|
243 self.dirMenu.addAction( |
|
244 self.tr('Expand all directories'), self._expandAllDirs) |
|
245 self.dirMenu.addAction( |
|
246 self.tr('Collapse all directories'), self._collapseAllDirs) |
|
247 self.dirMenu.addSeparator() |
|
248 self.dirMenu.addAction(self.tr('Configure...'), self._configure) |
|
249 self.dirMenu.addAction( |
|
250 self.tr('Configure Protobuf...'), self.__configureProtobuf) |
|
251 |
|
252 self.dirMultiMenu = QMenu(self) |
|
253 self.dirMultiMenu.addAction( |
|
254 self.tr('Compile all protocols'), |
|
255 self.__compileAllProtocols) |
|
256 self.dirMultiMenu.addSeparator() |
|
257 self.dirMultiMenu.addAction( |
|
258 self.tr('Compile all protocols as gRPC'), |
|
259 lambda: self.__compileAllProtocols(grpc=True)) |
|
260 self.dirMultiMenu.addAction( |
|
261 self.tr('Add protocols...'), self.project.addProtoFiles) |
|
262 self.dirMultiMenu.addAction( |
|
263 self.tr('Add protocols directory...'), self.project.addProtoDir) |
|
264 self.dirMultiMenu.addSeparator() |
|
265 self.dirMultiMenu.addAction( |
|
266 self.tr('Expand all directories'), self._expandAllDirs) |
|
267 self.dirMultiMenu.addAction( |
|
268 self.tr('Collapse all directories'), self._collapseAllDirs) |
|
269 self.dirMultiMenu.addSeparator() |
|
270 self.dirMultiMenu.addAction( |
|
271 self.tr('Configure...'), self._configure) |
|
272 self.dirMultiMenu.addAction(self.tr('Configure Protobuf...'), |
|
273 self.__configureProtobuf) |
|
274 |
|
275 self.sourceMenu.aboutToShow.connect(self.__showContextMenu) |
|
276 self.multiMenu.aboutToShow.connect(self.__showContextMenuMulti) |
|
277 self.dirMenu.aboutToShow.connect(self.__showContextMenuDir) |
|
278 self.dirMultiMenu.aboutToShow.connect(self.__showContextMenuDirMulti) |
|
279 self.backMenu.aboutToShow.connect(self.__showContextMenuBack) |
|
280 self.mainMenu = self.sourceMenu |
|
281 |
|
282 def _contextMenuRequested(self, coord): |
|
283 """ |
|
284 Protected slot to show the context menu. |
|
285 |
|
286 @param coord the position of the mouse pointer (QPoint) |
|
287 """ |
|
288 if not self.project.isOpen(): |
|
289 return |
|
290 |
|
291 try: |
|
292 categories = self.getSelectedItemsCountCategorized( |
|
293 [ProjectBrowserFileItem, BrowserClassItem, |
|
294 BrowserMethodItem, ProjectBrowserSimpleDirectoryItem]) |
|
295 cnt = categories["sum"] |
|
296 if cnt <= 1: |
|
297 index = self.indexAt(coord) |
|
298 if index.isValid(): |
|
299 self._selectSingleItem(index) |
|
300 categories = self.getSelectedItemsCountCategorized( |
|
301 [ProjectBrowserFileItem, BrowserClassItem, |
|
302 BrowserMethodItem, ProjectBrowserSimpleDirectoryItem]) |
|
303 cnt = categories["sum"] |
|
304 |
|
305 bfcnt = categories[str(ProjectBrowserFileItem)] |
|
306 cmcnt = categories[str(BrowserClassItem)] + \ |
|
307 categories[str(BrowserMethodItem)] |
|
308 sdcnt = categories[str(ProjectBrowserSimpleDirectoryItem)] |
|
309 if cnt > 1 and cnt == bfcnt: |
|
310 self.multiMenu.popup(self.mapToGlobal(coord)) |
|
311 elif cnt > 1 and cnt == sdcnt: |
|
312 self.dirMultiMenu.popup(self.mapToGlobal(coord)) |
|
313 else: |
|
314 index = self.indexAt(coord) |
|
315 if cnt == 1 and index.isValid(): |
|
316 if bfcnt == 1 or cmcnt == 1: |
|
317 itm = self.model().item(index) |
|
318 if isinstance(itm, ProjectBrowserFileItem): |
|
319 self.sourceMenu.popup(self.mapToGlobal(coord)) |
|
320 elif isinstance(itm, BrowserClassItem) or \ |
|
321 isinstance(itm, BrowserMethodItem): |
|
322 self.menu.popup(self.mapToGlobal(coord)) |
|
323 else: |
|
324 self.backMenu.popup(self.mapToGlobal(coord)) |
|
325 elif sdcnt == 1: |
|
326 self.dirMenu.popup(self.mapToGlobal(coord)) |
|
327 else: |
|
328 self.backMenu.popup(self.mapToGlobal(coord)) |
|
329 else: |
|
330 self.backMenu.popup(self.mapToGlobal(coord)) |
|
331 except Exception: |
|
332 pass |
|
333 |
|
334 def __showContextMenu(self): |
|
335 """ |
|
336 Private slot called by the menu aboutToShow signal. |
|
337 """ |
|
338 ProjectBaseBrowser._showContextMenu(self, self.menu) |
|
339 |
|
340 self.showMenu.emit("Main", self.menu) |
|
341 |
|
342 def __showContextMenuMulti(self): |
|
343 """ |
|
344 Private slot called by the multiMenu aboutToShow signal. |
|
345 """ |
|
346 ProjectBaseBrowser._showContextMenuMulti(self, self.multiMenu) |
|
347 |
|
348 self.showMenu.emit("MainMulti", self.multiMenu) |
|
349 |
|
350 def __showContextMenuDir(self): |
|
351 """ |
|
352 Private slot called by the dirMenu aboutToShow signal. |
|
353 """ |
|
354 ProjectBaseBrowser._showContextMenuDir(self, self.dirMenu) |
|
355 |
|
356 self.showMenu.emit("MainDir", self.dirMenu) |
|
357 |
|
358 def __showContextMenuDirMulti(self): |
|
359 """ |
|
360 Private slot called by the dirMultiMenu aboutToShow signal. |
|
361 """ |
|
362 ProjectBaseBrowser._showContextMenuDirMulti(self, self.dirMultiMenu) |
|
363 |
|
364 self.showMenu.emit("MainDirMulti", self.dirMultiMenu) |
|
365 |
|
366 def __showContextMenuBack(self): |
|
367 """ |
|
368 Private slot called by the backMenu aboutToShow signal. |
|
369 """ |
|
370 ProjectBaseBrowser._showContextMenuBack(self, self.backMenu) |
|
371 |
|
372 self.showMenu.emit("MainBack", self.backMenu) |
|
373 |
|
374 def _openItem(self): |
|
375 """ |
|
376 Protected slot to handle the open popup menu entry. |
|
377 """ |
|
378 itmList = self.getSelectedItems( |
|
379 [BrowserFileItem, BrowserClassItem, BrowserMethodItem, |
|
380 BrowserClassAttributeItem]) |
|
381 |
|
382 for itm in itmList: |
|
383 if isinstance(itm, BrowserFileItem): |
|
384 self.sourceFile[str].emit(itm.fileName()) |
|
385 elif isinstance(itm, BrowserClassItem): |
|
386 self.sourceFile[str, int].emit( |
|
387 itm.fileName(), itm.classObject().lineno) |
|
388 elif isinstance(itm, BrowserMethodItem): |
|
389 self.sourceFile[str, int].emit( |
|
390 itm.fileName(), itm.functionObject().lineno) |
|
391 elif isinstance(itm, BrowserClassAttributeItem): |
|
392 self.sourceFile[str, int].emit( |
|
393 itm.fileName(), itm.attributeObject().lineno) |
|
394 |
|
395 def __addProtocolFiles(self): |
|
396 """ |
|
397 Private method to add protocol files to the project. |
|
398 """ |
|
399 itm = self.model().item(self.currentIndex()) |
|
400 if isinstance(itm, ProjectBrowserFileItem) or \ |
|
401 isinstance(itm, BrowserClassItem) or \ |
|
402 isinstance(itm, BrowserMethodItem): |
|
403 dn = os.path.dirname(itm.fileName()) |
|
404 elif isinstance(itm, ProjectBrowserSimpleDirectoryItem) or \ |
|
405 isinstance(itm, ProjectBrowserDirectoryItem): |
|
406 dn = itm.dirName() |
|
407 else: |
|
408 dn = None |
|
409 self.project.addFiles('protocol', dn) |
|
410 |
|
411 def __addProtocolsDirectory(self): |
|
412 """ |
|
413 Private method to add protocol files of a directory to the project. |
|
414 """ |
|
415 itm = self.model().item(self.currentIndex()) |
|
416 if isinstance(itm, ProjectBrowserFileItem) or \ |
|
417 isinstance(itm, BrowserClassItem) or \ |
|
418 isinstance(itm, BrowserMethodItem): |
|
419 dn = os.path.dirname(itm.fileName()) |
|
420 elif isinstance(itm, ProjectBrowserSimpleDirectoryItem) or \ |
|
421 isinstance(itm, ProjectBrowserDirectoryItem): |
|
422 dn = itm.dirName() |
|
423 else: |
|
424 dn = None |
|
425 self.project.addDirectory('protocol', dn) |
|
426 |
|
427 def __deleteFile(self): |
|
428 """ |
|
429 Private method to delete files from the project. |
|
430 """ |
|
431 itmList = self.getSelectedItems() |
|
432 |
|
433 files = [] |
|
434 fullNames = [] |
|
435 for itm in itmList: |
|
436 fn2 = itm.fileName() |
|
437 fullNames.append(fn2) |
|
438 fn = self.project.getRelativePath(fn2) |
|
439 files.append(fn) |
|
440 |
|
441 from UI.DeleteFilesConfirmationDialog import \ |
|
442 DeleteFilesConfirmationDialog |
|
443 dlg = DeleteFilesConfirmationDialog( |
|
444 self.parent(), |
|
445 self.tr("Delete Protocols"), |
|
446 self.tr("Do you really want to delete these protocol files from" |
|
447 " the project?"), |
|
448 files) |
|
449 |
|
450 if dlg.exec_() == QDialog.Accepted: |
|
451 for fn2, fn in zip(fullNames, files): |
|
452 self.closeSourceWindow.emit(fn2) |
|
453 self.project.deleteFile(fn) |
|
454 |
|
455 ########################################################################### |
|
456 ## Methods to handle the various compile commands |
|
457 ########################################################################### |
|
458 |
|
459 def __getCompilerCommand(self, grpc): |
|
460 """ |
|
461 Private method to get the compiler command. |
|
462 |
|
463 @param grpc flag indicating to get a gRPC command |
|
464 @type bool |
|
465 @return tuple giving the executable and its parameter list |
|
466 @rtype tuple of (str, list of str) |
|
467 """ |
|
468 exe = None |
|
469 exeArgs = [] |
|
470 |
|
471 if grpc: |
|
472 exe = Preferences.getProtobuf("grpcPython") |
|
473 if exe == "": |
|
474 exe = sys.executable |
|
475 exeArgs = ['-m', 'grpc_tools.protoc'] |
|
476 else: |
|
477 exe = Preferences.getProtobuf("protoc") |
|
478 if exe == "": |
|
479 exe = Utilities.isWindowsPlatform() and \ |
|
480 "protoc.exe" or "protoc" |
|
481 if not Utilities.isinpath(exe): |
|
482 exe = None |
|
483 |
|
484 return exe, exeArgs |
|
485 |
|
486 def __readStdout(self): |
|
487 """ |
|
488 Private slot to handle the readyReadStandardOutput signal of the |
|
489 protoc process. |
|
490 """ |
|
491 if self.compileProc is None: |
|
492 return |
|
493 |
|
494 ioEncoding = Preferences.getSystem("IOEncoding") |
|
495 |
|
496 self.compileProc.setReadChannel(QProcess.StandardOutput) |
|
497 while self.compileProc and self.compileProc.canReadLine(): |
|
498 s = 'protoc: ' |
|
499 output = str(self.compileProc.readLine(), ioEncoding, 'replace') |
|
500 s += output |
|
501 self.appendStdout.emit(s) |
|
502 |
|
503 def __readStderr(self): |
|
504 """ |
|
505 Private slot to handle the readyReadStandardError signal of the |
|
506 protoc process. |
|
507 """ |
|
508 if self.compileProc is None: |
|
509 return |
|
510 |
|
511 ioEncoding = Preferences.getSystem("IOEncoding") |
|
512 |
|
513 self.compileProc.setReadChannel(QProcess.StandardError) |
|
514 while self.compileProc and self.compileProc.canReadLine(): |
|
515 s = 'protoc: ' |
|
516 error = str(self.compileProc.readLine(), ioEncoding, 'replace') |
|
517 s += error |
|
518 self.appendStderr.emit(s) |
|
519 |
|
520 def __compileProtoDone(self, exitCode, exitStatus, grpc): |
|
521 """ |
|
522 Private slot to handle the finished signal of the protoc process. |
|
523 |
|
524 @param exitCode exit code of the process |
|
525 @type int |
|
526 @param exitStatus exit status of the process |
|
527 @type QProcess.ExitStatus |
|
528 @param grpc flag indicating to compile as gRPC files |
|
529 @type bool |
|
530 """ |
|
531 self.__compileRunning = False |
|
532 ui = e5App().getObject("UserInterface") |
|
533 if exitStatus == QProcess.NormalExit and exitCode == 0: |
|
534 path = os.path.dirname(self.__protoFile) |
|
535 fileList = glob.glob(os.path.join(path, "*_pb2.py")) |
|
536 if grpc: |
|
537 fileList += glob.glob(os.path.join(path, "*_pb2_grpc.py")) |
|
538 for file in fileList: |
|
539 self.project.appendFile(file) |
|
540 if not self.noDialog and not ui.notificationsEnabled(): |
|
541 E5MessageBox.information( |
|
542 self, |
|
543 self.tr("Protocol Compilation"), |
|
544 self.tr( |
|
545 "The compilation of the protocol file was" |
|
546 " successful.")) |
|
547 else: |
|
548 if grpc: |
|
549 icon = UI.PixmapCache.getPixmap("gRPC48.png") |
|
550 else: |
|
551 icon = UI.PixmapCache.getPixmap("protobuf48.png") |
|
552 ui.showNotification( |
|
553 icon, |
|
554 self.tr("Protocol Compilation"), |
|
555 self.tr( |
|
556 "The compilation of the protocol file was" |
|
557 " successful.")) |
|
558 else: |
|
559 if not self.noDialog: |
|
560 E5MessageBox.information( |
|
561 self, |
|
562 self.tr("Protocol Compilation"), |
|
563 self.tr( |
|
564 "The compilation of the protocol file failed.")) |
|
565 else: |
|
566 if grpc: |
|
567 icon = UI.PixmapCache.getPixmap("gRPC48.png") |
|
568 else: |
|
569 icon = UI.PixmapCache.getPixmap("protobuf48.png") |
|
570 ui.showNotification( |
|
571 icon, |
|
572 self.tr("Protocol Compilation"), |
|
573 self.tr( |
|
574 "The compilation of the protocol file failed.")) |
|
575 self.compileProc = None |
|
576 |
|
577 def __compileProto(self, fn, noDialog=False, progress=None, grpc=False): |
|
578 """ |
|
579 Private method to compile a .proto file to Python. |
|
580 |
|
581 @param fn filename of the .proto file to be compiled |
|
582 @type str |
|
583 @param noDialog flag indicating silent operations |
|
584 @type bool |
|
585 @param progress reference to the progress dialog |
|
586 @type E5ProgressDialog |
|
587 @param grpc flag indicating to compile as gRPC files |
|
588 @type bool |
|
589 @return reference to the compile process |
|
590 @rtype QProcess |
|
591 """ |
|
592 exe, exeArgs = self.__getCompilerCommand(grpc) |
|
593 if exe: |
|
594 self.compileProc = QProcess() |
|
595 args = [] |
|
596 |
|
597 fn = os.path.join(self.project.ppath, fn) |
|
598 self.__protoFile = fn |
|
599 |
|
600 srcPath = os.path.dirname(fn) |
|
601 args.append("--proto_path={0}".format(srcPath)) |
|
602 args.append("--python_out={0}".format(srcPath)) |
|
603 if grpc: |
|
604 args.append("--grpc_python_out={0}".format(srcPath)) |
|
605 args.append(fn) |
|
606 |
|
607 self.compileProc.finished.connect( |
|
608 lambda c, s: self.__compileProtoDone(c, s, grpc)) |
|
609 self.compileProc.readyReadStandardOutput.connect(self.__readStdout) |
|
610 self.compileProc.readyReadStandardError.connect(self.__readStderr) |
|
611 |
|
612 self.noDialog = noDialog |
|
613 self.compileProc.start(exe, exeArgs + args) |
|
614 procStarted = self.compileProc.waitForStarted(5000) |
|
615 if procStarted: |
|
616 self.__compileRunning = True |
|
617 return self.compileProc |
|
618 else: |
|
619 self.__compileRunning = False |
|
620 if progress is not None: |
|
621 progress.cancel() |
|
622 E5MessageBox.critical( |
|
623 self, |
|
624 self.tr('Process Generation Error'), |
|
625 self.tr( |
|
626 '<p>Could not start {0}.<br>' |
|
627 'Ensure that it is in the search path.</p>' |
|
628 ).format(exe)) |
|
629 return None |
|
630 else: |
|
631 E5MessageBox.critical( |
|
632 self, |
|
633 self.tr('Compiler Invalid'), |
|
634 self.tr('The configured compiler is invalid.')) |
|
635 return None |
|
636 |
|
637 def __compileProtocol(self, grpc=False): |
|
638 """ |
|
639 Private method to compile a protocol to Python. |
|
640 |
|
641 @param grpc flag indicating to compile as gRPC files |
|
642 @type bool |
|
643 """ |
|
644 if self.__getCompilerCommand(grpc)[0] is not None: |
|
645 itm = self.model().item(self.currentIndex()) |
|
646 fn2 = itm.fileName() |
|
647 fn = self.project.getRelativePath(fn2) |
|
648 self.__compileProto(fn, grpc=grpc) |
|
649 |
|
650 def __compileAllProtocols(self, grpc=False): |
|
651 """ |
|
652 Private method to compile all protocols to Python. |
|
653 |
|
654 @param grpc flag indicating to compile as gRPC files |
|
655 @type bool |
|
656 """ |
|
657 if self.__getCompilerCommand(grpc)[0] is not None: |
|
658 numProtos = len(self.project.pdata["PROTOCOLS"]) |
|
659 progress = E5ProgressDialog( |
|
660 self.tr("Compiling Protocols..."), |
|
661 self.tr("Abort"), 0, numProtos, |
|
662 self.tr("%v/%m Protocols"), self) |
|
663 progress.setModal(True) |
|
664 progress.setMinimumDuration(0) |
|
665 progress.setWindowTitle(self.tr("Protocols")) |
|
666 i = 0 |
|
667 |
|
668 for fn in self.project.pdata["PROTOCOLS"]: |
|
669 progress.setValue(i) |
|
670 if progress.wasCanceled(): |
|
671 break |
|
672 proc = self.__compileProto(fn, True, progress, grpc=grpc) |
|
673 if proc is not None: |
|
674 while proc.state() == QProcess.Running: |
|
675 QApplication.processEvents() |
|
676 QThread.msleep(300) |
|
677 QApplication.processEvents() |
|
678 else: |
|
679 break |
|
680 i += 1 |
|
681 |
|
682 progress.setValue(numProtos) |
|
683 |
|
684 def __compileSelectedProtocols(self, grpc=False): |
|
685 """ |
|
686 Private method to compile selected protocols to Python. |
|
687 |
|
688 @param grpc flag indicating to compile as gRPC files |
|
689 @type bool |
|
690 """ |
|
691 if self.__getCompilerCommand(grpc)[0] is not None: |
|
692 items = self.getSelectedItems() |
|
693 |
|
694 files = [self.project.getRelativePath(itm.fileName()) |
|
695 for itm in items] |
|
696 numProtos = len(files) |
|
697 progress = E5ProgressDialog( |
|
698 self.tr("Compiling Protocols..."), |
|
699 self.tr("Abort"), 0, numProtos, |
|
700 self.tr("%v/%m Protocols"), self) |
|
701 progress.setModal(True) |
|
702 progress.setMinimumDuration(0) |
|
703 progress.setWindowTitle(self.tr("Protocols")) |
|
704 i = 0 |
|
705 |
|
706 for fn in files: |
|
707 progress.setValue(i) |
|
708 if progress.wasCanceled(): |
|
709 break |
|
710 proc = self.__compileProto(fn, True, progress, grpc=grpc) |
|
711 if proc is not None: |
|
712 while proc.state() == QProcess.Running: |
|
713 QApplication.processEvents() |
|
714 QThread.msleep(300) |
|
715 QApplication.processEvents() |
|
716 else: |
|
717 break |
|
718 i += 1 |
|
719 |
|
720 progress.setValue(numProtos) |
|
721 |
|
722 def __configureProtobuf(self): |
|
723 """ |
|
724 Private method to open the configuration dialog. |
|
725 """ |
|
726 e5App().getObject("UserInterface").showPreferences("protobufPage") |