"""Check a saved review and turn it into situations.json. python3 apply_review.py review/k60-s24 # finds the saved review itself python3 apply_review.py review/k60-s24 ~/Downloads/review-k60-s24.json Without a file named, it uses the newest review-*.json in the run directory, and otherwise the newest one in ~/Downloads, where a browser saves it. It prints every problem at once and writes nothing until the review passes; then it writes situations.json into the run directory. """ import datetime, json, os, pathlib, sys import review_format as rf def downloads_dir(): return pathlib.Path(os.environ.get('STP_DOWNLOADS', pathlib.Path.home() / 'Downloads')) def find_review(run_dir, run_id): for folder in (run_dir, downloads_dir()): if folder.is_dir(): found = sorted(folder.glob(f'review-{run_id}*.json'), key=lambda p: p.stat().st_mtime, reverse=True) if found: return found[0] return None def main(argv): if len(argv) not in (2, 3): print(__doc__.strip()) return 2 run_dir = pathlib.Path(argv[1]) cand_path = run_dir / 'candidates.json' if not cand_path.exists(): print(f'{run_dir} has no candidates.json. Check the folder name, or run review.py first.') return 1 candidates = json.loads(cand_path.read_text(encoding='utf-8')) run_id = candidates['run']['id'] review_path = pathlib.Path(argv[2]).expanduser() if len(argv) == 3 else find_review(run_dir, run_id) if review_path is None or not review_path.exists(): print(f'No saved review found for run {run_id}.') print(f'Open {run_dir / "REVIEW.html"} in a browser, and at the end press "Save review file".') print(f'It saves review-{run_id}.json into Downloads, where this looks for it.') return 1 print(f'reading {review_path}') try: form = json.loads(review_path.read_text(encoding='utf-8')) except json.JSONDecodeError: print('That file is damaged and cannot be read. Save the review again from REVIEW.html; ' 'your progress is still in the browser.') return 1 errors, warnings, result = rf.validate(candidates, form) for w in warnings: print(f'note: {w}') if errors: print(f'\nThe review is not finished. {len(errors)} thing(s) to sort out in REVIEW.html, ' f'then save the review file again:') for e in errors: print(f' - {e}') return 1 result['applied'] = datetime.datetime.now().astimezone().isoformat(timespec='seconds') result['review_file'] = str(review_path) out = run_dir / 'situations.json' out.write_text(json.dumps(result, indent=1, ensure_ascii=False), encoding='utf-8') c = result['counts'] print(f'\n{out}: {c["situations"]} situations ({c["kept"]} kept, {c["same"]} merged in, ' f'{c["dropped"]} dropped, {c["left_out"]} extras left out).') for s in result['situations']: print(f' {s["id"]} {s["situation"]} ({s["size"]} scenes, {s["stories"]} stories)') return 0 if __name__ == '__main__': sys.exit(main(sys.argv))