From 3319b114deb909c876dd1ecc00ea946071c36393 Mon Sep 17 00:00:00 2001 From: Arthur Milchior Date: Wed, 4 Mar 2020 18:11:13 +0100 Subject: [PATCH 1/2] hook debug_will_show_hook --- qt/aqt/gui_hooks.py | 29 ++++++++++++++++++++++++++++- qt/aqt/main.py | 1 + qt/tools/genhooks_gui.py | 9 +++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/qt/aqt/gui_hooks.py b/qt/aqt/gui_hooks.py index fded1c1c8..ff8721ed9 100644 --- a/qt/aqt/gui_hooks.py +++ b/qt/aqt/gui_hooks.py @@ -13,7 +13,7 @@ import anki import aqt from anki.cards import Card from anki.hooks import runFilter, runHook -from aqt.qt import QMenu +from aqt.qt import QDialog, QMenu # New hook/filter handling ############################################################################## @@ -520,6 +520,33 @@ class _CurrentNoteTypeDidChangeHook: current_note_type_did_change = _CurrentNoteTypeDidChangeHook() +class _DebugConsoleWillShowHook: + """Allows editing the debug window. E.g. setting a default code, or + previous code.""" + + _hooks: List[Callable[[QDialog], None]] = [] + + def append(self, cb: Callable[[QDialog], None]) -> None: + """(debug_window: QDialog)""" + self._hooks.append(cb) + + def remove(self, cb: Callable[[QDialog], None]) -> None: + if cb in self._hooks: + self._hooks.remove(cb) + + def __call__(self, debug_window: QDialog) -> None: + for hook in self._hooks: + try: + hook(debug_window) + except: + # if the hook fails, remove it + self._hooks.remove(hook) + raise + + +debug_console_will_show = _DebugConsoleWillShowHook() + + class _DeckBrowserDidRenderHook: """Allow to update the deck browser window. E.g. change its title.""" diff --git a/qt/aqt/main.py b/qt/aqt/main.py index 79ef51bf2..38e434dbf 100644 --- a/qt/aqt/main.py +++ b/qt/aqt/main.py @@ -1342,6 +1342,7 @@ will be lost. Continue?""" s.activated.connect(frm.log.clear) s = self.debugDiagShort = QShortcut(QKeySequence("ctrl+shift+l"), d) s.activated.connect(frm.text.clear) + gui_hooks.debug_console_will_show(d) d.show() def _captureOutput(self, on): diff --git a/qt/tools/genhooks_gui.py b/qt/tools/genhooks_gui.py index 1147c0653..be60a24b6 100644 --- a/qt/tools/genhooks_gui.py +++ b/qt/tools/genhooks_gui.py @@ -11,6 +11,7 @@ import sys pylib = os.path.join(os.path.dirname(__file__), "..", "..", "pylib") sys.path.append(pylib) + from tools.hookslib import Hook, update_file # Hook list @@ -89,6 +90,14 @@ hooks = [ legacy_hook="reviewCleanup", doc="Called before Anki transitions from the review screen to another screen.", ), + # Debug + ################### + Hook( + name="debug_console_will_show", + args=["debug_window: QDialog"], + doc="""Allows editing the debug window. E.g. setting a default code, or + previous code.""", + ), # Card layout ################### Hook( From 5adbc33d4c0fa726ef10c17e04a6561a960e838b Mon Sep 17 00:00:00 2001 From: Arthur Milchior Date: Wed, 4 Mar 2020 18:20:02 +0100 Subject: [PATCH 2/2] hook debug_ran_hook --- qt/aqt/gui_hooks.py | 28 ++++++++++++++++++++++++++++ qt/aqt/main.py | 12 ++++++++++-- qt/tools/genhooks_gui.py | 7 +++++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/qt/aqt/gui_hooks.py b/qt/aqt/gui_hooks.py index ff8721ed9..86fa5bdb3 100644 --- a/qt/aqt/gui_hooks.py +++ b/qt/aqt/gui_hooks.py @@ -520,6 +520,34 @@ class _CurrentNoteTypeDidChangeHook: current_note_type_did_change = _CurrentNoteTypeDidChangeHook() +class _DebugConsoleDidEvaluatePythonFilter: + """Allows processing the debug result. E.g. logging queries and + result, saving last query to display it later...""" + + _hooks: List[Callable[[str, str, QDialog], str]] = [] + + def append(self, cb: Callable[[str, str, QDialog], str]) -> None: + """(output: str, query: str, debug_window: QDialog)""" + self._hooks.append(cb) + + def remove(self, cb: Callable[[str, str, QDialog], str]) -> None: + if cb in self._hooks: + self._hooks.remove(cb) + + def __call__(self, output: str, query: str, debug_window: QDialog) -> str: + for filter in self._hooks: + try: + output = filter(output, query, debug_window) + except: + # if the hook fails, remove it + self._hooks.remove(filter) + raise + return output + + +debug_console_did_evaluate_python = _DebugConsoleDidEvaluatePythonFilter() + + class _DebugConsoleWillShowHook: """Allows editing the debug window. E.g. setting a default code, or previous code.""" diff --git a/qt/aqt/main.py b/qt/aqt/main.py index 38e434dbf..3f3034bb5 100644 --- a/qt/aqt/main.py +++ b/qt/aqt/main.py @@ -1404,9 +1404,17 @@ will be lost. Continue?""" else: buf += "... %s\n" % line try: - frm.log.appendPlainText(buf + (self._output or "")) + to_append = buf + (self._output or "") + to_append = gui_hooks.debug_console_did_evaluate_python( + to_append, text, frm + ) + frm.log.appendPlainText(to_append) except UnicodeDecodeError: - frm.log.appendPlainText(_("")) + to_append = _("") + to_append = gui_hooks.debug_console_did_evaluate_python( + to_append, text, frm + ) + frm.log.appendPlainText(to_append) frm.log.ensureCursorVisible() # System specific code diff --git a/qt/tools/genhooks_gui.py b/qt/tools/genhooks_gui.py index be60a24b6..e3049cd00 100644 --- a/qt/tools/genhooks_gui.py +++ b/qt/tools/genhooks_gui.py @@ -98,6 +98,13 @@ hooks = [ doc="""Allows editing the debug window. E.g. setting a default code, or previous code.""", ), + Hook( + name="debug_console_did_evaluate_python", + args=["output: str", "query: str", "debug_window: QDialog"], + return_type="str", + doc="""Allows processing the debug result. E.g. logging queries and + result, saving last query to display it later...""", + ), # Card layout ################### Hook(