2019-02-05 04:59:03 +01:00
|
|
|
# Copyright: Ankitects Pty Ltd and contributors
|
2012-12-21 08:51:59 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
2017-12-11 07:20:00 +01:00
|
|
|
import html
|
2019-02-17 00:35:44 +01:00
|
|
|
import re
|
2019-12-20 10:19:03 +01:00
|
|
|
import sys
|
|
|
|
import traceback
|
2021-02-01 13:08:56 +01:00
|
|
|
from typing import Optional
|
2013-10-22 08:30:46 +02:00
|
|
|
|
2020-02-23 09:01:34 +01:00
|
|
|
from markdown import markdown
|
|
|
|
|
2019-12-20 10:19:03 +01:00
|
|
|
from aqt import mw
|
2021-02-01 13:08:56 +01:00
|
|
|
from aqt.main import AnkiQt
|
2013-10-22 08:30:46 +02:00
|
|
|
from aqt.qt import *
|
2020-02-27 03:25:19 +01:00
|
|
|
from aqt.utils import TR, showText, showWarning, supportText, tr
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2017-01-08 11:30:34 +01:00
|
|
|
if not os.environ.get("DEBUG"):
|
2019-12-23 01:34:10 +01:00
|
|
|
|
|
|
|
def excepthook(etype, val, tb):
|
|
|
|
sys.stderr.write(
|
|
|
|
"Caught exception:\n%s\n"
|
|
|
|
% ("".join(traceback.format_exception(etype, val, tb)))
|
|
|
|
)
|
|
|
|
|
2017-01-08 11:30:34 +01:00
|
|
|
sys.excepthook = excepthook
|
2016-05-31 10:51:40 +02:00
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
class ErrorHandler(QObject):
|
|
|
|
"Catch stderr and write into buffer."
|
|
|
|
ivl = 100
|
|
|
|
|
2016-05-31 10:51:40 +02:00
|
|
|
errorTimer = pyqtSignal()
|
|
|
|
|
2021-02-01 13:08:56 +01:00
|
|
|
def __init__(self, mw: AnkiQt) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
QObject.__init__(self, mw)
|
|
|
|
self.mw = mw
|
2021-02-01 13:08:56 +01:00
|
|
|
self.timer: Optional[QTimer] = None
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(self.errorTimer, self._setTimer)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.pool = ""
|
2017-06-26 05:03:05 +02:00
|
|
|
self._oldstderr = sys.stderr
|
2012-12-21 08:51:59 +01:00
|
|
|
sys.stderr = self
|
|
|
|
|
2021-02-01 13:08:56 +01:00
|
|
|
def unload(self) -> None:
|
2017-06-26 05:03:05 +02:00
|
|
|
sys.stderr = self._oldstderr
|
|
|
|
sys.excepthook = None
|
|
|
|
|
2021-02-01 13:08:56 +01:00
|
|
|
def write(self, data: str) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
# dump to stdout
|
2016-05-12 06:45:35 +02:00
|
|
|
sys.stdout.write(data)
|
2012-12-21 08:51:59 +01:00
|
|
|
# save in buffer
|
|
|
|
self.pool += data
|
|
|
|
# and update timer
|
|
|
|
self.setTimer()
|
|
|
|
|
2021-02-01 13:08:56 +01:00
|
|
|
def setTimer(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
# we can't create a timer from a different thread, so we post a
|
|
|
|
# message to the object on the main thread
|
2020-07-24 19:19:40 +02:00
|
|
|
self.errorTimer.emit() # type: ignore
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-02-01 13:08:56 +01:00
|
|
|
def _setTimer(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
if not self.timer:
|
|
|
|
self.timer = QTimer(self.mw)
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(self.timer.timeout, self.onTimeout)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.timer.setInterval(self.ivl)
|
|
|
|
self.timer.setSingleShot(True)
|
|
|
|
self.timer.start()
|
|
|
|
|
2013-10-22 08:30:46 +02:00
|
|
|
def tempFolderMsg(self):
|
2020-11-18 02:32:22 +01:00
|
|
|
return tr(TR.QT_MISC_UNABLE_TO_ACCESS_ANKI_MEDIA_FOLDER)
|
2013-10-22 08:30:46 +02:00
|
|
|
|
2021-02-01 13:08:56 +01:00
|
|
|
def onTimeout(self) -> None:
|
2017-12-11 07:20:00 +01:00
|
|
|
error = html.escape(self.pool)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.pool = ""
|
|
|
|
self.mw.progress.clear()
|
|
|
|
if "abortSchemaMod" in error:
|
|
|
|
return
|
2020-12-16 05:25:11 +01:00
|
|
|
if "DeprecationWarning" in error:
|
|
|
|
return
|
2017-07-09 05:26:50 +02:00
|
|
|
if "10013" in error:
|
2021-02-01 13:08:56 +01:00
|
|
|
showWarning(tr(TR.QT_MISC_YOUR_FIREWALL_OR_ANTIVIRUS_PROGRAM_IS))
|
|
|
|
return
|
2016-05-31 10:51:40 +02:00
|
|
|
if "no default input" in error.lower():
|
2021-02-01 13:08:56 +01:00
|
|
|
showWarning(tr(TR.QT_MISC_PLEASE_CONNECT_A_MICROPHONE_AND_ENSURE))
|
|
|
|
return
|
2013-10-22 08:30:46 +02:00
|
|
|
if "invalidTempFolder" in error:
|
2021-02-01 13:08:56 +01:00
|
|
|
showWarning(self.tempFolderMsg())
|
|
|
|
return
|
2016-07-04 10:06:08 +02:00
|
|
|
if "Beautiful Soup is not an HTTP client" in error:
|
|
|
|
return
|
2019-04-23 02:19:05 +02:00
|
|
|
if "database or disk is full" in error or "Errno 28" in error:
|
2021-02-01 13:08:56 +01:00
|
|
|
showWarning(tr(TR.QT_MISC_YOUR_COMPUTERS_STORAGE_MAY_BE_FULL))
|
|
|
|
return
|
2014-07-22 01:01:19 +02:00
|
|
|
if "disk I/O error" in error:
|
2020-02-27 03:25:19 +01:00
|
|
|
showWarning(markdown(tr(TR.ERRORS_ACCESSING_DB)))
|
2020-02-23 09:01:34 +01:00
|
|
|
return
|
2017-09-06 08:40:35 +02:00
|
|
|
|
2017-08-26 07:14:20 +02:00
|
|
|
if self.mw.addonManager.dirty:
|
2020-02-27 03:25:19 +01:00
|
|
|
txt = markdown(tr(TR.ERRORS_ADDONS_ACTIVE_POPUP))
|
2019-02-24 14:50:39 +01:00
|
|
|
error = supportText() + self._addonText(error) + "\n" + error
|
2012-12-21 08:51:59 +01:00
|
|
|
else:
|
2020-02-27 03:25:19 +01:00
|
|
|
txt = markdown(tr(TR.ERRORS_STANDARD_POPUP))
|
2019-02-24 14:50:39 +01:00
|
|
|
error = supportText() + "\n" + error
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# show dialog
|
|
|
|
txt = txt + "<div style='white-space: pre-wrap'>" + error + "</div>"
|
2019-02-16 23:05:06 +01:00
|
|
|
showText(txt, type="html", copyBtn=True)
|
2017-09-06 08:40:35 +02:00
|
|
|
|
2019-02-17 00:35:44 +01:00
|
|
|
def _addonText(self, error):
|
|
|
|
matches = re.findall(r"addons21/(.*?)/", error)
|
|
|
|
if not matches:
|
|
|
|
return ""
|
|
|
|
# reverse to list most likely suspect first, dict to deduplicate:
|
2019-12-23 01:34:10 +01:00
|
|
|
addons = [
|
|
|
|
mw.addonManager.addonName(i) for i in dict.fromkeys(reversed(matches))
|
|
|
|
]
|
2019-02-17 00:35:44 +01:00
|
|
|
# highlight importance of first add-on:
|
|
|
|
addons[0] = "<b>{}</b>".format(addons[0])
|
2020-02-27 11:32:57 +01:00
|
|
|
addons_str = ", ".join(addons)
|
|
|
|
return tr(TR.ADDONS_POSSIBLY_INVOLVED, addons=addons_str) + "\n"
|