Skip to content

Commit

Permalink
Fix bulk fill for usage
Browse files Browse the repository at this point in the history
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 jdlorimer#177.
  • Loading branch information
spolcyn committed Nov 7, 2021
1 parent cfd1325 commit d98b267
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions chinese/fill.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,8 @@ def bulk_fill_usage():
<b>Cards with no Sentences added:</b> %(not_filled)d<br>
<b>Failed:</b> %(failed)d'''

fields = config.get_fields(['usage'])
target_fields = config.get_fields(["usage"])
hanzi_fields = config.get_fields(["hanzi"])

if not askUser(prompt):
return
Expand All @@ -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)
Expand Down

0 comments on commit d98b267

Please sign in to comment.