Replace method calls of methods marked deprecated

There are more methods that have replacements according to the common pattern of
going from camelCase to snake_case, but that are not explicitly marked
deprecated.

Reference was tag 2.1.45 of Anki, as that is the minimally supported version of
anki-connect as of creating this commit.
This commit is contained in:
Philipp Matthias Schäfer 2024-01-21 15:31:21 +01:00 committed by Alex Yatskov
parent 977871257a
commit 510f47fd08

View File

@ -190,14 +190,14 @@ class AnkiConnect:
def getModel(self, modelName):
model = self.collection().models.byName(modelName)
model = self.collection().models.by_name(modelName)
if model is None:
raise Exception('model was not found: {}'.format(modelName))
return model
def getField(self, model, fieldName):
fieldMap = self.collection().models.fieldMap(model)
fieldMap = self.collection().models.field_map(model)
if fieldName not in fieldMap:
raise Exception('field was not found in {}: {}'.format(model['name'], fieldName))
return fieldMap[fieldName][1]
@ -217,16 +217,16 @@ class AnkiConnect:
def createNote(self, note):
collection = self.collection()
model = collection.models.byName(note['modelName'])
model = collection.models.by_name(note['modelName'])
if model is None:
raise Exception('model was not found: {}'.format(note['modelName']))
deck = collection.decks.byName(note['deckName'])
deck = collection.decks.by_name(note['deckName'])
if deck is None:
raise Exception('deck was not found: {}'.format(note['deckName']))
ankiNote = anki.notes.Note(collection, model)
ankiNote.model()['did'] = deck['id']
ankiNote.note_type()['did'] = deck['id']
if 'tags' in note:
ankiNote.tags = note['tags']
@ -316,7 +316,7 @@ class AnkiConnect:
if duplicateScope == 'deck':
did = deck['id']
if duplicateScopeDeckName is not None:
deck2 = collection.decks.byName(duplicateScopeDeckName)
deck2 = collection.decks.by_name(duplicateScopeDeckName)
if deck2 is None:
# Invalid deck, so cannot be duplicate
return 0
@ -512,7 +512,7 @@ class AnkiConnect:
@util.api()
def deckNames(self):
return self.decks().allNames()
return [x.name for x in self.decks().all_names_and_ids()]
@util.api()
@ -576,7 +576,7 @@ class AnkiConnect:
decks = filter(lambda d: d in self.deckNames(), decks)
for deck in decks:
did = self.decks().id(deck)
self.decks().rem(did, cardsToo=cardsToo)
self.decks().remove([did])
@util.api()
@ -586,7 +586,7 @@ class AnkiConnect:
collection = self.collection()
did = collection.decks.id(deck)
return collection.decks.confForDid(did)
return collection.decks.config_dict_for_deck_id(did)
@util.api()
@ -600,7 +600,7 @@ class AnkiConnect:
return False
try:
collection.decks.save(config)
collection.decks.updateConf(config)
collection.decks.update_config(config)
except:
return False
return True
@ -634,8 +634,8 @@ class AnkiConnect:
if configId not in [c['id'] for c in collection.decks.all_config()]:
return False
config = collection.decks.getConf(configId)
return collection.decks.confId(name, config)
config = collection.decks.get_config(configId)
return collection.decks.add_config_returning_id(name, config)
@util.api()
@ -644,7 +644,7 @@ class AnkiConnect:
if int(configId) not in [c['id'] for c in collection.decks.all_config()]:
return False
collection.decks.remConf(configId)
collection.decks.remove_config(configId)
return True
@util.api()
@ -870,9 +870,9 @@ class AnkiConnect:
except NotFoundError:
continue
if note.hasTag(tag_to_replace):
note.delTag(tag_to_replace)
note.addTag(replace_with_tag)
if note.has_tag(tag_to_replace):
note.remove_tag(tag_to_replace)
note.add_tag(replace_with_tag)
note.flush()
self.window().requireReset()
@ -887,9 +887,9 @@ class AnkiConnect:
collection = self.collection()
for nid in collection.db.list('select id from notes'):
note = self.getNote(nid)
if note.hasTag(tag_to_replace):
note.delTag(tag_to_replace)
note.addTag(replace_with_tag)
if note.has_tag(tag_to_replace):
note.remove_tag(tag_to_replace)
note.add_tag(replace_with_tag)
note.flush()
self.window().requireReset()
@ -1037,7 +1037,7 @@ class AnkiConnect:
@util.api()
def modelNames(self):
return self.collection().models.allNames()
return [n.name for n in self.collection().models.all_names_and_ids()]
@util.api()
@ -1047,7 +1047,7 @@ class AnkiConnect:
raise Exception('Must provide at least one field for inOrderFields')
if len(cardTemplates) == 0:
raise Exception('Must provide at least one card for cardTemplates')
if modelName in self.collection().models.allNames():
if modelName in [n.name for n in self.collection().models.all_names_and_ids()]:
raise Exception('Model name already exists')
collection = self.collection()
@ -1060,7 +1060,7 @@ class AnkiConnect:
# Create fields and add them to Note
for field in inOrderFields:
fm = mm.newField(field)
fm = mm.new_field(field)
mm.addField(m, fm)
# Add shared css to model if exists. Use default otherwise
@ -1074,7 +1074,7 @@ class AnkiConnect:
if 'Name' in card:
cardName = card['Name']
t = mm.newTemplate(cardName)
t = mm.new_template(cardName)
cardCount += 1
t['qfmt'] = card['Front']
t['afmt'] = card['Back']
@ -1088,7 +1088,7 @@ class AnkiConnect:
def modelNamesAndIds(self):
models = {}
for model in self.modelNames():
models[model] = int(self.collection().models.byName(model)['id'])
models[model] = int(self.collection().models.by_name(model)['id'])
return models
@ -1108,7 +1108,7 @@ class AnkiConnect:
def findModelsByName(self, modelNames):
models = []
for name in modelNames:
model = self.collection().models.byName(name)
model = self.collection().models.by_name(name)
if model is None:
raise Exception("model was not found: {}".format(name))
else:
@ -1126,7 +1126,7 @@ class AnkiConnect:
@util.api()
def modelFieldNames(self, modelName):
model = self.collection().models.byName(modelName)
model = self.collection().models.by_name(modelName)
if model is None:
raise Exception('model was not found: {}'.format(modelName))
else:
@ -1135,7 +1135,7 @@ class AnkiConnect:
@util.api()
def modelFieldDescriptions(self, modelName):
model = self.collection().models.byName(modelName)
model = self.collection().models.by_name(modelName)
if model is None:
raise Exception('model was not found: {}'.format(modelName))
else:
@ -1163,7 +1163,7 @@ class AnkiConnect:
@util.api()
def modelFieldsOnTemplates(self, modelName):
model = self.collection().models.byName(modelName)
model = self.collection().models.by_name(modelName)
if model is None:
raise Exception('model was not found: {}'.format(modelName))
@ -1194,7 +1194,7 @@ class AnkiConnect:
@util.api()
def modelTemplates(self, modelName):
model = self.collection().models.byName(modelName)
model = self.collection().models.by_name(modelName)
if model is None:
raise Exception('model was not found: {}'.format(modelName))
@ -1207,7 +1207,7 @@ class AnkiConnect:
@util.api()
def modelStyling(self, modelName):
model = self.collection().models.byName(modelName)
model = self.collection().models.by_name(modelName)
if model is None:
raise Exception('model was not found: {}'.format(modelName))
@ -1217,7 +1217,7 @@ class AnkiConnect:
@util.api()
def updateModelTemplates(self, model):
models = self.collection().models
ankiModel = models.byName(model['name'])
ankiModel = models.by_name(model['name'])
if ankiModel is None:
raise Exception('model was not found: {}'.format(model['name']))
@ -1239,7 +1239,7 @@ class AnkiConnect:
@util.api()
def updateModelStyling(self, model):
models = self.collection().models
ankiModel = models.byName(model['name'])
ankiModel = models.by_name(model['name'])
if ankiModel is None:
raise Exception('model was not found: {}'.format(model['name']))
@ -1253,13 +1253,13 @@ class AnkiConnect:
if not modelName:
ankiModel = self.collection().models.allNames()
else:
model = self.collection().models.byName(modelName)
model = self.collection().models.by_name(modelName)
if model is None:
raise Exception('model was not found: {}'.format(modelName))
ankiModel = [modelName]
updatedModels = 0
for model in ankiModel:
model = self.collection().models.byName(model)
model = self.collection().models.by_name(model)
checkForText = False
if css and findText in model['css']:
checkForText = True
@ -1348,7 +1348,7 @@ class AnkiConnect:
model = self.getModel(modelName)
field = self.getField(model, fieldName)
mm.repositionField(model, field, index)
mm.reposition_field(model, field, index)
self.save_model(mm, model)
@ -1359,16 +1359,16 @@ class AnkiConnect:
model = self.getModel(modelName)
# only adds the field if it doesn't already exist
fieldMap = mm.fieldMap(model)
fieldMap = mm.field_map(model)
if fieldName not in fieldMap:
field = mm.newField(fieldName)
field = mm.new_field(fieldName)
mm.addField(model, field)
# repositions, even if the field already exists
if index is not None:
fieldMap = mm.fieldMap(model)
fieldMap = mm.field_map(model)
newField = fieldMap[fieldName][1]
mm.repositionField(model, newField, index)
mm.reposition_field(model, newField, index)
self.save_model(mm, model)
@ -1379,7 +1379,7 @@ class AnkiConnect:
model = self.getModel(modelName)
field = self.getField(model, fieldName)
mm.removeField(model, field)
mm.remove_field(model, field)
self.save_model(mm, model)
@ -1442,7 +1442,7 @@ class AnkiConnect:
if query is None:
return []
return list(map(int, self.collection().findNotes(query)))
return list(map(int, self.collection().find_notes(query)))
@util.api()
@ -1450,7 +1450,7 @@ class AnkiConnect:
if query is None:
return []
return list(map(int, self.collection().findCards(query)))
return list(map(int, self.collection().find_cards(query)))
@util.api()
@ -1459,7 +1459,7 @@ class AnkiConnect:
for cid in cards:
try:
card = self.getCard(cid)
model = card.model()
model = card.note_type()
note = card.note()
fields = {}
for info in model['flds']:
@ -1602,7 +1602,7 @@ class AnkiConnect:
for nid in notes:
try:
note = self.getNote(nid)
model = note.model()
model = note.note_type()
fields = {}
for info in model['flds']:
@ -1629,14 +1629,14 @@ class AnkiConnect:
@util.api()
def deleteNotes(self, notes):
self.collection().remNotes(notes)
self.collection().remove_notes(notes)
@util.api()
def removeEmptyNotes(self):
for model in self.collection().models.all():
if self.collection().models.useCount(model) == 0:
self.collection().models.rem(model)
if self.collection().models.use_count(model) == 0:
self.collection().models.remove(model["id"])
self.window().requireReset()
@ -1702,18 +1702,18 @@ class AnkiConnect:
if note is not None:
collection = self.collection()
deck = collection.decks.byName(note['deckName'])
deck = collection.decks.by_name(note['deckName'])
if deck is None:
raise Exception('deck was not found: {}'.format(note['deckName']))
collection.decks.select(deck['id'])
savedMid = deck.pop('mid', None)
model = collection.models.byName(note['modelName'])
model = collection.models.by_name(note['modelName'])
if model is None:
raise Exception('model was not found: {}'.format(note['modelName']))
collection.models.setCurrent(model)
collection.models.set_current(model)
collection.models.update(model)
ankiNote = anki.notes.Note(collection, model)
@ -1772,7 +1772,7 @@ class AnkiConnect:
reviewer = self.reviewer()
card = reviewer.card
model = card.model()
model = card.note_type()
note = card.note()
fields = {}
@ -1853,7 +1853,7 @@ class AnkiConnect:
def guiDeckOverview(self, name):
collection = self.collection()
if collection is not None:
deck = collection.decks.byName(name)
deck = collection.decks.by_name(name)
if deck is not None:
collection.decks.select(deck['id'])
self.window().onOverview()
@ -1956,7 +1956,7 @@ class AnkiConnect:
def exportPackage(self, deck, path, includeSched=False):
collection = self.collection()
if collection is not None:
deck = collection.decks.byName(deck)
deck = collection.decks.by_name(deck)
if deck is not None:
exporter = AnkiPackageExporter(collection)
exporter.did = deck['id']