From 6f4312f508e0f8f1e93843bb3b623934a6d5d478 Mon Sep 17 00:00:00 2001 From: rameauv Date: Fri, 6 Jan 2023 17:55:51 +0900 Subject: [PATCH] Handle toolbar stats loading in Anki 2.1.54-qt5 (#2) (#280) Handle the case where the toolbar stats are loaded before the profile. Since Anki 2.1.50, the "top_toolbar_did_init_links" hook is triggered before the profile is loaded, which raises an error because we need the profile to be loaded to be able to retrieve the stats. --- morph/errors/profileNotYetLoadedException.py | 3 +++ morph/preferences.py | 5 ++++- morph/stats.py | 3 ++- 3 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 morph/errors/profileNotYetLoadedException.py diff --git a/morph/errors/profileNotYetLoadedException.py b/morph/errors/profileNotYetLoadedException.py new file mode 100644 index 00000000..fc63cad8 --- /dev/null +++ b/morph/errors/profileNotYetLoadedException.py @@ -0,0 +1,3 @@ +class ProfileNotYetLoadedException(Exception): + "Raised when the profile is not yet loaded" + pass \ No newline at end of file diff --git a/morph/preferences.py b/morph/preferences.py index ea4d5369..d89e19d7 100644 --- a/morph/preferences.py +++ b/morph/preferences.py @@ -2,6 +2,8 @@ import importlib from aqt import mw +from .errors.profileNotYetLoadedException import ProfileNotYetLoadedException + # retrieving the configuration using get_config is very expensive operation # instead, save it config_data = None @@ -64,7 +66,8 @@ def _init_config_py(): def _get_config_py_preference(key, modelId=None, deckId=None): - assert config_py, 'Tried to use cfgMods before profile loaded' + if config_py == None: + raise ProfileNotYetLoadedException("Tried to use cfgMods before profile loaded") profile = mw.pm.name model = mw.col.models.get(modelId)['name'] if modelId else None deck = mw.col.decks.get(deckId)['name'] if deckId else None diff --git a/morph/stats.py b/morph/stats.py index d471a4d0..9b1adc57 100644 --- a/morph/stats.py +++ b/morph/stats.py @@ -10,6 +10,7 @@ from .util import mw from .preferences import get_preference as cfg +from .errors.profileNotYetLoadedException import ProfileNotYetLoadedException def getStatsPath(): return cfg('path_stats') @@ -22,7 +23,7 @@ def loadStats(): return d except IOError: # file DNE => create it return updateStats() - except AssertionError: # profile not loaded yet, can't do anything but wait + except ProfileNotYetLoadedException: # profile not loaded yet, can't do anything but wait return None