From 9eabb6a9d2a6807595478647fd968e8e4bac86fc Mon Sep 17 00:00:00 2001 From: Glutanimate Date: Sun, 11 Aug 2019 08:03:49 +0200 Subject: [PATCH] Check for presence of import bug and take appropriate steps cf. https://anki.tenderapp.com/discussions/ankidesktop/34836 --- src/cloze_overlapper/__init__.py | 96 ++++++++++++++++++++++++++------ 1 file changed, 78 insertions(+), 18 deletions(-) diff --git a/src/cloze_overlapper/__init__.py b/src/cloze_overlapper/__init__.py index 62c2533..001fdbf 100644 --- a/src/cloze_overlapper/__init__.py +++ b/src/cloze_overlapper/__init__.py @@ -38,26 +38,86 @@ from ._version import __version__ # noqa: F401 -from .consts import ADDON -from .libaddon.consts import setAddonProperties -setAddonProperties(ADDON) +def checkFor2114ImportError(): + from .consts import ADDON + try: + # litmus test for Anki import bug + from .libaddon.platform import anki_version # noqa: F401 + return True + except ImportError: + # Disable add-on and inform user of the bug + from aqt.utils import showWarning + from aqt import mw + from anki import version as anki_version -from anki.hooks import addHook + mw.addonManager.toggleEnabled(__name__, enable=False) -from .gui.options_global import initializeOptions -from .gui import initializeQtResources -from .template import initializeModels -from .editor import initializeEditor -from .sched import initializeScheduler -from .reviewer import initializeReviewer + bug = "https://anki.tenderapp.com/discussions/ankidesktop/34836" + downloads = "https://apps.ankiweb.net#download" + beta = "https://apps.ankiweb.net/downloads/beta/" + vers = "2.1.15" + title = "Warning: {name} disabled".format(name=ADDON.NAME) + msg = ("WARNING: {name} had to be disabled because the " + "version of Anki that is currently installed on your system " + "({anki_version}) is incompatible with the add-on.

" + "Earlier releases of Anki like this one " + "suffer from a bug that breaks " + "{name} and many other add-ons on your system. " + "In order to fix this you will have to update Anki " + "to version {vers} or higher.

" + "As of writing this message, Anki {vers} is still in " + "beta testing, but that might have " + "changed in the meantime. Please check with the " + "releases page to see if {vers} " + "or a later release is available, otherwise download and " + "install the 2.1.15 beta here.

" + "After updating Anki, please re-enable " + "{name} by heading to Tools → Add-ons, selecting the " + "add-on, and clicking Toggle Enabled.".format( + name=ADDON.NAME, anki_version=anki_version, bug=bug, + vers=vers, downloads=downloads, beta=beta + )) -def delayedInit(): - initializeModels() - initializeScheduler() + showWarning(msg, title=title, textFormat="rich") -initializeQtResources() -addHook("profileLoaded", delayedInit) -initializeOptions() -initializeEditor() -initializeReviewer() + return False + + +def initializeAddon(): + """Initializes add-on after performing a few checks + + Allows more fine-grained control over add-on execution, which can + be helpful when implementing workarounds for Anki bugs (e.g. the module + import bug present in all Anki 2.1 versions up to 2.1.14) + """ + + if not checkFor2114ImportError(): + return False + + from .consts import ADDON + from .libaddon.consts import setAddonProperties + + setAddonProperties(ADDON) + + from anki.hooks import addHook + + from .gui.options_global import initializeOptions + from .gui import initializeQtResources + from .template import initializeModels + from .editor import initializeEditor + from .sched import initializeScheduler + from .reviewer import initializeReviewer + + def delayedInit(): + initializeModels() + initializeScheduler() + + addHook("profileLoaded", delayedInit) + + initializeQtResources() + initializeOptions() + initializeEditor() + initializeReviewer() + +initializeAddon() \ No newline at end of file