File: E5ErrorMessage.py

package info (click to toggle)
eric 17.01-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 152,940 kB
  • ctags: 39,129
  • sloc: python: 293,469; xml: 229; makefile: 90; sh: 16
file content (208 lines) | stat: -rw-r--r-- 6,910 bytes parent folder | download
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
# -*- coding: utf-8 -*-

# Copyright (c) 2013 - 2017 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Module implementing a specialized error message dialog.
"""

from __future__ import unicode_literals

from PyQt5.QtCore import qInstallMessageHandler, QCoreApplication, \
    QtDebugMsg, QtWarningMsg, QtCriticalMsg, QtFatalMsg, QThread, \
    QMetaObject, Qt, Q_ARG, QSettings
from PyQt5.QtWidgets import QErrorMessage, qApp, QDialog

import Globals
import Utilities


__msgHandlerDialog = None
__origMsgHandler = None


class E5ErrorMessage(QErrorMessage):
    """
    Class implementing a specialized error message dialog.
    """
    def __init__(self, parent=None):
        """
        Constructor
        
        @param parent reference to the parent widget (QWidget)
        """
        super(E5ErrorMessage, self).__init__(parent)
        
        self.settings = QSettings(
            QSettings.IniFormat,
            QSettings.UserScope,
            Globals.settingsNameOrganization,
            "eric6messagefilters")
        
        self.__defaultFilters = [
            "QXcbConnection:",
            "QFont::",
            "QCocoaMenu::removeMenuItem",
            "QCocoaMenu::insertNative",
            ",type id:",
            "Remote debugging server started successfully",
            "Uncaught SecurityError:",
        ]
    
    def __filterMessage(self, message):
        """
        Private method to filter messages.
        
        @param message message to be checked (string)
        @return flag indicating that the message should be filtered out
            (boolean)
        """
        for filter in Globals.toList(self.settings.value(
                "MessageFilters", self.__defaultFilters)):
            if filter in message:
                return True
        
        return False
    
    def showMessage(self, message, msgType=""):
        """
        Public method to show a message.
        
        @param message error message to be shown (string)
        @param msgType type of the error message (string)
        """
        if not self.__filterMessage(message):
            if msgType:
                super(E5ErrorMessage, self).showMessage(message, msgType)
            else:
                super(E5ErrorMessage, self).showMessage(message)
    
    def editMessageFilters(self):
        """
        Public method to edit the list of message filters.
        """
        from .E5ErrorMessageFilterDialog import E5ErrorMessageFilterDialog
        dlg = E5ErrorMessageFilterDialog(
            self.settings.value("MessageFilters", self.__defaultFilters))
        if dlg.exec_() == QDialog.Accepted:
            filters = dlg.getFilters()
            self.settings.setValue("MessageFilters", filters)


def messageHandler(msgType, *args):
    """
    Module function handling messages.
    
    @param msgType type of the message (integer, QtMsgType)
    @param args message handler arguments, for PyQt4 message to be shown
        (bytes), for PyQt5 context information (QMessageLogContext) and
        message to be shown (bytes)
    """
    if len(args) == 2:
        context = args[0]
        message = args[1]
    else:
        message = args[0]
    if __msgHandlerDialog:
        try:
            if msgType == QtDebugMsg:
                messageType = QCoreApplication.translate(
                    "E5ErrorMessage", "Debug Message:")
            elif msgType == QtWarningMsg:
                messageType = QCoreApplication.translate(
                    "E5ErrorMessage", "Warning:")
            elif msgType == QtCriticalMsg:
                messageType = QCoreApplication.translate(
                    "E5ErrorMessage", "Critical:")
            elif msgType == QtFatalMsg:
                messageType = QCoreApplication.translate(
                    "E5ErrorMessage", "Fatal Error:")
            if isinstance(message, bytes):
                message = Utilities.decodeBytes(message)
            message = message.replace("\r\n", "<br/>")\
                             .replace("\n", "<br/>")\
                             .replace("\r", "<br/>")
            if len(args) == 2:
                msg = "<p><b>{0}</b></p><p>{1}</p><p>File: {2}</p>" \
                    "<p>Line: {3}</p><p>Function: {4}</p>".format(
                        messageType, Utilities.html_uencode(message),
                        context.file, context.line, context.function)
            else:
                msg = "<p><b>{0}</b></p><p>{1}</p>".format(
                    messageType, Utilities.html_uencode(message))
            if QThread.currentThread() == qApp.thread():
                __msgHandlerDialog.showMessage(msg)
            else:
                QMetaObject.invokeMethod(
                    __msgHandlerDialog,
                    "showMessage",
                    Qt.QueuedConnection,
                    Q_ARG(str, msg))
            return
        except RuntimeError:
            pass
    elif __origMsgHandler:
        __origMsgHandler(msgType, message)
        return
    
    if msgType == QtDebugMsg:
        messageType = QCoreApplication.translate(
            "E5ErrorMessage", "Debug Message")
    elif msgType == QtWarningMsg:
        messageType = QCoreApplication.translate(
            "E5ErrorMessage", "Warning")
    elif msgType == QtCriticalMsg:
        messageType = QCoreApplication.translate(
            "E5ErrorMessage", "Critical")
    elif msgType == QtFatalMsg:
        messageType = QCoreApplication.translate(
            "E5ErrorMessage", "Fatal Error")
    if isinstance(message, bytes):
        message = message.decode()
    if len(args) == 2:
        print("{0}: {1} in {2} at line {3} ({4})".format(
            messageType, message, context.file, context.line,
            context.function))
    else:
        print("{0}: {1}".format(messageType, message))


def qtHandler():
    """
    Module function to install an E5ErrorMessage dialog as the global
    message handler.
    
    @return reference to the message handler dialog (E5ErrorMessage)
    """
    global __msgHandlerDialog, __origMsgHandler
    
    if __msgHandlerDialog is None:
        # Install an E5ErrorMessage dialog as the global message handler.
        __msgHandlerDialog = E5ErrorMessage()
        __origMsgHandler = qInstallMessageHandler(messageHandler)
    
    return __msgHandlerDialog


def editMessageFilters():
    """
    Module function to edit the list of message filters.
    """
    if __msgHandlerDialog:
        __msgHandlerDialog.editMessageFilters()
    else:
        print(QCoreApplication.translate(
            "E5ErrorMessage", "No message handler installed."))


def messageHandlerInstalled():
    """
    Module function to check, if a message handler was installed.
    
    @return flag indicating an installed message handler (boolean)
    """
    return __msgHandlerDialog is not None

#
# eflag: noqa = M801