From 700c6ae218c6aa7557fcaae526a70405b1790a54 Mon Sep 17 00:00:00 2001 From: oakkitten Date: Thu, 14 Apr 2022 00:42:13 +0100 Subject: [PATCH] Explicitly require Anki >= 2.1.45 Consolidate Anki version checks across files --- plugin/__init__.py | 28 +++++++++++----------------- plugin/edit.py | 4 ++-- tests/conftest.py | 4 ++-- tests/test_decks.py | 4 ---- 4 files changed, 15 insertions(+), 25 deletions(-) diff --git a/plugin/__init__.py b/plugin/__init__.py index b947a06..ed4c008 100644 --- a/plugin/__init__.py +++ b/plugin/__init__.py @@ -13,6 +13,13 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +import aqt + +anki_version = tuple(int(segment) for segment in aqt.appVersion.split(".")) + +if anki_version < (2, 1, 45): + raise Exception("Minimum Anki version supported: 2.1.45") + import base64 import glob import hashlib @@ -27,21 +34,15 @@ import unicodedata import anki import anki.exporting import anki.storage -import aqt from anki.cards import Card from anki.consts import MODEL_CLOZE from anki.exporting import AnkiPackageExporter from anki.importing import AnkiPackageImporter from anki.notes import Note +from anki.errors import NotFoundError from aqt.qt import Qt, QTimer, QMessageBox, QCheckBox from .edit import Edit - -try: - from anki.rsbackend import NotFoundError -except: - NotFoundError = Exception - from . import web, util @@ -50,8 +51,6 @@ from . import web, util # class AnkiConnect: - _anki21_version = int(aqt.appVersion.split('.')[-1]) - def __init__(self): self.log = None self.timer = None @@ -78,11 +77,7 @@ class AnkiConnect: ) def save_model(self, models, ankiModel): - if self._anki21_version < 45: - models.save(ankiModel, True) - models.flush() - else: - models.update_dict(ankiModel) + models.update_dict(ankiModel) def logEvent(self, name, data): if self.log is not None: @@ -541,9 +536,8 @@ class AnkiConnect: # however, since 62c23c6816adf912776b9378c008a52bb50b2e8d (2.1.45) # passing cardsToo to `rem` (long deprecated) won't raise an error! # this is dangerous, so let's raise our own exception - if self._anki21_version >= 28: - raise Exception("Since Anki 2.1.28 it's not possible " - "to delete decks without deleting cards as well") + raise Exception("Since Anki 2.1.28 it's not possible " + "to delete decks without deleting cards as well") try: self.startEditing() decks = filter(lambda d: d in self.deckNames(), decks) diff --git a/plugin/edit.py b/plugin/edit.py index 2ae9a89..a8c5ac9 100644 --- a/plugin/edit.py +++ b/plugin/edit.py @@ -8,6 +8,8 @@ from anki.errors import NotFoundError from anki.consts import QUEUE_TYPE_SUSPENDED from anki.utils import ids2str +from . import anki_version + # Edit dialog. Like Edit Current, but: # * has a Preview button to preview the cards for the note @@ -25,8 +27,6 @@ from anki.utils import ids2str DOMAIN_PREFIX = "foosoft.ankiconnect." -anki_version = tuple(int(segment) for segment in aqt.appVersion.split(".")) - def get_note_by_note_id(note_id): return aqt.mw.col.get_note(note_id) diff --git a/tests/conftest.py b/tests/conftest.py index cb26734..208abe2 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -10,7 +10,7 @@ from _pytest.monkeypatch import MonkeyPatch # noqa from pytest_anki._launch import anki_running, temporary_user # noqa from waitress import wasyncore -from plugin import AnkiConnect +from plugin import AnkiConnect, anki_version from plugin.edit import Edit from plugin.util import DEFAULT_CONFIG @@ -85,7 +85,7 @@ def waitress_patched_to_prevent_it_from_dying(): @contextmanager def anki_patched_to_prevent_backups(): with MonkeyPatch().context() as monkey: - if ac._anki21_version < 50: + if anki_version < (2, 1, 50): monkey.setitem(aqt.profiles.profileConf, "numBackups", 0) else: monkey.setattr(anki.collection.Collection, "create_backup", diff --git a/tests/test_decks.py b/tests/test_decks.py index 04e6107..c388b03 100755 --- a/tests/test_decks.py +++ b/tests/test_decks.py @@ -30,10 +30,6 @@ def test_deleteDeck(setup): assert {*before} - {*after} == {"test_deck"} -@pytest.mark.skipif( - condition=ac._anki21_version < 28, - reason=f"Not applicable to Anki < 2.1.28" -) def test_deleteDeck_must_be_called_with_cardsToo_set_to_True_on_later_api(setup): with pytest.raises(Exception): ac.deleteDecks(decks=["test_deck"])