"""Read-only D01-D06 QA slices and cell verification; never modifies INE workbooks.""" import argparse import hashlib import json from pathlib import Path from demography_workbook import parse cli = argparse.ArgumentParser() cli.add_argument('directory', type=Path) cli.add_argument('--output', type=Path, required=True) args = cli.parse_args() args.output.mkdir(parents=True, exist_ok=True) slices, references = [], [] for name, territory, cell in [('Gaza.xlsx', 'Bilene', None), ('Inhambane.xlsx', 'Funhalouro', 'B54'), ('Maputo Provincia.xlsx', 'Moamba', 'B179')]: original = args.directory / name before = hashlib.sha256(original.read_bytes()).hexdigest() parsed = parse(original) rows = [r for r in parsed['records'] if r['territory'] == territory and 2026 <= r['year'] <= 2028 and r['sex'] == 'all'] unchanged = before == hashlib.sha256(original.read_bytes()).hexdigest() if not unchanged: raise RuntimeError('Source changed during verification') slices.append({'file': name, 'sha256': before, 'source_unchanged': unchanged, 'records': rows}) if cell: matched = [r for r in rows if r['sheet'] == '2026' and r['cell'] == cell] if len(matched) != 1: raise RuntimeError('Reference not uniquely verified') references.append({'file': name, 'sha256': before, 'source_unchanged': unchanged, 'state': 'verified_from_original_labels', 'observation': matched[0]}) initial = args.directory.parent / 'Inhambane.xlsx' if initial.is_file(): before = hashlib.sha256(initial.read_bytes()).hexdigest() matched = [r for r in parse(initial)['records'] if r['sheet'] == '2026' and r['cell'] == 'B54'] if len(matched) != 1 or before != hashlib.sha256(initial.read_bytes()).hexdigest(): raise RuntimeError('Initial Inhambane exemplar not verified') references.append({'file': '../Inhambane.xlsx (exemplar inicial distinto)', 'sha256': before, 'source_unchanged': True, 'state': 'verified_from_original_labels', 'observation': matched[0]}) (args.output / 'source-slices.json').write_text(json.dumps(slices, ensure_ascii=False, indent=2), encoding='utf-8') (args.output / 'source-references.json').write_text(json.dumps(references, ensure_ascii=False, indent=2), encoding='utf-8') print(json.dumps({'files': len(slices), 'verified_cells': len(references), 'originals_unchanged': True}))