Boss

Boss: The Morning Archive Run

The boss
Boss Fight!
Every passing test breaks a shield.

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

  1. discover(inbox): a sorted list of Paths for the .csv files directly in inbox.

  2. 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".

  3. morning_run(inbox, ledger_path, apply=False) loads the ledger ({} if missing) and merges every row by id, like The Repeatable Import. It returns {"mode": "preview" or "apply", "files": <count>, "added": a, "updated": u, "unchanged": n, "problems": [all problem strings]}. Only when apply is True does it save the ledger (indent=2, sort_keys=True). A preview writes nothing.

  4. 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
  5. main(argv) parses argv with argparse: a positional inbox, --ledger (default "ledger.json") and an --apply switch. It runs morning_run, prints report(result) and returns result.