1
yomichan-anki/yomi_base/anki_bridge.py

117 lines
3.0 KiB
Python
Raw Normal View History

2011-08-28 18:01:32 +00:00
# -*- coding: utf-8 -*-
2011-10-27 15:22:26 +00:00
# Copyright (C) 2013 Alex Yatskov
2011-08-28 18:01:32 +00:00
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2012-12-25 16:25:10 +00:00
import anki
import aqt
2011-08-28 18:01:32 +00:00
class Anki:
2016-05-02 01:42:17 +00:00
def addNote(self, deckName, modelName, fields, tags=[]):
collection = self.collection()
if collection is None:
return
2012-12-25 02:53:41 +00:00
note = self.createNote(deckName, modelName, fields, tags)
if note is None:
return
self.startEditing()
collection.addNote(note)
collection.autosave()
return note.id
2011-08-28 18:01:32 +00:00
2012-12-24 17:24:02 +00:00
def canAddNote(self, deckName, modelName, fields):
return bool(self.createNote(deckName, modelName, fields))
2011-08-28 18:01:32 +00:00
2016-05-02 01:42:17 +00:00
def createNote(self, deckName, modelName, fields, tags=[]):
collection = self.collection()
if collection is None:
return
model = collection.models.byName(modelName)
2012-12-24 03:13:45 +00:00
if model is None:
return
2011-08-28 18:01:32 +00:00
deck = collection.decks.byName(deckName)
2012-12-24 17:24:02 +00:00
if deck is None:
return
2012-12-24 17:24:02 +00:00
note = anki.notes.Note(collection, model)
2012-12-24 17:24:02 +00:00
note.model()['did'] = deck['id']
note.tags = tags
2012-12-24 03:13:45 +00:00
2012-12-24 02:54:05 +00:00
for name, value in fields.items():
if name in note:
note[name] = value
2012-12-24 02:54:05 +00:00
2012-12-25 16:25:10 +00:00
if not note.dupeOrEmpty():
return note
2011-08-28 18:01:32 +00:00
2012-12-25 02:58:39 +00:00
def browseNote(self, noteId):
2012-12-25 16:25:10 +00:00
browser = aqt.dialogs.open('Browser', self.window())
browser.form.searchEdit.lineEdit().setText('nid:{0}'.format(noteId))
browser.onSearch()
2011-08-28 18:01:32 +00:00
2012-12-25 02:53:41 +00:00
def startEditing(self):
self.window().requireReset()
2011-08-28 18:01:32 +00:00
2012-12-25 02:53:41 +00:00
def stopEditing(self):
if self.collection() is not None:
2012-12-25 16:39:21 +00:00
self.window().maybeReset()
2012-12-25 02:53:41 +00:00
def window(self):
return aqt.mw
2011-08-28 18:01:32 +00:00
2012-12-25 17:11:51 +00:00
def addUiAction(self, action):
self.window().form.menuTools.addAction(action)
2011-08-28 18:01:32 +00:00
def collection(self):
return self.window().col
2011-08-28 18:01:32 +00:00
2012-12-24 00:02:00 +00:00
def modelNames(self):
collection = self.collection()
if collection is not None:
return collection.models.allNames()
2012-12-24 00:02:00 +00:00
2012-12-25 02:53:41 +00:00
def modelFieldNames(self, modelName):
collection = self.collection()
if collection is None:
return
model = collection.models.byName(modelName)
2012-12-25 16:25:10 +00:00
if model is not None:
return [field['name'] for field in model['flds']]
2012-12-24 02:11:14 +00:00
def deckNames(self):
collection = self.collection()
if collection is not None:
return collection.decks.allNames()