"""Extract the user-supplied SQL workbook without executing it.""" import hashlib import json from pathlib import Path import xml.etree.ElementTree as ET import zipfile source = Path('C:/Mac/Home/Documents/TEMPO/Portal Tempo/Servicos Tempo/prompt/cartografia_moz.xlsx') checksum = hashlib.sha256(source.read_bytes()).hexdigest() if checksum != '1e36c769c4767420b10f1ff5ee0c2955cdb1b03c98179c5cad549e832ffb1f15': raise ValueError('Source changed; inspect and approve the new mapping before execution') ns = {'s': 'http://schemas.openxmlformats.org/spreadsheetml/2006/main'} with zipfile.ZipFile(source) as archive: strings = [''.join(item.itertext()) for item in ET.fromstring(archive.read('xl/sharedStrings.xml')).findall('s:si', ns)] def cells(number): result = {} for cell in ET.fromstring(archive.read(f'xl/worksheets/sheet{number}.xml')).findall('.//s:c', ns): value = cell.findtext('s:v', '', ns) if cell.attrib.get('t') == 's': value = strings[int(value)] elif cell.attrib.get('t') == 'inlineStr': value = ''.join(cell.find('s:is', ns).itertext()) if cell.find('s:f', ns) is not None: raise ValueError('SQL package must not depend on evaluated formulas') result[cell.attrib['r']] = value return result ddl_cells, insert_cells = cells(3), cells(10) ddl = [ddl_cells[f'A{row}'] for row in range(1, 15)] statements = [insert_cells[f'B{row}'] for row in range(2, 21416)] tables = ['provincia', 'distrito', 'posto_administrativo', 'localidade', 'bairro_urbano', 'aldeia_quarteirao'] counts = {table: sum(sql.startswith('INSERT INTO ' + table + ' ') for sql in statements) for table in tables} expected = dict(zip(tables, [11, 150, 451, 1377, 2838, 16583])) if counts != expected or statements[2] != 'BEGIN;' or statements[-1] != 'COMMIT;': raise ValueError('Unexpected transaction or counts') quality = cells(11) report = {'source_sha256': checksum, 'ddl_cells': 'SQL_DDL!A1:A14', 'insert_cells': 'SQL_INSERTS!B2:B21415', 'counts': counts, 'total': sum(counts.values()), 'auxiliary_parents_from_source': 1075, 'quality_rows': 1147, 'has_coordinates': False, 'warning': '1075 parents explicitly marked NAO INFORMADO by the workbook; these are unresolved references, not authenticated administrative designations.'} output = Path('storage/app/private/cartography-sql-package.json') output.write_text(json.dumps({'report': report, 'ddl': ddl, 'statements': statements, 'quality_cells': quality}, ensure_ascii=False), encoding='utf-8') Path('docs/investmoz/gis/sql-workbook-inspection.json').write_text(json.dumps(report, ensure_ascii=False, indent=2) + '\n', encoding='utf-8') print(json.dumps(report))