1

Bring Yomichan AnkiConnect API up to date

This commit is contained in:
Alex Yatskov 2016-05-29 16:04:39 -07:00
parent 1ea22fcc3b
commit 5421d0bde7
3 changed files with 78 additions and 71 deletions

View File

@ -22,12 +22,19 @@ import aqt
class Anki:
def addNote(self, deckName, modelName, fields, tags=[]):
note = self.createNote(deckName, modelName, fields, tags)
if note is not None:
collection = self.collection()
if collection is None:
return
note = self.createNote(deckName, modelName, fields, tags)
if note is None:
return
self.startEditing()
collection.addNote(note)
collection.autosave()
self.startEditing()
return note.id
@ -36,15 +43,19 @@ class Anki:
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 None
return
deck = self.decks().byName(deckName)
deck = collection.decks.byName(deckName)
if deck is None:
return None
return
note = anki.notes.Note(self.collection(), model)
note = anki.notes.Note(collection, model)
note.model()['did'] = deck['id']
note.tags = tags
@ -67,7 +78,7 @@ class Anki:
def stopEditing(self):
if self.collection():
if self.collection() is not None:
self.window().maybeReset()
@ -83,23 +94,23 @@ class Anki:
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()

View File

@ -19,7 +19,7 @@
from PyQt4 import QtCore
from ajax import AjaxServer
import reader_util
from constants import c
class AnkiConnect:
@ -47,59 +47,54 @@ class AnkiConnect:
self.server.advance()
def prepareNoteArgs(self, definition, mode):
if definition is None:
return None
if mode == 'vocabExp':
profile = 'vocab'
markupFunc = reader_util.markupVocabExp
elif mode == 'vocabReading':
profile = 'vocab'
markupFunc = reader_util.markupVocabReading
elif mode == 'kanji':
profile = 'kanji'
markupFunc = reader_util.markupKanji
else:
return None
profile = self.preferences['profiles'].get(profile)
if profile is None:
return None
markup = markupFunc(definition)
if markup is None:
return None
return {
'deck': profile['deck'],
'model': profile['model'],
'fields': reader_util.formatFields(profile['fields'], markup),
'tags': self.preferences['tags']
}
def handler(self, request):
action = 'api_' + request.get('action', '')
action = 'api_' + (request.get('action') or '')
if hasattr(self, action):
return getattr(self, action)(**request.get('params', {}))
return getattr(self, action)(**(request.get('params') or {}))
def api_addNote(self, definition, mode):
args = self.prepareNoteArgs(definition, mode)
if args is not None:
return self.anki.addNote(args['deck'], args['model'], args['fields'], args['tags'])
def api_deckNames(self):
return self.anki.deckNames()
def api_canAddNotes(self, definitions, modes):
states = []
def api_modelNames(self):
return self.anki.modelNames()
for definition in definitions:
state = {}
for mode in modes:
args = self.prepareNoteArgs(definition, mode)
state[mode] = args is not None and self.anki.canAddNote(args['deck'], args['model'], args['fields'])
states.append(state)
def api_modelFieldNames(self, modelName):
return self.anki.modelFieldNames(modelName)
return states
def api_addNote(self, note):
return self.anki.addNote(
note['deckName'],
note['modelName'],
note['fields'],
note['tags']
)
def api_canAddNotes(self, notes):
results = []
for note in notes:
results.append(self.anki.canAddNote(
note['deckName'],
note['modelName'],
note['fields']
))
return results
def api_features(self):
features = {}
for name in dir(self):
method = getattr(self, name)
if name.startswith('api_') and callable(method):
features[name[4:]] = list(method.func_code.co_varnames[1:])
return features
def api_version(self):
return c['apiVersion']

View File

@ -18,4 +18,5 @@
c = {
'appVersion': '0.21b',
'apiVersion': 1
}