
Boss: The Morning Archive Run

Every dawn, keepers drop their ledgers into the lighthouse inbox.
The Guild wants them merged into ledger.json before anyone wakes
up. But the Batch Bug has been sneaking bad rows and broken
headers into the pile!
Build the Morning Archive Run: a tool that discovers, validates, merges and reports. It must have a preview mode you can trust, and a rerun must change nothing.
On a real computer
You'd save this as morning.py, end it with
if __name__ == "__main__": main(sys.argv[1:]), and ask the computer
to run python morning.py inbox --apply every morning. Use cron on
macOS and Linux, or Task Scheduler on Windows. Here we call main()
with a list instead.
Assignment
-
discover(inbox): a sorted list ofPaths for the.csvfiles directly ininbox. -
read_batch(path)returns(rows, problems). Use the rules from Keep the Reject Reasons. Rows are{"id": ..., "item": ..., "qty": <int>}. Problems are strings:"w3.csv: bad header","w2.csv line 3: missing item","w2.csv line 4: bad qty". -
morning_run(inbox, ledger_path, apply=False)loads the ledger ({}if missing) and merges every row byid, like The Repeatable Import. It returns{"mode": "preview" or "apply", "files": <count>, "added": a, "updated": u, "unchanged": n, "problems": [all problem strings]}. Only whenapplyisTruedoes it save the ledger (indent=2, sort_keys=True). A preview writes nothing. -
report(result)returns this text (one-line per problem):Morning run (preview) Files: 3 Added: 2, Updated: 1, Unchanged: 0 Problems: 1 - w3.csv: bad header -
main(argv)parsesargvwithargparse: a positionalinbox,--ledger(default"ledger.json") and an--applyswitch. It runsmorning_run, printsreport(result)and returnsresult.
-
discover(inbox): a sorted list ofPaths for the.csvfiles directly ininbox. -
read_batch(path)returns(rows, problems). Use the rules from Keep the Reject Reasons. Rows are{"id": ..., "item": ..., "qty": <int>}. Problems are strings:"w3.csv: bad header","w2.csv line 3: missing item","w2.csv line 4: bad qty". -
morning_run(inbox, ledger_path, apply=False)loads the ledger ({}if missing) and merges every row byid, like The Repeatable Import. It returns{"mode": "preview" or "apply", "files": <count>, "added": a, "updated": u, "unchanged": n, "problems": [all problem strings]}. Only whenapplyisTruedoes it save the ledger (indent=2, sort_keys=True). A preview writes nothing. -
report(result)returns this text (one-line per problem):Morning run (preview) Files: 3 Added: 2, Updated: 1, Unchanged: 0 Problems: 1 - w3.csv: bad header -
main(argv)parsesargvwithargparse: a positionalinbox,--ledger(default"ledger.json") and an--applyswitch. It runsmorning_run, printsreport(result)and returnsresult.
Press Run to try your code, then Check to test it.