Skip to content

Commit

Permalink
Fix importer
Browse files Browse the repository at this point in the history
  • Loading branch information
1over137 committed Mar 20, 2024
1 parent 036f3de commit 946412b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
13 changes: 5 additions & 8 deletions vocabsieve/importer/GenericImporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,9 @@ def defineWords(self) -> None:
else:
sentence = note.sentence

if defi1.getDefinitions(word):
definition1 = defi1.getDefinitions(word)[0]
else:
definition1 = None
if definition2_enabled and defi2.getDefinitions(word):
definition2 = defi2.getDefinitions(word)[0]
definition1 = defi1.getFirstDefinition(word)
if definition2_enabled:
definition2 = defi2.getFirstDefinition(word)
else:
definition2 = None
if not (definition1 or definition2) and not self.add_even_if_no_defi.isChecked():
Expand Down Expand Up @@ -210,8 +207,8 @@ def defineWords(self) -> None:
new_note_item = SRSNote(
word=word, # fine if no definition
sentence=sentence, # fine if empty string
definition1=self._parent.definition.toAnki(definition1) if definition1 is not None else None,
definition2=self._parent.definition2.toAnki(definition2) if definition2 is not None else None,
definition1=definition1.definition if definition1 is not None else None,
definition2=definition2.definition if definition2 is not None else None,
audio_path=audio_path or None,
tags=tags
)
Expand Down
19 changes: 15 additions & 4 deletions vocabsieve/ui/multi_definition_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,19 @@ def populateDefinitions(self):
self.currentIndex = 0
self.updateIndex()

def getFirstDefinition(self, target) -> Optional[Definition]:
"""
Blocking function to get the first definition from all sources
For use outside of the main interface
"""
for source in self.sources:
logger.debug("Getting definition from source " + source.name)
for defi in source.define(target):
logger.debug("Got definition from source " + defi.source + ": " + str(defi))
if defi.definition is not None:
return defi
return None

def updateIndex(self):
if not self.definitions:
return
Expand Down Expand Up @@ -241,16 +254,14 @@ def getSource(self, source_name: str) -> Optional[DictionarySource]:
return source
return None

def toAnki(self, defi: Optional[Definition] = None) -> str:
def toAnki(self) -> str:
"""Process definitions before sending to Anki"""
# Figure out display mode of current source
if self.currentDefinition is None:
return self.toPlainText().replace("\n", "<br>")
if defi is None:
defi = self.currentDefinition
source_name = self.currentDefinition.source
source = self.getSource(source_name)
if source is None: # This means no definition is found but maybe the user typed in something
return self.toPlainText().replace("\n", "<br>")

return process_defi_anki(self.toPlainText(), self.toMarkdown(), defi, source)
return process_defi_anki(self.toPlainText(), self.toMarkdown(), self.currentDefinition, source)

0 comments on commit 946412b

Please sign in to comment.