--convert --text for iterators, docs for --convert

This commit is contained in:
Simon Willison 2022-01-05 22:19:52 -08:00
commit 413f8ed754
3 changed files with 136 additions and 13 deletions

View file

@ -814,7 +814,16 @@ def insert_upsert_implementation(
if lines:
docs = (fn(doc["line"]) for doc in docs)
elif text:
docs = (fn(doc["text"]) for doc in docs)
# Special case: this is allowed to be an iterable
text_value = list(docs)[0]["text"]
fn_return = fn(text_value)
if isinstance(fn_return, dict):
docs = [fn_return]
else:
try:
docs = iter(fn_return)
except TypeError:
raise click.ClickException("--convert must return dict or iterator")
else:
docs = (fn(doc) for doc in docs)