scripts: Tweaked csv read/writing to pass notes through

This was, uh, half-implemented in csv.py's collect_csv, but completely
ignored in read_csv/write_csv. Adding support to read_csv/write_csv
wasn't too hard, so maybe we should keep this?

There's an argument notes should not be included in csv output, as the
nested commas (for multiple results) can make a mess of csv's simplicty.
(There's a different argument that csv is a terrible format, but I'm not
sure I agree.)

But for now, including notes doesn't seem to harm anything.

---

Note this also includes a fix for filtering empty notes in csv.py's
collect_csv.
This commit is contained in:
Christopher Haster
2026-03-05 10:06:50 -06:00
parent e521e21764
commit ecc72e3dba
9 changed files with 163 additions and 44 deletions
+18 -5
View File
@@ -937,10 +937,10 @@ def read_csv(path, Result, *,
with openio(path, 'r') as f:
# csv or json? assume json starts with [
is_json = (f.buffer.peek(1)[:1] == b'[')
json = (f.buffer.peek(1)[:1] == b'[')
# read csv?
if not is_json:
if not json:
results = []
reader = csv.DictReader(f, restval='')
for r in reader:
@@ -955,7 +955,13 @@ def read_csv(path, Result, *,
and r[k].strip()}
| {k: r[prefix+k] for k in fields
if prefix+k in r
and r[prefix+k].strip()})))
and r[prefix+k].strip()}
| ({Result._notes: set(n.strip()
for n in r[Result._notes].split(',')
if n.strip())}
if hasattr(Result, '_notes')
and Result._notes in r
else {}))))
except TypeError:
pass
return results
@@ -1021,7 +1027,9 @@ def write_csv(path, Result, results, *,
writer = csv.DictWriter(f, list(
co.OrderedDict.fromkeys(it.chain(
by,
(prefix+k for k in fields))).keys()))
(prefix+k for k in fields),
[Result._notes] if hasattr(Result, '_notes')
else [])).keys()))
writer.writeheader()
for r in results:
# note this allows by/fields to overlap
@@ -1031,7 +1039,12 @@ def write_csv(path, Result, results, *,
if getattr(r, k) is not None}
| {prefix+k: getattr(r, k).__csv__()
for k in fields
if getattr(r, k) is not None})
if getattr(r, k) is not None}
| ({Result._notes: ','.join(
getattr(r, Result._notes))}
if hasattr(Result, '_notes')
and getattr(r, Result._notes)
else {}))
# write json?
else: