Skip to content

Commit

Permalink
Handle toolbar stats loading in Anki 2.1.54-qt5 (#2) (#280)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rameauv authored Jan 6, 2023
1 parent 444f18f commit 6f4312f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
3 changes: 3 additions & 0 deletions morph/errors/profileNotYetLoadedException.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class ProfileNotYetLoadedException(Exception):
"Raised when the profile is not yet loaded"
pass
5 changes: 4 additions & 1 deletion morph/preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion morph/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -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


Expand Down

0 comments on commit 6f4312f

Please sign in to comment.