From a623e97ebccc237c46d03e4179c217e302ab4b29 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 29 May 2016 10:31:24 -0700 Subject: [PATCH] Handle Anki intermediate states --- anki_connect.py | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/anki_connect.py b/anki_connect.py index 95fdd1c..3b709d9 100644 --- a/anki_connect.py +++ b/anki_connect.py @@ -186,13 +186,16 @@ class AjaxServer: class AnkiBridge: def addNote(self, deckName, modelName, fields, tags=[]): + collection = self.collection() + if collection is None: + return + note = self.createNote(deckName, modelName, fields, tags) if note is None: return self.startEditing() - collection = self.collection() collection.addNote(note) collection.autosave() @@ -204,15 +207,19 @@ class AnkiBridge: def createNote(self, deckName, modelName, fields, tags=[]): - model = self.models().byName(modelName) + collection = self.collection() + if collection is None: + return + + model = collection.models.byName(modelName) if model is None: return - deck = self.decks().byName(deckName) + deck = collection.decks.byName(deckName) if deck is None: return - note = anki.notes.Note(self.collection(), model) + note = anki.notes.Note(collection, model) note.model()['did'] = deck['id'] note.tags = tags @@ -235,7 +242,7 @@ class AnkiBridge: def stopEditing(self): - if self.collection(): + if self.collection() is not None: self.window().maybeReset() @@ -247,26 +254,26 @@ class AnkiBridge: return self.window().col - def models(self): - return self.collection().models - - def modelNames(self): - return self.models().allNames() + collection = self.collection() + if collection is not None: + return collection.models.allNames() def modelFieldNames(self, modelName): - model = self.models().byName(modelName) + collection = self.collection() + if collection is None: + return + + model = collection.models.byName(modelName) if model is not None: return [field['name'] for field in model['flds']] - def decks(self): - return self.collection().decks - - def deckNames(self): - return self.decks().allNames() + collection = self.collection() + if collection is not None: + return collection.decks.allNames() #