Compare commits

...

3 Commits

Author SHA1 Message Date
Philipp Matthias Schäfer
2996476e03 Remove call to syncDelete
The method does no longer exist as of version 2.1.45, which is the minimum
supported version as of creation of this commit.
2024-01-21 13:52:23 -08:00
Philipp Matthias Schäfer
510f47fd08 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.
2024-01-21 13:52:17 -08:00
Philipp Matthias Schäfer
977871257a Remove internal method stopEditing
The only method it calls, maybeReset, contains only a pass statement and has
thus never any effect.
2024-01-21 13:52:06 -08:00

View File

@ -190,14 +190,14 @@ class AnkiConnect:
def getModel(self, modelName): def getModel(self, modelName):
model = self.collection().models.byName(modelName) model = self.collection().models.by_name(modelName)
if model is None: if model is None:
raise Exception('model was not found: {}'.format(modelName)) raise Exception('model was not found: {}'.format(modelName))
return model return model
def getField(self, model, fieldName): def getField(self, model, fieldName):
fieldMap = self.collection().models.fieldMap(model) fieldMap = self.collection().models.field_map(model)
if fieldName not in fieldMap: if fieldName not in fieldMap:
raise Exception('field was not found in {}: {}'.format(model['name'], fieldName)) raise Exception('field was not found in {}: {}'.format(model['name'], fieldName))
return fieldMap[fieldName][1] return fieldMap[fieldName][1]
@ -214,24 +214,19 @@ class AnkiConnect:
self.window().requireReset() self.window().requireReset()
def stopEditing(self):
if self.collection() is not None:
self.window().maybeReset()
def createNote(self, note): def createNote(self, note):
collection = self.collection() collection = self.collection()
model = collection.models.byName(note['modelName']) model = collection.models.by_name(note['modelName'])
if model is None: if model is None:
raise Exception('model was not found: {}'.format(note['modelName'])) 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: if deck is None:
raise Exception('deck was not found: {}'.format(note['deckName'])) raise Exception('deck was not found: {}'.format(note['deckName']))
ankiNote = anki.notes.Note(collection, model) ankiNote = anki.notes.Note(collection, model)
ankiNote.model()['did'] = deck['id'] ankiNote.note_type()['did'] = deck['id']
if 'tags' in note: if 'tags' in note:
ankiNote.tags = note['tags'] ankiNote.tags = note['tags']
@ -321,7 +316,7 @@ class AnkiConnect:
if duplicateScope == 'deck': if duplicateScope == 'deck':
did = deck['id'] did = deck['id']
if duplicateScopeDeckName is not None: if duplicateScopeDeckName is not None:
deck2 = collection.decks.byName(duplicateScopeDeckName) deck2 = collection.decks.by_name(duplicateScopeDeckName)
if deck2 is None: if deck2 is None:
# Invalid deck, so cannot be duplicate # Invalid deck, so cannot be duplicate
return 0 return 0
@ -517,7 +512,7 @@ class AnkiConnect:
@util.api() @util.api()
def deckNames(self): def deckNames(self):
return self.decks().allNames() return [x.name for x in self.decks().all_names_and_ids()]
@util.api() @util.api()
@ -545,13 +540,8 @@ class AnkiConnect:
@util.api() @util.api()
def createDeck(self, deck): def createDeck(self, deck):
try: self.startEditing()
self.startEditing() return self.decks().id(deck)
did = self.decks().id(deck)
finally:
self.stopEditing()
return did
@util.api() @util.api()
@ -569,7 +559,6 @@ class AnkiConnect:
# then move into new deck # then move into new deck
self.collection().db.execute('update cards set usn=?, mod=?, did=? where id in ' + scids, usn, mod, did) self.collection().db.execute('update cards set usn=?, mod=?, did=? where id in ' + scids, usn, mod, did)
self.stopEditing()
@util.api() @util.api()
@ -583,14 +572,11 @@ class AnkiConnect:
# this is dangerous, so let's raise our own exception # this is dangerous, so let's raise our own exception
raise Exception("Since Anki 2.1.28 it's not possible " raise Exception("Since Anki 2.1.28 it's not possible "
"to delete decks without deleting cards as well") "to delete decks without deleting cards as well")
try: self.startEditing()
self.startEditing() decks = filter(lambda d: d in self.deckNames(), decks)
decks = filter(lambda d: d in self.deckNames(), decks) for deck in decks:
for deck in decks: did = self.decks().id(deck)
did = self.decks().id(deck) self.decks().remove([did])
self.decks().rem(did, cardsToo=cardsToo)
finally:
self.stopEditing()
@util.api() @util.api()
@ -600,7 +586,7 @@ class AnkiConnect:
collection = self.collection() collection = self.collection()
did = collection.decks.id(deck) did = collection.decks.id(deck)
return collection.decks.confForDid(did) return collection.decks.config_dict_for_deck_id(did)
@util.api() @util.api()
@ -614,7 +600,7 @@ class AnkiConnect:
return False return False
try: try:
collection.decks.save(config) collection.decks.save(config)
collection.decks.updateConf(config) collection.decks.update_config(config)
except: except:
return False return False
return True return True
@ -648,8 +634,8 @@ class AnkiConnect:
if configId not in [c['id'] for c in collection.decks.all_config()]: if configId not in [c['id'] for c in collection.decks.all_config()]:
return False return False
config = collection.decks.getConf(configId) config = collection.decks.get_config(configId)
return collection.decks.confId(name, config) return collection.decks.add_config_returning_id(name, config)
@util.api() @util.api()
@ -658,7 +644,7 @@ class AnkiConnect:
if int(configId) not in [c['id'] for c in collection.decks.all_config()]: if int(configId) not in [c['id'] for c in collection.decks.all_config()]:
return False return False
collection.decks.remConf(configId) collection.decks.remove_config(configId)
return True return True
@util.api() @util.api()
@ -722,10 +708,7 @@ class AnkiConnect:
@util.api() @util.api()
def deleteMediaFile(self, filename): def deleteMediaFile(self, filename):
try: self.media().trash_files([filename])
self.media().syncDelete(filename)
except AttributeError:
self.media().trash_files([filename])
@util.api() @util.api()
def getMediaDirPath(self): def getMediaDirPath(self):
@ -741,7 +724,6 @@ class AnkiConnect:
if nCardsAdded < 1: if nCardsAdded < 1:
raise Exception('The field values you have provided would make an empty question on all cards.') raise Exception('The field values you have provided would make an empty question on all cards.')
collection.autosave() collection.autosave()
self.stopEditing()
return ankiNote.id return ankiNote.id
@ -821,7 +803,6 @@ class AnkiConnect:
ankiNote.flush() ankiNote.flush()
self.collection().autosave() self.collection().autosave()
self.stopEditing()
@util.api() @util.api()
@ -859,7 +840,6 @@ class AnkiConnect:
def addTags(self, notes, tags, add=True): def addTags(self, notes, tags, add=True):
self.startEditing() self.startEditing()
self.collection().tags.bulkAdd(notes, tags, add) self.collection().tags.bulkAdd(notes, tags, add)
self.stopEditing()
@util.api() @util.api()
@ -887,9 +867,9 @@ class AnkiConnect:
except NotFoundError: except NotFoundError:
continue continue
if note.hasTag(tag_to_replace): if note.has_tag(tag_to_replace):
note.delTag(tag_to_replace) note.remove_tag(tag_to_replace)
note.addTag(replace_with_tag) note.add_tag(replace_with_tag)
note.flush() note.flush()
self.window().requireReset() self.window().requireReset()
@ -904,9 +884,9 @@ class AnkiConnect:
collection = self.collection() collection = self.collection()
for nid in collection.db.list('select id from notes'): for nid in collection.db.list('select id from notes'):
note = self.getNote(nid) note = self.getNote(nid)
if note.hasTag(tag_to_replace): if note.has_tag(tag_to_replace):
note.delTag(tag_to_replace) note.remove_tag(tag_to_replace)
note.addTag(replace_with_tag) note.add_tag(replace_with_tag)
note.flush() note.flush()
self.window().requireReset() self.window().requireReset()
@ -993,7 +973,6 @@ class AnkiConnect:
scheduler.suspendCards(cards) scheduler.suspendCards(cards)
else: else:
scheduler.unsuspendCards(cards) scheduler.unsuspendCards(cards)
self.stopEditing()
return True return True
@ -1055,7 +1034,7 @@ class AnkiConnect:
@util.api() @util.api()
def modelNames(self): def modelNames(self):
return self.collection().models.allNames() return [n.name for n in self.collection().models.all_names_and_ids()]
@util.api() @util.api()
@ -1065,7 +1044,7 @@ class AnkiConnect:
raise Exception('Must provide at least one field for inOrderFields') raise Exception('Must provide at least one field for inOrderFields')
if len(cardTemplates) == 0: if len(cardTemplates) == 0:
raise Exception('Must provide at least one card for cardTemplates') 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') raise Exception('Model name already exists')
collection = self.collection() collection = self.collection()
@ -1078,7 +1057,7 @@ class AnkiConnect:
# Create fields and add them to Note # Create fields and add them to Note
for field in inOrderFields: for field in inOrderFields:
fm = mm.newField(field) fm = mm.new_field(field)
mm.addField(m, fm) mm.addField(m, fm)
# Add shared css to model if exists. Use default otherwise # Add shared css to model if exists. Use default otherwise
@ -1092,7 +1071,7 @@ class AnkiConnect:
if 'Name' in card: if 'Name' in card:
cardName = card['Name'] cardName = card['Name']
t = mm.newTemplate(cardName) t = mm.new_template(cardName)
cardCount += 1 cardCount += 1
t['qfmt'] = card['Front'] t['qfmt'] = card['Front']
t['afmt'] = card['Back'] t['afmt'] = card['Back']
@ -1106,7 +1085,7 @@ class AnkiConnect:
def modelNamesAndIds(self): def modelNamesAndIds(self):
models = {} models = {}
for model in self.modelNames(): 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 return models
@ -1126,7 +1105,7 @@ class AnkiConnect:
def findModelsByName(self, modelNames): def findModelsByName(self, modelNames):
models = [] models = []
for name in modelNames: for name in modelNames:
model = self.collection().models.byName(name) model = self.collection().models.by_name(name)
if model is None: if model is None:
raise Exception("model was not found: {}".format(name)) raise Exception("model was not found: {}".format(name))
else: else:
@ -1144,7 +1123,7 @@ class AnkiConnect:
@util.api() @util.api()
def modelFieldNames(self, modelName): def modelFieldNames(self, modelName):
model = self.collection().models.byName(modelName) model = self.collection().models.by_name(modelName)
if model is None: if model is None:
raise Exception('model was not found: {}'.format(modelName)) raise Exception('model was not found: {}'.format(modelName))
else: else:
@ -1153,7 +1132,7 @@ class AnkiConnect:
@util.api() @util.api()
def modelFieldDescriptions(self, modelName): def modelFieldDescriptions(self, modelName):
model = self.collection().models.byName(modelName) model = self.collection().models.by_name(modelName)
if model is None: if model is None:
raise Exception('model was not found: {}'.format(modelName)) raise Exception('model was not found: {}'.format(modelName))
else: else:
@ -1181,7 +1160,7 @@ class AnkiConnect:
@util.api() @util.api()
def modelFieldsOnTemplates(self, modelName): def modelFieldsOnTemplates(self, modelName):
model = self.collection().models.byName(modelName) model = self.collection().models.by_name(modelName)
if model is None: if model is None:
raise Exception('model was not found: {}'.format(modelName)) raise Exception('model was not found: {}'.format(modelName))
@ -1212,7 +1191,7 @@ class AnkiConnect:
@util.api() @util.api()
def modelTemplates(self, modelName): def modelTemplates(self, modelName):
model = self.collection().models.byName(modelName) model = self.collection().models.by_name(modelName)
if model is None: if model is None:
raise Exception('model was not found: {}'.format(modelName)) raise Exception('model was not found: {}'.format(modelName))
@ -1225,7 +1204,7 @@ class AnkiConnect:
@util.api() @util.api()
def modelStyling(self, modelName): def modelStyling(self, modelName):
model = self.collection().models.byName(modelName) model = self.collection().models.by_name(modelName)
if model is None: if model is None:
raise Exception('model was not found: {}'.format(modelName)) raise Exception('model was not found: {}'.format(modelName))
@ -1235,7 +1214,7 @@ class AnkiConnect:
@util.api() @util.api()
def updateModelTemplates(self, model): def updateModelTemplates(self, model):
models = self.collection().models models = self.collection().models
ankiModel = models.byName(model['name']) ankiModel = models.by_name(model['name'])
if ankiModel is None: if ankiModel is None:
raise Exception('model was not found: {}'.format(model['name'])) raise Exception('model was not found: {}'.format(model['name']))
@ -1257,7 +1236,7 @@ class AnkiConnect:
@util.api() @util.api()
def updateModelStyling(self, model): def updateModelStyling(self, model):
models = self.collection().models models = self.collection().models
ankiModel = models.byName(model['name']) ankiModel = models.by_name(model['name'])
if ankiModel is None: if ankiModel is None:
raise Exception('model was not found: {}'.format(model['name'])) raise Exception('model was not found: {}'.format(model['name']))
@ -1271,13 +1250,13 @@ class AnkiConnect:
if not modelName: if not modelName:
ankiModel = self.collection().models.allNames() ankiModel = self.collection().models.allNames()
else: else:
model = self.collection().models.byName(modelName) model = self.collection().models.by_name(modelName)
if model is None: if model is None:
raise Exception('model was not found: {}'.format(modelName)) raise Exception('model was not found: {}'.format(modelName))
ankiModel = [modelName] ankiModel = [modelName]
updatedModels = 0 updatedModels = 0
for model in ankiModel: for model in ankiModel:
model = self.collection().models.byName(model) model = self.collection().models.by_name(model)
checkForText = False checkForText = False
if css and findText in model['css']: if css and findText in model['css']:
checkForText = True checkForText = True
@ -1366,7 +1345,7 @@ class AnkiConnect:
model = self.getModel(modelName) model = self.getModel(modelName)
field = self.getField(model, fieldName) field = self.getField(model, fieldName)
mm.repositionField(model, field, index) mm.reposition_field(model, field, index)
self.save_model(mm, model) self.save_model(mm, model)
@ -1377,16 +1356,16 @@ class AnkiConnect:
model = self.getModel(modelName) model = self.getModel(modelName)
# only adds the field if it doesn't already exist # only adds the field if it doesn't already exist
fieldMap = mm.fieldMap(model) fieldMap = mm.field_map(model)
if fieldName not in fieldMap: if fieldName not in fieldMap:
field = mm.newField(fieldName) field = mm.new_field(fieldName)
mm.addField(model, field) mm.addField(model, field)
# repositions, even if the field already exists # repositions, even if the field already exists
if index is not None: if index is not None:
fieldMap = mm.fieldMap(model) fieldMap = mm.field_map(model)
newField = fieldMap[fieldName][1] newField = fieldMap[fieldName][1]
mm.repositionField(model, newField, index) mm.reposition_field(model, newField, index)
self.save_model(mm, model) self.save_model(mm, model)
@ -1397,7 +1376,7 @@ class AnkiConnect:
model = self.getModel(modelName) model = self.getModel(modelName)
field = self.getField(model, fieldName) field = self.getField(model, fieldName)
mm.removeField(model, field) mm.remove_field(model, field)
self.save_model(mm, model) self.save_model(mm, model)
@ -1460,7 +1439,7 @@ class AnkiConnect:
if query is None: if query is None:
return [] return []
return list(map(int, self.collection().findNotes(query))) return list(map(int, self.collection().find_notes(query)))
@util.api() @util.api()
@ -1468,7 +1447,7 @@ class AnkiConnect:
if query is None: if query is None:
return [] return []
return list(map(int, self.collection().findCards(query))) return list(map(int, self.collection().find_cards(query)))
@util.api() @util.api()
@ -1477,7 +1456,7 @@ class AnkiConnect:
for cid in cards: for cid in cards:
try: try:
card = self.getCard(cid) card = self.getCard(cid)
model = card.model() model = card.note_type()
note = card.note() note = card.note()
fields = {} fields = {}
for info in model['flds']: for info in model['flds']:
@ -1541,7 +1520,6 @@ class AnkiConnect:
self.startEditing() self.startEditing()
scids = anki.utils.ids2str(cards) scids = anki.utils.ids2str(cards)
self.collection().db.execute('update cards set type=0, queue=0, left=0, ivl=0, due=0, odue=0, factor=0 where id in ' + scids) self.collection().db.execute('update cards set type=0, queue=0, left=0, ivl=0, due=0, odue=0, factor=0 where id in ' + scids)
self.stopEditing()
@util.api() @util.api()
@ -1549,7 +1527,6 @@ class AnkiConnect:
self.startEditing() self.startEditing()
scids = anki.utils.ids2str(cards) scids = anki.utils.ids2str(cards)
self.collection().db.execute('update cards set type=3, queue=1 where id in ' + scids) self.collection().db.execute('update cards set type=3, queue=1 where id in ' + scids)
self.stopEditing()
@util.api() @util.api()
@ -1622,7 +1599,7 @@ class AnkiConnect:
for nid in notes: for nid in notes:
try: try:
note = self.getNote(nid) note = self.getNote(nid)
model = note.model() model = note.note_type()
fields = {} fields = {}
for info in model['flds']: for info in model['flds']:
@ -1649,17 +1626,14 @@ class AnkiConnect:
@util.api() @util.api()
def deleteNotes(self, notes): def deleteNotes(self, notes):
try: self.collection().remove_notes(notes)
self.collection().remNotes(notes)
finally:
self.stopEditing()
@util.api() @util.api()
def removeEmptyNotes(self): def removeEmptyNotes(self):
for model in self.collection().models.all(): for model in self.collection().models.all():
if self.collection().models.useCount(model) == 0: if self.collection().models.use_count(model) == 0:
self.collection().models.rem(model) self.collection().models.remove(model["id"])
self.window().requireReset() self.window().requireReset()
@ -1725,18 +1699,18 @@ class AnkiConnect:
if note is not None: if note is not None:
collection = self.collection() collection = self.collection()
deck = collection.decks.byName(note['deckName']) deck = collection.decks.by_name(note['deckName'])
if deck is None: if deck is None:
raise Exception('deck was not found: {}'.format(note['deckName'])) raise Exception('deck was not found: {}'.format(note['deckName']))
collection.decks.select(deck['id']) collection.decks.select(deck['id'])
savedMid = deck.pop('mid', None) savedMid = deck.pop('mid', None)
model = collection.models.byName(note['modelName']) model = collection.models.by_name(note['modelName'])
if model is None: if model is None:
raise Exception('model was not found: {}'.format(note['modelName'])) raise Exception('model was not found: {}'.format(note['modelName']))
collection.models.setCurrent(model) collection.models.set_current(model)
collection.models.update(model) collection.models.update(model)
ankiNote = anki.notes.Note(collection, model) ankiNote = anki.notes.Note(collection, model)
@ -1795,7 +1769,7 @@ class AnkiConnect:
reviewer = self.reviewer() reviewer = self.reviewer()
card = reviewer.card card = reviewer.card
model = card.model() model = card.note_type()
note = card.note() note = card.note()
fields = {} fields = {}
@ -1876,7 +1850,7 @@ class AnkiConnect:
def guiDeckOverview(self, name): def guiDeckOverview(self, name):
collection = self.collection() collection = self.collection()
if collection is not None: if collection is not None:
deck = collection.decks.byName(name) deck = collection.decks.by_name(name)
if deck is not None: if deck is not None:
collection.decks.select(deck['id']) collection.decks.select(deck['id'])
self.window().onOverview() self.window().onOverview()
@ -1979,7 +1953,7 @@ class AnkiConnect:
def exportPackage(self, deck, path, includeSched=False): def exportPackage(self, deck, path, includeSched=False):
collection = self.collection() collection = self.collection()
if collection is not None: if collection is not None:
deck = collection.decks.byName(deck) deck = collection.decks.by_name(deck)
if deck is not None: if deck is not None:
exporter = AnkiPackageExporter(collection) exporter = AnkiPackageExporter(collection)
exporter.did = deck['id'] exporter.did = deck['id']
@ -1999,10 +1973,8 @@ class AnkiConnect:
importer = AnkiPackageImporter(collection, path) importer = AnkiPackageImporter(collection, path)
importer.run() importer.run()
except: except:
self.stopEditing()
raise raise
else: else:
self.stopEditing()
return True return True
return False return False