|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# ----------------------------------------------------------------------------- |
| 3 | +# Copyright © SARDES Project Contributors |
| 4 | +# https://github.com/cgq-qgc/sardes |
| 5 | +# |
| 6 | +# This file is part of SARDES. |
| 7 | +# Licensed under the terms of the GNU General Public License. |
| 8 | +# ----------------------------------------------------------------------------- |
| 9 | + |
| 10 | +# ---- Standard library imports |
| 11 | +import sys |
| 12 | +import traceback |
| 13 | + |
| 14 | +# ---- Third party imports |
| 15 | +from qtpy.QtCore import QObject, Signal |
| 16 | +from qtpy.QtWidgets import QApplication |
| 17 | + |
| 18 | + |
| 19 | +class ExceptHook(QObject): |
| 20 | + """ |
| 21 | + A Qt object to catch exceptions and emit a formatted string of the error. |
| 22 | + """ |
| 23 | + sig_except_caught = Signal(str) |
| 24 | + |
| 25 | + def excepthook(self, exc_type, exc_value, exc_traceback): |
| 26 | + """Handle uncaught exceptions.""" |
| 27 | + sys.__excepthook__(exc_type, exc_value, exc_traceback) |
| 28 | + if not issubclass(exc_type, SystemExit): |
| 29 | + log_msg = ''.join(traceback.format_exception( |
| 30 | + exc_type, exc_value, exc_traceback)) |
| 31 | + self.sig_except_caught.emit(log_msg) |
| 32 | + |
| 33 | + |
| 34 | +class StandardStreamEmitter(QObject): |
| 35 | + """ |
| 36 | + A Qt object to intercept and emit the input and output of the |
| 37 | + Python interpreter. |
| 38 | +
|
| 39 | + https://docs.python.org/3/library/sys.html#sys.stdout |
| 40 | + https://docs.python.org/3/library/sys.html#sys.stderr |
| 41 | + """ |
| 42 | + sig_new_text = Signal(str) |
| 43 | + |
| 44 | + def write(self, text): |
| 45 | + try: |
| 46 | + sys.__stdout__.write(text) |
| 47 | + except Exception: |
| 48 | + pass |
| 49 | + self.sig_new_text.emit(str(text)) |
| 50 | + |
| 51 | + |
| 52 | +class SysCaptureManager(QObject): |
| 53 | + """ |
| 54 | + A manager to capture and manage Python's standard input and output |
| 55 | + streams, logging and internal errors reporting. |
| 56 | +
|
| 57 | + Important Note: |
| 58 | + the system capture manager should NOT be started when testing |
| 59 | + under pytest because this will cause problems with the way |
| 60 | + pytest is already capturing standard system messages and |
| 61 | + raised exceptions. |
| 62 | +
|
| 63 | + See pytest/src/_pytest/capture.py |
| 64 | + """ |
| 65 | + |
| 66 | + def __init__(self, start_capture=False): |
| 67 | + super().__init__() |
| 68 | + self._stdstream_stack = '' |
| 69 | + self._stdstream_consoles = [] |
| 70 | + self._is_capturing = False |
| 71 | + |
| 72 | + self.except_dialog = None |
| 73 | + |
| 74 | + # Setup the Except hook. |
| 75 | + self.except_hook = ExceptHook() |
| 76 | + self.except_hook.sig_except_caught.connect(self._handle_except) |
| 77 | + |
| 78 | + # Setup the standard stream emitter. |
| 79 | + self.stdout_emitter = StandardStreamEmitter() |
| 80 | + self.stdout_emitter.sig_new_text.connect(self.__handle_stdout) |
| 81 | + |
| 82 | + self.stderr_emitter = StandardStreamEmitter() |
| 83 | + self.stderr_emitter.sig_new_text.connect(self.handle_stderr) |
| 84 | + |
| 85 | + if start_capture: |
| 86 | + self.start_capture() |
| 87 | + |
| 88 | + def start_capture(self): |
| 89 | + """ |
| 90 | + Start capturing Python interpreter standard messages and unhandled |
| 91 | + raised exceptions. |
| 92 | +
|
| 93 | + Important Note: |
| 94 | + the system capture manager should NOT be started when testing |
| 95 | + under pytest because this will cause problems with the way |
| 96 | + pytest is already capturing standard system messages and |
| 97 | + raised exceptions. |
| 98 | +
|
| 99 | + See pytest/src/_pytest/capture.py |
| 100 | + """ |
| 101 | + self._is_capturing = True |
| 102 | + self.__orig_except_hook = sys.excepthook |
| 103 | + self.__orig_stdout = sys.stdout |
| 104 | + self.__orig_stderr = sys.stderr |
| 105 | + |
| 106 | + sys.excepthook = self.except_hook.excepthook |
| 107 | + sys.stdout = self.stdout_emitter |
| 108 | + sys.stderr = self.stderr_emitter |
| 109 | + |
| 110 | + def stop_capture(self): |
| 111 | + """ |
| 112 | + Stop capturing Python interpreter standard messages and unhandled |
| 113 | + raised exceptions. |
| 114 | + """ |
| 115 | + if self._is_capturing: |
| 116 | + self._is_capturing = False |
| 117 | + sys.excepthook = self.__orig_except_hook |
| 118 | + sys.stdout = self.__orig_stdout |
| 119 | + sys.stderr = self.__orig_stderr |
| 120 | + |
| 121 | + def register_stdstream_console(self, console): |
| 122 | + """ |
| 123 | + Register the specified console to this system capture manager. |
| 124 | + """ |
| 125 | + self._stdstream_consoles.append(console) |
| 126 | + console.write(self._stdstream_stack) |
| 127 | + |
| 128 | + def register_except_dialog(self, except_dialog): |
| 129 | + """ |
| 130 | + Register the system except dialog. |
| 131 | + """ |
| 132 | + self.except_dialog = except_dialog |
| 133 | + |
| 134 | + def handle_stderr(self, text): |
| 135 | + """ |
| 136 | + Handle Python interpreter standard errors. |
| 137 | + """ |
| 138 | + self._stdstream_stack += text |
| 139 | + for console in self._stdstream_consoles: |
| 140 | + console.write(text) |
| 141 | + |
| 142 | + def __handle_stdout(self, text): |
| 143 | + """ |
| 144 | + Handle Python interpreter standard output. |
| 145 | + """ |
| 146 | + self._stdstream_stack += text |
| 147 | + for console in self._stdstream_consoles: |
| 148 | + console.write(text) |
| 149 | + |
| 150 | + def _handle_except(self, log_msg): |
| 151 | + """ |
| 152 | + Handle raised exceptions that have not been handled properly |
| 153 | + internally and need to be reported for bug fixing. |
| 154 | + """ |
| 155 | + QApplication.restoreOverrideCursor() |
| 156 | + if self.except_dialog is not None: |
| 157 | + self.except_dialog.show_error( |
| 158 | + log_msg, self._stdstream_stack |
| 159 | + ) |
0 commit comments