"""Builds the stored-MAPEM bundle the app ships with, from the MAPEM test corpus. python tools/mapem_bundle.py Reads app/src/test/resources/mapem/corpus.txt (tools/mapem_corpus.py: every distinct MAPEM on record, UPER plus asn1tools' fields) and writes app/src/main/assets/mapem/hamburg.txt: one UPER MAPEM per intersection and revision. Intersections without a region are left out: their id is not unique, and the only one on record is the bench RSU's test crossroads. """ import re from pathlib import Path ROOT = Path(__file__).resolve().parent.parent CORPUS = ROOT / 'app/src/test/resources/mapem/corpus.txt' OUT = ROOT / 'app/src/main/assets/mapem/hamburg.txt' RECORDED = '2026-03-18' def main(): messages = [] # (hex, [(key, revision, name)]) for line in CORPUS.read_text(encoding='ascii').splitlines(): if line.startswith('#hex '): messages.append((line[5:], [])) elif line.startswith('I '): key, revision, name = re.match(r'I key=(\S+) rev=(\d+) name=(.*?) ref=', line).groups() messages[-1][1].append((key, int(revision), name)) seen = set() body = [] for uper, intersections in messages: new = {(k, r) for k, r, _ in intersections if not k.startswith('-1/')} - seen if not new: continue seen |= new body += [f'# {k} rev {r} {name}' for k, r, name in intersections] body.append(uper) header = [f'# Stored MAPEMs, recorded on the {RECORDED} drive in Hamburg. {len(seen)} intersection revisions.', '# One UPER MAPEM per line, hex. Generated by tools/mapem_bundle.py; do not edit.'] OUT.parent.mkdir(parents=True, exist_ok=True) OUT.write_text('\n'.join(header + body) + '\n', encoding='utf-8', newline='\n') print(f'{OUT.relative_to(ROOT)}: {len(seen)} intersection revisions, {len({k for k, _ in seen})} intersections') if __name__ == '__main__': main()