From 8dcd84e7c00dd844365e7d325a21344398966509 Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Mon, 14 Sep 2020 12:22:01 +0200 Subject: [PATCH] Add sync_will_start and sync_did_finish hook --- qt/aqt/gui_hooks.py | 54 ++++++++++++++++++++++++++++++++++++++++ qt/aqt/main.py | 2 ++ qt/tools/genhooks_gui.py | 2 ++ 3 files changed, 58 insertions(+) diff --git a/qt/aqt/gui_hooks.py b/qt/aqt/gui_hooks.py index 0ddb69c15..1ca99856a 100644 --- a/qt/aqt/gui_hooks.py +++ b/qt/aqt/gui_hooks.py @@ -2721,6 +2721,60 @@ class _StyleDidInitFilter: style_did_init = _StyleDidInitFilter() +class _SyncDidFinishHook: + _hooks: List[Callable[[], None]] = [] + + def append(self, cb: Callable[[], None]) -> None: + """()""" + self._hooks.append(cb) + + def remove(self, cb: Callable[[], None]) -> None: + if cb in self._hooks: + self._hooks.remove(cb) + + def count(self) -> int: + return len(self._hooks) + + def __call__(self) -> None: + for hook in self._hooks: + try: + hook() + except: + # if the hook fails, remove it + self._hooks.remove(hook) + raise + + +sync_did_finish = _SyncDidFinishHook() + + +class _SyncWillStartHook: + _hooks: List[Callable[[], None]] = [] + + def append(self, cb: Callable[[], None]) -> None: + """()""" + self._hooks.append(cb) + + def remove(self, cb: Callable[[], None]) -> None: + if cb in self._hooks: + self._hooks.remove(cb) + + def count(self) -> int: + return len(self._hooks) + + def __call__(self) -> None: + for hook in self._hooks: + try: + hook() + except: + # if the hook fails, remove it + self._hooks.remove(hook) + raise + + +sync_will_start = _SyncWillStartHook() + + class _TagEditorDidProcessKeyHook: _hooks: List[Callable[[TagEdit, QEvent], None]] = [] diff --git a/qt/aqt/main.py b/qt/aqt/main.py index 25be4c4ac..31f42d1e9 100644 --- a/qt/aqt/main.py +++ b/qt/aqt/main.py @@ -910,7 +910,9 @@ title="%s" %s>%s""" % ( self.col.models._clear_cache() self.reset() after_sync() + gui_hooks.sync_did_finish() + gui_hooks.sync_will_start() sync_collection(self, on_done=on_collection_sync_finished) def maybe_auto_sync_on_open_close(self, after_sync: Callable[[], None]) -> None: diff --git a/qt/tools/genhooks_gui.py b/qt/tools/genhooks_gui.py index 4dde50e32..dd2ed8f53 100644 --- a/qt/tools/genhooks_gui.py +++ b/qt/tools/genhooks_gui.py @@ -562,6 +562,8 @@ gui_hooks.webview_did_inject_style_into_page.append(mytest) args=["diag: aqt.emptycards.EmptyCardsDialog"], doc="""Allows changing the list of cards to delete.""", ), + Hook(name="sync_will_start", args=[]), + Hook(name="sync_did_finish", args=[]), # Adding cards ################### Hook(