File: console_output.py

package info (click to toggle)
qgis 3.40.13%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 1,185,160 kB
  • sloc: cpp: 1,615,781; python: 372,865; xml: 23,474; sh: 3,761; perl: 3,664; ansic: 2,829; sql: 2,137; yacc: 1,068; lex: 577; javascript: 540; lisp: 411; makefile: 155
file content (381 lines) | stat: -rw-r--r-- 13,830 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
"""
/***************************************************************************
Python Console for QGIS
                             -------------------
begin                : 2012-09-10
copyright            : (C) 2012 by Salvatore Larosa
email                : lrssvtml (at) gmail (dot) com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
Some portions of code were taken from https://code.google.com/p/pydee/
"""

from __future__ import annotations

import sys
from typing import TYPE_CHECKING

from qgis.PyQt import sip
from qgis.PyQt.QtCore import (
    Qt,
    QCoreApplication,
    QThread,
    QMetaObject,
    Q_ARG,
    QObject,
    pyqtSlot,
)
from qgis.PyQt.QtGui import QColor, QKeySequence
from qgis.PyQt.QtWidgets import (
    QAction,
    QGridLayout,
    QSpacerItem,
    QSizePolicy,
    QShortcut,
    QMenu,
    QApplication,
)
from qgis.PyQt.Qsci import QsciScintilla
from qgis.core import Qgis, QgsApplication, QgsSettings
from qgis.gui import QgsMessageBar, QgsCodeEditorPython

if TYPE_CHECKING:
    from .console import PythonConsoleWidget
    from .console_sci import ShellScintilla


class writeOut(QObject):
    # QsciLexerPython uses style codes up to 15 (Decorator style). We use 16 for error messages
    ERROR_STYLE_INDEX = 16
    ERROR_COLOR = "#e31a1c"

    def __init__(self, shellOut, out=None, style=None):
        """
        This class allows writing to stdout and stderr
        """
        super().__init__()
        self.sO = shellOut
        self.out = None
        self.style = style
        self.fire_keyboard_interrupt = False

    @pyqtSlot(str)
    def write(self, m):
        if sip.isdeleted(self.sO):
            return

        # This manage the case when console is called from another thread
        if QThread.currentThread() != QCoreApplication.instance().thread():
            QMetaObject.invokeMethod(
                self, "write", Qt.ConnectionType.QueuedConnection, Q_ARG(str, m)
            )
            return

        if self.style == "_traceback":
            # Show errors in red
            stderrColor = QColor(
                QgsSettings().value(
                    "pythonConsole/stderrFontColor", QColor(self.ERROR_COLOR)
                )
            )
            self.sO.SendScintilla(
                QsciScintilla.SCI_STYLESETFORE, self.ERROR_STYLE_INDEX, stderrColor
            )
            self.sO.SendScintilla(
                QsciScintilla.SCI_STYLESETITALIC, self.ERROR_STYLE_INDEX, True
            )
            self.sO.SendScintilla(
                QsciScintilla.SCI_STYLESETBOLD, self.ERROR_STYLE_INDEX, True
            )
            pos = self.sO.linearPosition()
            self.sO.SendScintilla(QsciScintilla.SCI_STARTSTYLING, pos, 0)
            self.sO.append(m)
            self.sO.SendScintilla(
                QsciScintilla.SCI_SETSTYLING, len(m), self.ERROR_STYLE_INDEX
            )

        else:
            self.sO.append(m)

        if self.out:
            self.out.write(m)

        self.sO.moveCursorToEnd()

        if self.style != "_traceback":
            self.sO.repaint()

        if self.fire_keyboard_interrupt:
            self.fire_keyboard_interrupt = False
            raise KeyboardInterrupt

    def flush(self):
        pass

    def isatty(self):
        return False


FULL_HELP_TEXT = QCoreApplication.translate(
    "PythonConsole",
    """QGIS Python Console
======================================

The console is a Python interpreter that allows you to execute python commands.
Modules from QGIS (analysis, core, gui, 3d) and Qt (QtCore, QtGui, QtNetwork,
QtWidgets, QtXml) as well as Python's math, os, re and sys modules are already
imported and can be used directly.

Useful variables:

- iface will return the current QGIS interface, class 'QgisInterface'
- iface.mainWindow() will return the Qt Main Window
- iface.mapCanvas() will return the map canvas
- iface.layerTreeView() will return the layer tree
- iface.activeLayer() will return the active layer
- QgsProject.instance() will return the current project

From the console, you can type the following special commands:

    - _pyqgis, _pyqgis(object): Open the QGIS Python API (or the Qt documentation) in a web browser
    - _api, _api(object): Open the QGIS C++ API (or the Qt documentation) in a web browser
    - _cookbook: Open the PyQGIS Developer Cookbook in a web browser
    - System commands: Any command starting with an exclamation mark (!) will be executed by the system shell. Examples:
        !gdalinfo --formats: List all available GDAL drivers
        !ogr2ogr --help: Show help for the ogr2ogr command
        !ping www.qgis.org: Ping the QGIS website
        !pip install black: install black python formatter using pip (if available)
    - ?: Show this help
""",
)


