From b82a42f875d454cf9972b0aec666f51bc006e3a0 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 10 Nov 2013 13:59:37 -0800 Subject: [PATCH] Reader now works with multiple profiles Former-commit-id: 8b63fc882c208986956eaab5e046c7e2174137b1 --- yomi_base/reader.py | 32 ++++++++++++++++++++------------ yomi_base/reader_util.py | 10 +++++----- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/yomi_base/reader.py b/yomi_base/reader.py index 59bf2ac..0084196 100644 --- a/yomi_base/reader.py +++ b/yomi_base/reader.py @@ -258,14 +258,14 @@ class MainWindowReader(QtGui.QMainWindow, reader_ui.Ui_MainWindowReader): definition['glossary'], definition['sentence'] ) - self.ankiAddFact(markup) + self.ankiAddFact('vocab', markup) if command == 'addReading': markup = reader_util.buildFactMarkupReading( definition['reading'], definition['glossary'], definition['sentence'] ) - self.ankiAddFact(markup) + self.ankiAddFact('vocab', markup) elif command == 'copyDefinition': reader_util.copyDefinitions([definition]) @@ -429,11 +429,15 @@ class MainWindowReader(QtGui.QMainWindow, reader_ui.Ui_MainWindowReader): self.state.searchText = text - def ankiAddFact(self, markup): + def ankiAddFact(self, profile, markup): if self.anki is None: return False - fields = reader_util.replaceMarkupInFields(self.preferences['tags'], markup) + profile = self.preferences["profiles"].get(profile) + if profile is None: + return False + + fields = reader_util.replaceMarkupInFields(profile['fields'], markup) tagsSplit = reader_util.splitTags(unicode(self.comboTags.currentText())) tagsJoined = ' '.join(tagsSplit) @@ -444,8 +448,7 @@ class MainWindowReader(QtGui.QMainWindow, reader_ui.Ui_MainWindowReader): self.comboTags.insertItem(0, tagsJoined) self.preferences.updateFactTags(tagsJoined) - #FIXME - factId = self.anki.addNote(self.preferences.ankiDeck, self.preferences.ankiModel, fields, tagsSplit) + factId = self.anki.addNote(profile['deck'], profile['model'], fields, tagsSplit) if factId is None: return False @@ -463,11 +466,16 @@ class MainWindowReader(QtGui.QMainWindow, reader_ui.Ui_MainWindowReader): return True - def ankiIsFactValid(self, markup): - #FIXME - if self.anki is not None: - fields = reader_util.replaceMarkupInFields(self.preferences.ankiFields, markup) - return self.anki.canAddNote(self.preferences.ankiDeck, self.preferences.ankiModel, fields) + def ankiIsFactValid(self, profile, markup): + if self.anki is None: + return False + + profile = self.preferences["profiles"].get(profile) + if profile is None: + return False + + fields = reader_util.replaceMarkupInFields(profile['fields'], markup) + return self.anki.canAddNote(profile['deck'], profile['model'], fields) def updateSampleMouseEvent(self, event): @@ -563,7 +571,7 @@ class MainWindowReader(QtGui.QMainWindow, reader_ui.Ui_MainWindowReader): def updateDefinitions(self): - html = reader_util.buildDefinitionsHtml(self.state.definitions, self.ankiIsFactValid) + html = reader_util.buildDefinitionsHtml(self.state.definitions, self.ankiIsFactValid, 'vocab') self.textDefinitions.setHtml(html) diff --git a/yomi_base/reader_util.py b/yomi_base/reader_util.py index 067d54f..3edb951 100644 --- a/yomi_base/reader_util.py +++ b/yomi_base/reader_util.py @@ -122,7 +122,7 @@ def copyDefinitions(definitions): QtGui.QApplication.clipboard().setText(text) -def buildDefinitionHtml(definition, factIndex, factQuery): +def buildDefinitionHtml(definition, factIndex, factQuery, profile): reading = unicode() if definition['reading']: reading = u'[{0}]'.format(definition['reading']) @@ -134,9 +134,9 @@ def buildDefinitionHtml(definition, factIndex, factQuery): links = ''.format(factIndex) if factQuery: - if factQuery(buildFactMarkupExpression(definition['expression'], definition['reading'], definition['glossary'])): + if factQuery(profile, buildFactMarkupExpression(definition['expression'], definition['reading'], definition['glossary'])): links += ''.format(factIndex) - if factQuery(buildFactMarkupReading(definition['reading'], definition['glossary'])): + if factQuery(profile, buildFactMarkupReading(definition['reading'], definition['glossary'])): links += ''.format(factIndex) html = u""" @@ -149,7 +149,7 @@ def buildDefinitionHtml(definition, factIndex, factQuery): return html -def buildDefinitionsHtml(definitions, factQuery): +def buildDefinitionsHtml(definitions, factQuery, profile): palette = QtGui.QApplication.palette() toolTipBg = palette.color(QtGui.QPalette.Window).name() toolTipFg = palette.color(QtGui.QPalette.WindowText).name() @@ -162,7 +162,7 @@ def buildDefinitionsHtml(definitions, factQuery): if len(definitions) > 0: for i, definition in enumerate(definitions): - html += buildDefinitionHtml(definition, i, factQuery) + html += buildDefinitionHtml(definition, i, factQuery, profile) else: html += """

No definitions to display.