1
yomichan-anki/yomi_base/anki_host.py

116 lines
2.8 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
2011-08-28 18:01:32 +00:00
# Copyright (C) 2011 Alex Yatskov
#
# 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-23 22:11:48 +00:00
import aqt
2011-08-28 18:01:32 +00:00
import re
class Anki:
2012-12-24 00:02:00 +00:00
def __init__(self, modelName=None):
self.setModelName(modelName)
def setModelName(self, modelName):
self.modelName = modelName
def addNote(self, fields, tags=unicode()):
note = self.createNote(fields, tags)
if not note:
2011-08-28 18:01:32 +00:00
return None
action = lang._('Add')
collection = self.collection()
collection.setUndoStart(action)
collection.addNote(note, False)
collection.setUndoEnd(action)
collection.rebuildCounts()
2011-08-28 18:01:32 +00:00
ankiqt.mw.updateTitleBar()
ankiqt.mw.statusView.redraw()
return note.id
2011-08-28 18:01:32 +00:00
def canAddNote(self, fields):
return bool(self.createNote(fields))
2011-08-28 18:01:32 +00:00
def createNote(self, fields, tags=unicode()):
collection = self.collection()
note = collection.newnote()
note.tags = self.cleanupTags(tags)
2011-08-28 18:01:32 +00:00
try:
for field in note.fields:
2011-08-28 18:01:32 +00:00
field.value = fields.get(field.getName()) or unicode()
if not note.fieldValid(field) or not note.fieldUnique(field, collection.s):
2011-08-28 18:01:32 +00:00
return None
except KeyError:
return None
return note
2011-08-28 18:01:32 +00:00
def browseNote(self, noteId):
2011-08-28 18:01:32 +00:00
browser = ui.dialogs.get('CardList', self.window())
browser.dialog.filterEdit.setText('fid:' + str(noteId))
2011-08-28 18:01:32 +00:00
browser.updateSearch()
browser.onnote()
2011-08-28 18:01:32 +00:00
def cleanupTags(self, tags):
return re.sub('[;,]', unicode(), tags).strip()
def window(self):
return aqt.mw
2011-08-28 18:01:32 +00:00
def form(self):
return self.window().form
2011-08-28 18:01:32 +00:00
def toolsMenu(self):
return self.form().menuTools
2011-08-28 18:01:32 +00:00
def collection(self):
return self.window().col
2011-08-28 18:01:32 +00:00
def models(self):
return self.collection().models
2011-08-28 18:01:32 +00:00
2012-12-24 00:02:00 +00:00
def modelNames(self):
return self.models().allNames()
def currentModel(self):
2012-12-24 00:02:00 +00:00
for model in self.models().models.values():
if model['name'] == self.modelName:
return model
def currentModelFieldNames(self):
model = self.currentModel()
if model is not None:
return [field['name'] for field in model['flds']]