Skip to content

Commit

Permalink
use collectionop to update notes in batch
Browse files Browse the repository at this point in the history
  • Loading branch information
Luc W committed Jan 30, 2024
1 parent 11acc19 commit 659154c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 18 deletions.
16 changes: 13 additions & 3 deletions anki_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,7 @@ def undo_end_fn():

def update_note(self, note):
ensure_anki_collection_open()
# aqt.mw.col.update_note(note)
collection_op = aqt.operations.note.update_note(parent=aqt.mw, note=note)
collection_op.run_in_background()
aqt.mw.col.update_note(note)

def create_card_from_note(self, note, card_ord, model, template):
return note.ephemeral_card(
Expand All @@ -141,6 +139,18 @@ def save_note_type_update(self, note_model):
logger.info(f"""updating note type: {note_model['name']}""")
aqt.mw.col.models.update_dict(note_model)

def run_in_background_collection_op(self, parent_widget, update_fn):
# update fn takes collection as a parameter
def update_fn_with_undo(col):
# start new undo entry
undo_id = aqt.mw.col.add_custom_undo_entry(constants.UNDO_ENTRY_NAME)
# run actual operation
update_fn(col)
# merge undo entries
return aqt.mw.col.merge_undo_entries(undo_id)

collection_op = aqt.operations.CollectionOp(parent_widget, update_fn_with_undo)
collection_op.run_in_background()

def run_in_background(self, task_fn, task_done_fn):
aqt.mw.taskman.run_in_background(task_fn, task_done_fn)
Expand Down
2 changes: 1 addition & 1 deletion component_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def configure_browser(self, note_id_list):
self.target = component_target.BatchTarget(self.hypertts, field_list, self.target_model_updated)
self.voice_selection = component_voiceselection.VoiceSelection(self.hypertts, self.dialog, self.voice_selection_model_updated)
self.text_processing = component_text_processing.TextProcessing(self.hypertts, self.text_processing_model_updated)
self.preview = component_batch_preview.BatchPreview(self.hypertts, self.note_id_list,
self.preview = component_batch_preview.BatchPreview(self.hypertts, self.dialog, self.note_id_list,
self.sample_selected, self.apply_notes_batch_start, self.apply_notes_batch_end)
self.editor_mode = False
self.show_settings = True
Expand Down
22 changes: 14 additions & 8 deletions component_batch_preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import aqt.qt
import time
import html
import aqt.operations

constants = __import__('constants', globals(), locals(), [], sys._addon_import_level_base)
component_common = __import__('component_common', globals(), locals(), [], sys._addon_import_level_base)
Expand Down Expand Up @@ -76,8 +77,9 @@ def headerData(self, col, orientation, role):
return aqt.qt.QVariant()

class BatchPreview(component_common.ComponentBase):
def __init__(self, hypertts, note_id_list, sample_selection_fn, batch_start_fn, batch_end_fn):
def __init__(self, hypertts, dialog, note_id_list, sample_selection_fn, batch_start_fn, batch_end_fn):
self.hypertts = hypertts
self.dialog = dialog
self.note_id_list = note_id_list
self.sample_selection_fn = sample_selection_fn
self.batch_start_fn = batch_start_fn
Expand Down Expand Up @@ -209,19 +211,23 @@ def get_selected_note_status(self):

def apply_audio_to_notes(self):
self.apply_to_notes_batch_started = True
self.hypertts.anki_utils.run_in_background(self.load_audio_task, self.load_audio_task_done)
self.hypertts.anki_utils.run_in_background_collection_op(self.dialog, self.apply_audio_fn)

def stop_button_pressed(self):
self.batch_status.stop()

def load_audio_task(self):
logger.info('load_audio_task')
self.hypertts.process_batch_audio(self.note_id_list, self.batch_model, self.batch_status)
logger.info('load_audio_task finish')
def apply_audio_fn(self, anki_collection):
self.hypertts.process_batch_audio(self.note_id_list, self.batch_model, self.batch_status, anki_collection)

def load_audio_task_done(self, result):
logger.info('load_audio_task_done')
# def load_audio_task(self, col):
# logger.info('load_audio_task')
# return self.hypertts.process_batch_audio(self.note_id_list, self.batch_model, self.batch_status)
# # logger.info('load_audio_task finish')

# def load_audio_task_done(self, result):
# collection_op = result.result()
# collection_op.run_in_background()
# logger.info('load_audio_task_done')

def batch_start(self):
self.hypertts.anki_utils.run_on_main(self.show_running_stack)
Expand Down
16 changes: 10 additions & 6 deletions hypertts.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import aqt.addcards
import anki.notes
import anki.cards
import aqt.operations

constants = __import__('constants', globals(), locals(), [], sys._addon_import_level_base)
options = __import__('options', globals(), locals(), [], sys._addon_import_level_base)
Expand Down Expand Up @@ -48,27 +49,30 @@ def __init__(self, anki_utils, service_manager):
self.perform_config_migration()


def process_batch_audio(self, note_id_list, batch, batch_status):
def process_batch_audio(self, note_id_list, batch, batch_status, anki_collection):
# for each note, generate audio
with batch_status.get_batch_running_action_context():
undo_id = self.anki_utils.undo_start()

modified_notes = []

for note_id in note_id_list:
with batch_status.get_note_action_context(note_id, False) as note_action_context:
note = self.anki_utils.get_note_by_id(note_id)
# process note
source_text, processed_text, sound_file, full_filename = self.process_note_audio(batch, note, False,
context.AudioRequestContext(constants.AudioRequestReason.batch), None)
context.AudioRequestContext(constants.AudioRequestReason.batch), None, anki_collection)
# update note action context
note_action_context.set_source_text(source_text)
note_action_context.set_processed_text(processed_text)
note_action_context.set_sound(sound_file)
note_action_context.set_status(constants.BatchNoteStatus.Done)

modified_notes.append(note)
if batch_status.must_continue == False:
logger.info('batch_status execution interrupted')
break
self.anki_utils.undo_end(undo_id)

def process_note_audio(self, batch: config_models.BatchConfig, note, add_mode, audio_request_context, text_override):
def process_note_audio(self, batch: config_models.BatchConfig, note, add_mode, audio_request_context, text_override, anki_collection):
target_field = batch.target.target_field

if target_field not in note:
Expand Down Expand Up @@ -98,7 +102,7 @@ def process_note_audio(self, batch: config_models.BatchConfig, note, add_mode, a

note[target_field] = target_field_content
if not add_mode:
self.anki_utils.update_note(note)
anki_collection.update_note(note)

return source_text, processed_text, sound_file, full_filename

Expand Down

0 comments on commit 659154c

Please sign in to comment.