"""python -B second_195_layout.py manifest.json . The selector consumes positions and lengths only."""
from line_mechanism_108 import geometry
from pathlib import Path
import json,hashlib,sys
D=Path(__file__).resolve().parent
def run():
    g,w=geometry()
    layout=[(a['rune_offset'],len(a['values']),a['section'],a['page'],a['page_rune_offset']) for a in w]
    out=[]
    for p,left,right in [(27,3,2),(17,4,0),(48,8,1),(51,3,1)]:
        boundary=max(i for i,z in enumerate(g) if z[0]==p)+1
        first=boundary-left;length=left+right;target=next(row for row in layout if row[0]==first and row[1]==length)
        future=[row for row in layout if row[0]>=boundary+64 and row[2]==target[2] and row[1]==length]
        chosen={str(d):next((list(row) for row in future if (row[0]-first)%3==d),None) for d in (1,2)}
        out.append(dict(page=p,cut=boundary,anchor=target,targets=future,chosen=chosen))
    return dict(source_sha256=hashlib.sha256((D/'liber-primus__transcription--master.txt').read_bytes()).hexdigest(),fields=['global_rune_offset','word_length','section','LP2_start_page','page_rune_offset'],sites=out)
if __name__=='__main__':Path(sys.argv[1]).write_text(json.dumps(run(),indent=1)+'\n')
