Differential D3592 Diff 8765 plugins/extensions/pykrita/plugin/plugins/scripter/ui_scripter/actions/runaction.py
Changeset View
Changeset View
Standalone View
Standalone View
plugins/extensions/pykrita/plugin/plugins/scripter/ui_scripter/actions/runaction.py
- This file was added.
1 | from PyQt5.QtWidgets import QAction, QMessageBox | ||||
---|---|---|---|---|---|
2 | from PyQt5.QtGui import QIcon | ||||
3 | import sys | ||||
4 | from ..editor import docwrapper | ||||
5 | | ||||
6 | class RunAction(QAction): | ||||
7 | | ||||
8 | def __init__(self, scripter, parent=None): | ||||
9 | super(RunAction, self).__init__(parent) | ||||
10 | self.scripter = scripter | ||||
11 | | ||||
12 | self.editor = self.scripter.uicontroller.editor | ||||
13 | self.output = self.scripter.uicontroller.output | ||||
14 | | ||||
15 | self.triggered.connect(self.run) | ||||
16 | | ||||
17 | self.setText('Run') | ||||
18 | self.setIcon(QIcon('/home/eliakincosta/Pictures/play.svg')) | ||||
19 | | ||||
20 | @property | ||||
21 | def parent(self): | ||||
22 | return 'toolBar' | ||||
23 | | ||||
24 | def run(self): | ||||
25 | stdout = sys.stdout | ||||
26 | stderr = sys.stderr | ||||
27 | output = docwrapper.DocWrapper(self.output.document()) | ||||
28 | output.write("======================================\n") | ||||
29 | sys.stdout = output | ||||
30 | sys.stderr = output | ||||
31 | script = self.editor.document().toPlainText() | ||||
32 | try: | ||||
33 | bc = compile(script, "<string>", "exec") | ||||
34 | except Exception as e: | ||||
35 | QMessageBox.critical(self.editor, "Error compiling script", str(e)) | ||||
36 | return | ||||
37 | | ||||
38 | exec(bc) | ||||
39 | | ||||
40 | sys.stdout = stdout | ||||
41 | sys.stderr = stderr |