from pathlib import Path import json,re,collections out=Path(__file__).resolve().parent d=json.loads((out/'FONTES-EXTRAIDAS.json').read_text('utf-8')) books=[] for x in d['selection']: if 'sheets' not in x:continue cells=x['sheets'][0]['cells'];rows={} for c in cells: m=re.fullmatch('([A-Z]+)([0-9]+)',c['cell']);rows.setdefault(int(m[2]),{})[m[1]]=c['value'] records=[{'sheet_row':r,'source_id':v.get('A'),'code':v.get('B'),'parent_source_id':v.get('C'),'level':v.get('D'),'flag':v.get('E'),'label':v.get('F')} for r,v in rows.items() if r>1] byid={r['source_id']:r for r in records} selected=[r for r in records if (r['code'] or '') in ['A','01','011','0111','01111','01110','0112','01120','10','106','1061','10611','10610']] issues={'duplicate_source_ids':[k for k,v in collections.Counter(r['source_id'] for r in records).items() if v>1], 'duplicate_codes':[k for k,v in collections.Counter(r['code'] for r in records).items() if v>1], 'unresolved_parents':[r for r in records if r['parent_source_id'] not in [None,'','0'] and r['parent_source_id'] not in byid]} books.append({'path':x['path'],'sha256':x['sha256'],'sheet':x['sheets'][0]['name'],'records':records,'counts':{'records':len(records),'levels':dict(collections.Counter(r['level'] for r in records)),'flags':dict(collections.Counter(r['flag'] for r in records))},'selected_branch':selected,'issues':issues}) result={'books':books,'bytes_equal':books[0]['sha256']==books[1]['sha256'],'semantic_cells_equal':d['selection'][0]['sheets']==d['selection'][1]['sheets']} (out/'CAE-ESTRUTURA.json').write_text(json.dumps(result,ensure_ascii=False,indent=2)+'\n','utf-8') brief=[{k:v for k,v in b.items() if k!='records'} for b in books] (out/'CAE-RESUMO.json').write_text(json.dumps(brief,ensure_ascii=False,indent=2)+'\n','utf-8') print(json.dumps({'books':len(books),'bytes_equal':result['bytes_equal'],'semantic_cells_equal':result['semantic_cells_equal'],'counts':[b['counts'] for b in books]}))