From 7fa69048129286061f53244e82741484f339fac3 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Mon, 24 Dec 2012 09:24:02 -0800 Subject: [PATCH] bleh --- yomi_base/anki_host.py | 58 ++++++++++++++++++++-------------------- yomi_base/reader.py | 18 ++++++------- yomi_base/reader_util.py | 4 +++ 3 files changed, 42 insertions(+), 38 deletions(-) diff --git a/yomi_base/anki_host.py b/yomi_base/anki_host.py index 566aee8..d8f5e70 100644 --- a/yomi_base/anki_host.py +++ b/yomi_base/anki_host.py @@ -18,43 +18,44 @@ import aqt import anki.hooks -import re class Anki: - def addNote(self, deckName, modelName, fields, tags=unicode()): + def addNote(self, deckName, modelName, fields, tags=list()): note = self.createNote(fields, deckName, modelName, tags) - if not note: - return None - - note.model()['did'] = self.currentDeck()['id'] - - self.collection().addNote(note) - self.window().requireReset() - - return note.id + if note is not None: + self.collection().addNote(note) + self.decks().save(self.currentDeck()) + self.window().requireReset() - def canAddNote(self, modelName, fields): - return bool(self.createNote(modelName, fields)) + def canAddNote(self, deckName, modelName, fields): + return bool(self.createNote(deckName, modelName, fields)) - def createNote(self, modelName, fields, tags=unicode()): + def createNote(self, deckName, modelName, fields, tags=list()): model = self.findModel(modelName) if model is None: return None - conf = self.collection().conf - deck = self.currentDeck() + deck = self.findDeck(deckName) + if deck is None: + return None - if conf['curModel'] != model['id'] or deck['mid'] != model['id']: - conf['curModel'] = deck['mid'] = model['id'] - self.collection().decks.save(deck) - anki.hooks.runHook('currentModelChanged') - self.window().reset() + #~ conf = self.collection().conf + #~ deck = self.currentDeck() + #~ if conf['curModel'] != model['id'] or deck['mid'] != model['id']: + #~ conf['curModel'] = deck['mid'] = model['id'] + #~ self.collection().decks.save(deck) + #~ anki.hooks.runHook('currentModelChanged') + #~ self.window().reset() - note = self.collection().newNote() - note.tags = re.split('[;,\s]', tags) + #~ self.collection().conf['curModel'] = model['id'] + #~ deck['mid'] = model['id'] + + note = anki.notes.Note(self.collection(), model) + note.model()['did'] = deck['id'] + note.tags = tags for name, value in fields.items(): note[name] = value @@ -62,12 +63,11 @@ class Anki: return None if note.dupeOrEmpty() else note - def browseNote(self, noteId): - pass - #browser = ui.dialogs.get('CardList', self.window()) - #browser.dialog.filterEdit.setText('fid:' + str(noteId)) - #browser.updateSearch() - #browser.onnote() + #~ def browseNote(self, noteId): + #~ browser = ui.dialogs.get('CardList', self.window()) + #~ browser.dialog.filterEdit.setText('fid:' + str(noteId)) + #~ browser.updateSearch() + #~ browser.onnote() def window(self): diff --git a/yomi_base/reader.py b/yomi_base/reader.py index a45f67f..d1e8e8d 100644 --- a/yomi_base/reader.py +++ b/yomi_base/reader.py @@ -100,9 +100,7 @@ class MainWindowReader(QtGui.QMainWindow): if self.preferences.uiReaderSize != None: self.resize(QtCore.QSize(*self.preferences.uiReaderSize)) - for tags in self.preferences.ankiTags: - self.comboTags.addItem(tags) - + self.comboTags.addItems(self.preferences.ankiTags) self.applyPreferencesContent() @@ -440,15 +438,17 @@ class MainWindowReader(QtGui.QMainWindow): fields = reader_util.replaceMarkupInFields(self.preferences.ankiFields, markup) - tags = self.anki.cleanupTags(unicode(self.comboTags.currentText())) - tagIndex = self.comboTags.findText(tags) + tagsSplit = reader_util.splitTags(unicode(self.comboTags.currentText())) + tagsJoined = ' '.join(tagsSplit) + + tagIndex = self.comboTags.findText(tagsJoined) if tagIndex > 0: self.comboTags.removeItem(tagIndex) if tagIndex != 0: - self.comboTags.insertItem(0, tags) - self.preferences.updateFactTags(tags) + self.comboTags.insertItem(0, tagsJoined) + self.preferences.updateFactTags(tagsJoined) - factId = self.anki.addNote(self.preferences.ankiDeck, self.preferences.ankiModel, fields, tags) + factId = self.anki.addNote(self.preferences.ankiDeck, self.preferences.ankiModel, fields, tagsSplit) if not factId: return False @@ -471,7 +471,7 @@ class MainWindowReader(QtGui.QMainWindow): return False fields = reader_util.replaceMarkupInFields(self.preferences.ankiFields, markup) - return self.anki.canAddNote(self.preferences.ankiModel, fields) + return self.anki.canAddNote(self.preferences.ankiDeck, self.preferences.ankiModel, fields) def updateSampleMouseEvent(self, event): diff --git a/yomi_base/reader_util.py b/yomi_base/reader_util.py index d7bb83a..3b0de77 100644 --- a/yomi_base/reader_util.py +++ b/yomi_base/reader_util.py @@ -118,6 +118,10 @@ def buildFactMarkupReading(reading, glossary, sentence=None): } +def splitTags(tags): + return re.split('[;,\s]', tags) + + def convertDefinitions(definitions, sentence=None): return [ Definition(*(definition + (sentence,))) for definition in definitions