From 8dcd84e7c00dd844365e7d325a21344398966509 Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Mon, 14 Sep 2020 12:22:01 +0200 Subject: [PATCH 1/4] 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( From 6e5a8269931cc8e8d59070629e59eaf5f201b5f4 Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Mon, 14 Sep 2020 13:06:20 +0200 Subject: [PATCH 2/4] Put sync_did_finish hook before after_sync because it might unload collection --- qt/aqt/main.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/qt/aqt/main.py b/qt/aqt/main.py index 31f42d1e9..912bdc4c7 100644 --- a/qt/aqt/main.py +++ b/qt/aqt/main.py @@ -909,9 +909,10 @@ title="%s" %s>%s""" % ( self.col.clearUndo() self.col.models._clear_cache() self.reset() - after_sync() gui_hooks.sync_did_finish() + after_sync() + gui_hooks.sync_will_start() sync_collection(self, on_done=on_collection_sync_finished) From 0c7caa1644852b742fb2611a4700a5bb4ebe902e Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Mon, 14 Sep 2020 13:18:16 +0200 Subject: [PATCH 3/4] Add some documentation to sync_did_finish hook --- qt/aqt/gui_hooks.py | 4 ++++ qt/tools/genhooks_gui.py | 8 +++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/qt/aqt/gui_hooks.py b/qt/aqt/gui_hooks.py index 1ca99856a..265f3de98 100644 --- a/qt/aqt/gui_hooks.py +++ b/qt/aqt/gui_hooks.py @@ -2722,6 +2722,10 @@ style_did_init = _StyleDidInitFilter() class _SyncDidFinishHook: + """Executes after the sync of the collection concluded. + + Note that the media sync did not necessarily finish at this point.""" + _hooks: List[Callable[[], None]] = [] def append(self, cb: Callable[[], None]) -> None: diff --git a/qt/tools/genhooks_gui.py b/qt/tools/genhooks_gui.py index dd2ed8f53..41eb9382c 100644 --- a/qt/tools/genhooks_gui.py +++ b/qt/tools/genhooks_gui.py @@ -563,7 +563,13 @@ gui_hooks.webview_did_inject_style_into_page.append(mytest) doc="""Allows changing the list of cards to delete.""", ), Hook(name="sync_will_start", args=[]), - Hook(name="sync_did_finish", args=[]), + Hook( + name="sync_did_finish", + args=[], + doc="""Executes after the sync of the collection concluded. + + Note that the media sync did not necessarily finish at this point.""", + ), # Adding cards ################### Hook( From f147e9014619e6b5c26f2db6e6b9cb53288078d0 Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Tue, 15 Sep 2020 13:06:11 +0200 Subject: [PATCH 4/4] Move sync_did_finish before reset --- qt/aqt/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qt/aqt/main.py b/qt/aqt/main.py index 912bdc4c7..a20ea514e 100644 --- a/qt/aqt/main.py +++ b/qt/aqt/main.py @@ -908,8 +908,8 @@ title="%s" %s>%s""" % ( def on_collection_sync_finished(): self.col.clearUndo() self.col.models._clear_cache() - self.reset() gui_hooks.sync_did_finish() + self.reset() after_sync()