Bring Yomichan AnkiConnect API up to date
This commit is contained in:
parent
1ea22fcc3b
commit
5421d0bde7
@ -22,12 +22,19 @@ import aqt
|
|||||||
|
|
||||||
class Anki:
|
class Anki:
|
||||||
def addNote(self, deckName, modelName, fields, tags=[]):
|
def addNote(self, deckName, modelName, fields, tags=[]):
|
||||||
note = self.createNote(deckName, modelName, fields, tags)
|
|
||||||
if note is not None:
|
|
||||||
collection = self.collection()
|
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.addNote(note)
|
||||||
collection.autosave()
|
collection.autosave()
|
||||||
self.startEditing()
|
|
||||||
return note.id
|
return note.id
|
||||||
|
|
||||||
|
|
||||||
@ -36,15 +43,19 @@ class Anki:
|
|||||||
|
|
||||||
|
|
||||||
def createNote(self, deckName, modelName, fields, tags=[]):
|
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:
|
if model is None:
|
||||||
return None
|
return
|
||||||
|
|
||||||
deck = self.decks().byName(deckName)
|
deck = collection.decks.byName(deckName)
|
||||||
if deck is None:
|
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.model()['did'] = deck['id']
|
||||||
note.tags = tags
|
note.tags = tags
|
||||||
|
|
||||||
@ -67,7 +78,7 @@ class Anki:
|
|||||||
|
|
||||||
|
|
||||||
def stopEditing(self):
|
def stopEditing(self):
|
||||||
if self.collection():
|
if self.collection() is not None:
|
||||||
self.window().maybeReset()
|
self.window().maybeReset()
|
||||||
|
|
||||||
|
|
||||||
@ -83,23 +94,23 @@ class Anki:
|
|||||||
return self.window().col
|
return self.window().col
|
||||||
|
|
||||||
|
|
||||||
def models(self):
|
|
||||||
return self.collection().models
|
|
||||||
|
|
||||||
|
|
||||||
def modelNames(self):
|
def modelNames(self):
|
||||||
return self.models().allNames()
|
collection = self.collection()
|
||||||
|
if collection is not None:
|
||||||
|
return collection.models.allNames()
|
||||||
|
|
||||||
|
|
||||||
def modelFieldNames(self, modelName):
|
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:
|
if model is not None:
|
||||||
return [field['name'] for field in model['flds']]
|
return [field['name'] for field in model['flds']]
|
||||||
|
|
||||||
|
|
||||||
def decks(self):
|
|
||||||
return self.collection().decks
|
|
||||||
|
|
||||||
|
|
||||||
def deckNames(self):
|
def deckNames(self):
|
||||||
return self.decks().allNames()
|
collection = self.collection()
|
||||||
|
if collection is not None:
|
||||||
|
return collection.decks.allNames()
|
||||||
|
@ -19,7 +19,7 @@
|
|||||||
|
|
||||||
from PyQt4 import QtCore
|
from PyQt4 import QtCore
|
||||||
from ajax import AjaxServer
|
from ajax import AjaxServer
|
||||||
import reader_util
|
from constants import c
|
||||||
|
|
||||||
|
|
||||||
class AnkiConnect:
|
class AnkiConnect:
|
||||||
@ -47,59 +47,54 @@ class AnkiConnect:
|
|||||||
self.server.advance()
|
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):
|
def handler(self, request):
|
||||||
action = 'api_' + request.get('action', '')
|
action = 'api_' + (request.get('action') or '')
|
||||||
if hasattr(self, action):
|
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):
|
def api_deckNames(self):
|
||||||
args = self.prepareNoteArgs(definition, mode)
|
return self.anki.deckNames()
|
||||||
if args is not None:
|
|
||||||
return self.anki.addNote(args['deck'], args['model'], args['fields'], args['tags'])
|
|
||||||
|
|
||||||
|
|
||||||
def api_canAddNotes(self, definitions, modes):
|
def api_modelNames(self):
|
||||||
states = []
|
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']
|
||||||
|
@ -18,4 +18,5 @@
|
|||||||
|
|
||||||
c = {
|
c = {
|
||||||
'appVersion': '0.21b',
|
'appVersion': '0.21b',
|
||||||
|
'apiVersion': 1
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user