class ShellOutputScintilla(QgsCodeEditorPython):

    def __init__(
        self, console_widget: PythonConsoleWidget, shell_editor: ShellScintilla
    ):
        super().__init__(console_widget)
        self.console_widget: PythonConsoleWidget = console_widget
        self.shell_editor: ShellScintilla = shell_editor

        # Creates layout for message bar
        self.layout = QGridLayout(self)
        self.layout.setContentsMargins(0, 0, 0, 0)
        spacerItem = QSpacerItem(
            20, 40, QSizePolicy.Policy.Minimum, QSizePolicy.Policy.Expanding
        )
        self.layout.addItem(spacerItem, 1, 0, 1, 1)
        # messageBar instance
        self.infoBar = QgsMessageBar()
        sizePolicy = QSizePolicy(QSizePolicy.Policy.Minimum, QSizePolicy.Policy.Fixed)
        self.infoBar.setSizePolicy(sizePolicy)
        self.layout.addWidget(self.infoBar, 0, 0, 1, 1)

        self._old_stdout = sys.stdout
        self._old_stderr = sys.stderr

        sys.stdout = writeOut(self, sys.stdout)
        sys.stderr = writeOut(self, sys.stderr, "_traceback")

        QgsApplication.instance().aboutToQuit.connect(self.on_app_exit)

        self.insertInitText()
        self.refreshSettingsOutput()

        self.setMinimumHeight(120)

        self.setWrapMode(QsciScintilla.WrapMode.WrapCharacter)
        self.SendScintilla(QsciScintilla.SCI_SETHSCROLLBAR, 0)

        self.runScut = QShortcut(QKeySequence(Qt.Modifier.CTRL | Qt.Key.Key_E), self)
        self.runScut.setContext(Qt.ShortcutContext.WidgetShortcut)
        self.runScut.activated.connect(self.enteredSelected)
        # Reimplemented copy action to prevent paste prompt (>>>,...) in command view
        self.copyShortcut = QShortcut(QKeySequence.StandardKey.Copy, self)
        self.copyShortcut.setContext(Qt.ShortcutContext.WidgetWithChildrenShortcut)
        self.copyShortcut.activated.connect(self.copy)
        self.selectAllShortcut = QShortcut(QKeySequence.StandardKey.SelectAll, self)
        self.selectAllShortcut.setContext(Qt.ShortcutContext.WidgetWithChildrenShortcut)
        self.selectAllShortcut.activated.connect(self.selectAll)

    def on_app_exit(self):
        """
        Prepares the console for a graceful close
        """
        sys.stdout = self._old_stdout
        sys.stderr = self._old_stderr

    def insertInitText(self):
        txtInit = QCoreApplication.translate(
            "PythonConsole",
            "Python Console\n"
            "Use iface to access QGIS API interface or type '?' for more info\n"
            "Security warning: typing commands from an untrusted source can harm your computer",
        )

        txtInit = "\n".join(["# " + line for line in txtInit.split("\n")])

        # some translation string for the console header ends without '\n'
        # and the first command in console will be appended at the header text.
        # The following code add a '\n' at the end of the string if not present.
        if txtInit.endswith("\n"):
            self.setText(txtInit)
        else:
            self.setText(txtInit + "\n")

    def insertHelp(self):
        self.append(FULL_HELP_TEXT)
        self.moveCursorToEnd()

    def initializeLexer(self):
        super().initializeLexer()
        self.setFoldingVisible(False)
        self.setEdgeMode(QsciScintilla.EdgeMode.EdgeNone)

    def refreshSettingsOutput(self):
        # Set Python lexer
        self.initializeLexer()
        self.setReadOnly(True)

        self.setCaretWidth(0)  # NO (blinking) caret in the output

    def clearConsole(self):
        self.setText("")
        self.insertInitText()
        self.shell_editor.setFocus()

    def contextMenuEvent(self, e):
        menu = QMenu(self)
        menu.addAction(
            QgsApplication.getThemeIcon("console/iconHideToolConsole.svg"),
            QCoreApplication.translate("PythonConsole", "Hide/Show Toolbar"),
            self.hideToolBar,
        )
        menu.addSeparator()
        showEditorAction = menu.addAction(
            QgsApplication.getThemeIcon("console/iconShowEditorConsole.svg"),
            QCoreApplication.translate("PythonConsole", "Show Editor"),
            self.showEditor,
        )
        menu.addSeparator()
        runAction = QAction(
            QgsApplication.getThemeIcon("console/mIconRunConsole.svg"),
            QCoreApplication.translate("PythonConsole", "Enter Selected"),
            menu,
        )
        runAction.triggered.connect(self.enteredSelected)
        runAction.setShortcut(QKeySequence(Qt.Modifier.CTRL | Qt.Key.Key_E))
        menu.addAction(runAction)

        clearAction = QAction(
            QgsApplication.getThemeIcon("console/iconClearConsole.svg"),
            QCoreApplication.translate("PythonConsole", "Clear Console"),
            menu,
        )
        clearAction.triggered.connect(self.clearConsole)
        menu.addAction(clearAction)

        pyQGISHelpAction = QAction(
            QgsApplication.getThemeIcon("console/iconHelpConsole.svg"),
            QCoreApplication.translate(
                "PythonConsole", "Search Selection in PyQGIS Documentation"
            ),
            menu,
        )
        pyQGISHelpAction.triggered.connect(self.searchSelectedTextInPyQGISDocs)
        menu.addAction(pyQGISHelpAction)

        menu.addSeparator()
        copyAction = QAction(
            QgsApplication.getThemeIcon("mActionEditCopy.svg"),
            QCoreApplication.translate("PythonConsole", "Copy"),
            menu,
        )
        copyAction.triggered.connect(self.copy)
        copyAction.setShortcut(QKeySequence.StandardKey.Copy)
        menu.addAction(copyAction)

        selectAllAction = QAction(
            QCoreApplication.translate("PythonConsole", "Select All"), menu
        )
        selectAllAction.triggered.connect(self.selectAll)
        selectAllAction.setShortcut(QKeySequence.StandardKey.SelectAll)
        menu.addAction(selectAllAction)

        menu.addSeparator()
        settings_action = QAction(
            QgsApplication.getThemeIcon("console/iconSettingsConsole.svg"),
            QCoreApplication.translate("PythonConsole", "Options…"),
            menu,
        )
        settings_action.triggered.connect(self.console_widget.openSettings)
        menu.addAction(settings_action)

        runAction.setEnabled(False)
        clearAction.setEnabled(False)
        copyAction.setEnabled(False)
        pyQGISHelpAction.setEnabled(False)
        selectAllAction.setEnabled(False)
        showEditorAction.setEnabled(True)
        if self.hasSelectedText():
            runAction.setEnabled(True)
            copyAction.setEnabled(True)
            pyQGISHelpAction.setEnabled(True)
        if not self.text(3) == "":
            selectAllAction.setEnabled(True)
            clearAction.setEnabled(True)
        if self.console_widget.tabEditorWidget.isVisible():
            showEditorAction.setEnabled(False)
        menu.exec(self.mapToGlobal(e.pos()))

    def hideToolBar(self):
        tB = self.console_widget.toolBar
        tB.hide() if tB.isVisible() else tB.show()
        self.shell_editor.setFocus()

    def showEditor(self):
        Ed = self.console_widget.splitterObj
        if not Ed.isVisible():
            Ed.show()
            self.console_widget.showEditorButton.setChecked(True)
        self.shell_editor.setFocus()

    def copy(self):
        """Copy text to clipboard... or keyboard interrupt"""
        if self.hasSelectedText():
            text = self.selectedText()
            text = (
                text.replace(">>> ", "").replace("... ", "").strip()
            )  # removing prompts
            QApplication.clipboard().setText(text)
        else:
            raise KeyboardInterrupt

    def enteredSelected(self):
        cmd = self.selectedText()
        self.shell_editor.insertFromDropPaste(cmd)
        self.shell_editor.entered()

    def keyPressEvent(self, e):
        # empty text indicates possible shortcut key sequence so stay in output
        txt = e.text()
        if len(txt) and txt >= " ":
            self.shell_editor.append(txt)
            self.shell_editor.moveCursorToEnd()
            self.shell_editor.setFocus()
            e.ignore()
        else:
            # possible shortcut key sequence, accept it
            e.accept()

    def widgetMessageBar(self, text: str):
        self.infoBar.pushMessage(text, Qgis.MessageLevel.Info)