Explicitly require Anki >= 2.1.45

Consolidate Anki version checks across files
This commit is contained in:
oakkitten 2022-04-14 00:42:13 +01:00
parent 056e722187
commit 700c6ae218
4 changed files with 15 additions and 25 deletions

View File

@ -13,6 +13,13 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
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)

View File

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

View File

@ -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",

View File

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