Tolerate missing keys in profile DB

https://forums.ankiweb.net/t/crash-with-error-keyerror-mainwindowstate/29689
This commit is contained in:
Damien Elmes 2023-04-26 19:07:59 +10:00
parent c12689160f
commit e6f970e969
3 changed files with 8 additions and 7 deletions

View file

@ -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)

View file

@ -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()

View file

@ -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")):