# -*- coding: utf-8 -*- """xmldiff:实件 AttrInfo 区 vs 导出件 AttrInfo 区——结构差异精确定位。""" import sys sys.path.insert(0, '.') import classic_skeleton as cs import sqlite3 import zipfile import io import re import xml.etree.ElementTree as ET from xmldiff import main, formatting con = sqlite3.connect(r'D:/PricingReview/CostReview/data/history.db') con.row_factory = sqlite3.Row def extract_attrinfo(xml_text): i = xml_text.find('') if i < 0: i = xml_text.find('', i) + len('') return xml_text[i:j] # ① 广联达件:实件 vs 导出件 rg = con.execute("SELECT blob FROM classic_skel WHERE original_name LIKE '%广联达导出最新%'").fetchone() zg = zipfile.ZipFile(io.BytesIO(rg['blob'])) # 实件最全成员的 AttrInfo 区 best = None for nm in zg.namelist(): if not nm.lower().endswith('.xml') or 'Project' in nm: continue t = zg.read(nm).decode('utf-8', errors='replace') if '' in t: n = len(re.findall(r'<[A-Za-z_]', t)) if best is None or n > best[0]: best = (n, nm, t) orig_attr = extract_attrinfo(best[2]) open('_orig_gld_attr.xml', 'w', encoding='utf-8').write(orig_attr) # ② 导出件 AttrInfo 区 raw_exp, name_exp, err = None, None, None import export_v2 as _ev2 sc = sqlite3.connect(r'D:/PricingReview/CostReview/data/session.db') sc.row_factory = sqlite3.Row raw_exp, name_exp, err = _ev2.export_v2(con, sc, 1, 'cos4') z = zipfile.ZipFile(io.BytesIO(raw_exp)) units = [i for i in z.infolist() if 'Project' not in (i.filename.encode('cp437').decode('gbk') if not (i.flag_bits & 0x800) else i.filename)] t_exp = z.read(units[0]).decode('utf-8') exp_attr = extract_attrinfo(t_exp) open('_exp_gld_attr.xml', 'w', encoding='utf-8').write(exp_attr) print('=== xmldiff:广联达实件 AttrInfo vs 导出件 AttrInfo ===') diff = main.diff_texts(orig_attr, exp_attr) for d in diff: print(' ', d) print() # ③ 易达件同理 ry = con.execute("SELECT blob FROM classic_skel WHERE original_name LIKE '%1易达%'").fetchone() zy = zipfile.ZipFile(io.BytesIO(ry['blob'])) best_y = None for nm in zy.namelist(): if not nm.lower().endswith('.xml') or 'Project' in nm: continue t = zy.read(nm).decode('utf-8', errors='replace') if '' in t: n = len(re.findall(r'<[A-Za-z_]', t)) if best_y is None or n > best_y[0]: best_y = (n, nm, t) orig_attr_y = extract_attrinfo(best_y[2]) open('_orig_yida_attr.xml', 'w', encoding='utf-8').write(orig_attr_y) print('=== xmldiff:易达实件 AttrInfo vs 导出件 AttrInfo ===') diff_y = main.diff_texts(orig_attr_y, exp_attr) for d in diff_y: print(' ', d)