From 89dc7b0746408be2896649f81755ae9a06e29a0f 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 a264fd1..f558fa7 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)