"""PCB render and comparison inside the existing parser resource boundary.""" from . import parse_task # Apply CPU/address-space/output/FD limits before parsing. import json import sys from pathlib import Path from .documents import parse_document from .pcb_diff import compare_pcbs from .parser.record_decoder import ParseError from .render import svg_document def run(): source,destination,filename,identifier,kind=sys.argv[1:] dest=Path(destination) try: if kind=='pcb_comparison': manifest=json.loads(Path(source).read_text()) if manifest.get('schema_version')!='1.0' or manifest.get('document_type')!='pcb_comparison': raise ParseError('Unsupported PCB comparison manifest') old=parse_document((dest/'old.PcbDoc').read_bytes(),manifest['old_filename'],identifier+'-old',document_type='pcb') new=parse_document((dest/'new.PcbDoc').read_bytes(),manifest['new_filename'],identifier+'-new',document_type='pcb') report=compare_pcbs(old,new) (dest/'data.json').write_text(json.dumps(report,ensure_ascii=False),encoding='utf-8') for side,doc in [('old',old),('new',new)]: annotations={c[f'{side}_id']:dict(id=c['id'],status=c['status'],bounds=c[f'{side}_bounds']) for c in report['changes'] if c[f'{side}_id'] is not None} (dest/f'{side}.svg').write_bytes(svg_document(doc,annotations,id_prefix=side+'-')) summary=dict(summary=report['summary'],total_changed=report['total_changed']) elif kind=='pcb_render': doc=parse_document(Path(source).read_bytes(),filename,identifier,document_type='pcb') (dest/'data.json').write_text(doc.model_dump_json(),encoding='utf-8') (dest/'drawing.svg').write_bytes(svg_document(doc)) summary={k:doc.metadata[k] for k in ('compatibility','record_count','component_count','primitive_count','net_count')} summary.update(schema_version=doc.schema_version,diagnostics=[d.model_dump() for d in doc.diagnostics],layers=[l.model_dump() for l in doc.layers]) else:raise ParseError('Unsupported PCB job kind') result=dict(ok=True,summary=summary) except (ParseError,ValueError) as exc: result=dict(ok=False,code='invalid_pcb',message=str(exc)[:500]) except Exception: result=dict(ok=False,code='pcb_processing_error',message='PCB processing could not be completed') (dest/'result.json').write_text(json.dumps(result),encoding='utf-8') if __name__=='__main__':run()