hook debug_ran_hook

This commit is contained in:
Arthur Milchior 2020-03-04 18:20:02 +01:00
parent 3319b114de
commit 5adbc33d4c
3 changed files with 45 additions and 2 deletions

View file

@ -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."""

View file

@ -1404,9 +1404,17 @@ will be lost. Continue?"""
else:
buf += "... %s\n" % line
try:
frm.log.appendPlainText(buf + (self._output or "<no output>"))
to_append = buf + (self._output or "<no output>")
to_append = gui_hooks.debug_console_did_evaluate_python(
to_append, text, frm
)
frm.log.appendPlainText(to_append)
except UnicodeDecodeError:
frm.log.appendPlainText(_("<non-unicode text>"))
to_append = _("<non-unicode text>")
to_append = gui_hooks.debug_console_did_evaluate_python(
to_append, text, frm
)
frm.log.appendPlainText(to_append)
frm.log.ensureCursorVisible()
# System specific code

View file

@ -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(