From 405b32b5a3bc75b1dd57117f72c02d7003cab189 Mon Sep 17 00:00:00 2001 From: Stephen Polcyn Date: Sun, 7 Nov 2021 17:45:07 -0500 Subject: [PATCH] Fix bulk fill for usage Previously, if there existed any notes in the target deck that didn't have one of the hanzi fields, the `get_hanzi` step would fail as a result of `cleanup` raising a `ValueError` if it received `text=None`. This PR modifies the logic so that presence of a `hanzi` field is verified before getting the `hanzi` itself. It also modifies the behavior such that cards that do not have usages filled are marked as failed. Otherwise, the user doesn't know why many cards did not have usages filled. Relevant to #177. --- chinese/fill.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/chinese/fill.py b/chinese/fill.py index ce392f7..9db350d 100644 --- a/chinese/fill.py +++ b/chinese/fill.py @@ -465,7 +465,8 @@ def bulk_fill_usage(): Cards with no Sentences added: %(not_filled)d
Failed: %(failed)d''' - fields = config.get_fields(['usage']) + target_fields = config.get_fields(["usage"]) + hanzi_fields = config.get_fields(["hanzi"]) if not askUser(prompt): return @@ -476,24 +477,30 @@ def bulk_fill_usage(): n_notfilled = 0 failed_hanzi = [] - note_ids = Finder(mw.col).findNotes('deck:current') + note_ids = mw.col.findNotes("deck:current") mw.progress.start(immediate=True, min=0, max=len(note_ids)) for i, note_id in enumerate(note_ids): note = mw.col.getNote(note_id) copy = dict(note) - hanzi = get_hanzi(copy) - if has_any_field(copy, fields) and hanzi: + # Ensure note type has hanzi present before trying to get + hanzi = None + if has_any_field(copy, hanzi_fields): + hanzi = get_hanzi(copy) + + if hanzi and has_any_field(copy, target_fields): n_processed += 1 try: - if all_fields_empty(copy, fields): + if all_fields_empty(copy, target_fields): result = fill_usage(hanzi, copy) if result: n_updated += 1 else: n_notfilled += 1 + n_failed += 1 + failed_hanzi.append(hanzi) except: n_failed += 1 failed_hanzi.append(hanzi)