From e6f970e969a366eca3402375204223b2652e258e Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Wed, 26 Apr 2023 19:07:59 +1000 Subject: [PATCH] Tolerate missing keys in profile DB https://forums.ankiweb.net/t/crash-with-error-keyerror-mainwindowstate/29689 --- qt/aqt/browser/browser.py | 4 ++-- qt/aqt/main.py | 7 ++++--- qt/aqt/profiles.py | 4 ++-- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/qt/aqt/browser/browser.py b/qt/aqt/browser/browser.py index 574e803ad..81b4a303e 100644 --- a/qt/aqt/browser/browser.py +++ b/qt/aqt/browser/browser.py @@ -406,7 +406,7 @@ class Browser(QMainWindow): self.form.searchEdit.lineEdit().setPlaceholderText( tr.browsing_search_bar_hint() ) - self.form.searchEdit.addItems([""] + self.mw.pm.profile["searchHistory"]) + self.form.searchEdit.addItems([""] + self.mw.pm.profile.get("searchHistory")) if search is not None: self.search_for_terms(*search) else: @@ -451,7 +451,7 @@ class Browser(QMainWindow): showWarning(str(err)) def update_history(self) -> None: - sh = self.mw.pm.profile["searchHistory"] + sh = self.mw.pm.profile.get("searchHistory") if self._lastSearchTxt in sh: sh.remove(self._lastSearchTxt) sh.insert(0, self._lastSearchTxt) diff --git a/qt/aqt/main.py b/qt/aqt/main.py index 2c0023d9a..6d48d863d 100644 --- a/qt/aqt/main.py +++ b/qt/aqt/main.py @@ -476,7 +476,7 @@ class AnkiQt(QMainWindow): self.setup_sound() self.flags = FlagManager(self) # show main window - if self.pm.profile["mainWindowState"]: + if self.pm.profile.get("mainWindowState"): restoreGeom(self, "mainWindow") restoreState(self, "mainWindow") # titlebar @@ -687,8 +687,9 @@ class AnkiQt(QMainWindow): def maybeOptimize(self) -> None: # have two weeks passed? - if (int_time() - self.pm.profile["lastOptimize"]) < 86400 * 14: - return + if (last_optimize := self.pm.profile.get("lastOptimize")) is not None: + if (int_time() - last_optimize) < 86400 * 14: + return self.progress.start(label=tr.qt_misc_optimizing()) self.col.optimize() self.pm.profile["lastOptimize"] = int_time() diff --git a/qt/aqt/profiles.py b/qt/aqt/profiles.py index f7ad507e6..fa6a21bb5 100644 --- a/qt/aqt/profiles.py +++ b/qt/aqt/profiles.py @@ -632,10 +632,10 @@ create table if not exists profiles self.profile["hostNum"] = val or 0 def media_syncing_enabled(self) -> bool: - return self.profile["syncMedia"] + return self.profile.get("syncMedia") def auto_syncing_enabled(self) -> bool: - return self.profile["autoSync"] + return self.profile.get("autoSync") def sync_auth(self) -> SyncAuth | None: if not (hkey := self.profile.get("syncKey")):