fix(manual): 手工导出骨架与导入边界修复,单位按目录自然序排列
- 上传口骨架收集改为空操作,骨架仅经升级入库(决策 231/补记十四) - s_lme_lib 价格快照垫底,修手工会话组成子行价格落 0 - 导出编辑相交消解:删除块与闭合前插入冲突时重映射,防采纳定额丢失/碎 XML - _span 识别自闭合标签,防容器配对失败留孤儿闭合 - 手工导入放开空名/同名/深层级校验 - 单位顺序改按左侧目录树序(单项入库序+组内编号自然序) - 补齐手工导出/导入/MCP 切换相关测试
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""经典整件骨架库(决策 229,2026-10-27 主人定):
|
||||
"""经典整件骨架库(决策 231,2026-10-27 主人定):
|
||||
|
||||
手工工程(无源文件,决策 119/230)没有「本项目原件」可当导出模板——把**经典项目的
|
||||
整件原件**收进骨架库当模板:结构字段相同(同签名)只存一件;每组保留**最全**的代表
|
||||
@@ -16,6 +16,8 @@
|
||||
存储:history.db classic_skel 一组一行(连代表件原件字节存库,不随会话清理)。
|
||||
"""
|
||||
import hashlib
|
||||
import sqlite3
|
||||
import io
|
||||
import os
|
||||
import re
|
||||
import time
|
||||
@@ -92,40 +94,120 @@ def extract(path):
|
||||
return {"sig": sig, "suffix": suf, "n_shapes": len(shapes), "n_nodes": n_nodes}
|
||||
|
||||
|
||||
def _decode(raw):
|
||||
m = re.match(rb'^\s*<\?xml[^>]*encoding="([^"]*)"', raw[:200])
|
||||
enc = m.group(1).decode("ascii") if m else "utf-8"
|
||||
try:
|
||||
return raw.decode(enc, errors="replace")
|
||||
except Exception:
|
||||
return raw.decode("utf-8", errors="replace")
|
||||
|
||||
|
||||
def collect(con, sha, path, name=""):
|
||||
"""上传口统一入口:抽取并按组收代表。返回 'new'/'rep'/'old'/None
|
||||
(新组 / 更全顶掉旧代表 / 平手取最新 / 比代表瘦没动 / 不成组)。
|
||||
⚠️ con=history.db 连接;失败绝不挡上传(调用方兜 try)。"""
|
||||
"""上传口骨架收集=**空操作**(补记十四,2026-10-28 主人定「后期不存骨架了,
|
||||
现有也只有 2 个骨架」):骨架只经升级(seed_frame)入库;上传什么原件都不进
|
||||
classic_skel,库里只保留已升级固定的骨架(COS4骨架G/Y…)。保留函数签名供上传口
|
||||
兼容调用。"""
|
||||
return None
|
||||
|
||||
def rep_for(con, suffix):
|
||||
"""某后缀的当前代表件 → dict;该后缀还没收过 → None(导出侧提示先导入原件)。
|
||||
补记五:专用骨架件(is_frame=1)优先于实项目件。"""
|
||||
_ensure_table(con)
|
||||
try:
|
||||
con.execute("SELECT is_frame FROM classic_skel LIMIT 1")
|
||||
except Exception:
|
||||
return dict(con.execute(
|
||||
"SELECT * FROM classic_skel WHERE suffix=? ORDER BY n_nodes DESC, "
|
||||
"extracted_at DESC LIMIT 1", (suffix,)).fetchone() or ())
|
||||
row = con.execute("""SELECT * FROM classic_skel WHERE suffix=?
|
||||
ORDER BY is_frame DESC, n_nodes DESC, extracted_at DESC
|
||||
LIMIT 1""", (suffix,)).fetchone()
|
||||
return dict(row) if row else None
|
||||
|
||||
|
||||
def seed_frame(con, path, name):
|
||||
con.row_factory = sqlite3.Row
|
||||
"""升级添加骨架(补记十二,主人定:骨架只经升级进库、上传口不自动收;命名不带
|
||||
软件名——COS4骨架G/Y、COS骨架G/Y…)。同名骨架=升级替换(重写签名/字节);
|
||||
新名=新增固定骨架。返回 sig;解析失败返回 None。"""
|
||||
info = extract(path)
|
||||
if not info:
|
||||
return None
|
||||
_ensure_table(con)
|
||||
row = con.execute("SELECT n_nodes, extracted_at FROM classic_skel WHERE sig=?",
|
||||
(info["sig"],)).fetchone()
|
||||
if row is not None and info["n_nodes"] < (row["n_nodes"] or 0):
|
||||
return "old" # 不如代表全:不收(主人定:取最全)
|
||||
_cols = {r[1] for r in con.execute("PRAGMA table_info(classic_skel)")}
|
||||
if "is_frame" not in _cols:
|
||||
con.execute("ALTER TABLE classic_skel ADD COLUMN is_frame INTEGER DEFAULT 0")
|
||||
old = con.execute("SELECT sig FROM classic_skel WHERE original_name=?", (name,)).fetchone()
|
||||
if old and old["sig"] != info["sig"]:
|
||||
con.execute("DELETE FROM classic_skel WHERE original_name=?", (name,))
|
||||
with open(path, "rb") as f:
|
||||
blob = f.read()
|
||||
con.execute("""INSERT INTO classic_skel(sig,suffix,rep_sha,original_name,
|
||||
n_shapes,n_nodes,blob,extracted_at)
|
||||
VALUES(?,?,?,?,?,?,?,?)
|
||||
n_shapes,n_nodes,blob,extracted_at,is_frame)
|
||||
VALUES(?,?,?,?,?,?,?,?,1)
|
||||
ON CONFLICT(sig) DO UPDATE SET rep_sha=excluded.rep_sha,
|
||||
original_name=excluded.original_name, n_shapes=excluded.n_shapes,
|
||||
n_nodes=excluded.n_nodes, blob=excluded.blob,
|
||||
extracted_at=excluded.extracted_at""",
|
||||
(info["sig"], info["suffix"], sha, os.path.basename(name or path),
|
||||
extracted_at=excluded.extracted_at, is_frame=1""",
|
||||
(info["sig"], info["suffix"],
|
||||
hashlib.sha256(blob).hexdigest(), name,
|
||||
info["n_shapes"], info["n_nodes"], blob, _now()))
|
||||
con.commit() # sqlite3 隐式事务:不提交=连接关闭即回滚(DDL 却即时生效)
|
||||
return "new" if row is None else "rep"
|
||||
con.commit()
|
||||
return info["sig"]
|
||||
|
||||
|
||||
def rep_for(con, suffix):
|
||||
"""某后缀的当前代表件 → dict;该后缀还没收过 → None(导出侧提示先导入原件)。"""
|
||||
def _shapes_of_blob(blob):
|
||||
"""整包字节 → 结构形态集合(与 extract 同口径:标签+字段名集+有无子级)。"""
|
||||
shapes = set()
|
||||
try:
|
||||
with zipfile.ZipFile(io.BytesIO(blob)) as z:
|
||||
for nm in z.namelist():
|
||||
if not nm.lower().endswith(".xml"):
|
||||
continue
|
||||
s, _n = _shapes_of_bytes(z.read(nm))
|
||||
shapes |= s
|
||||
except Exception:
|
||||
pass
|
||||
return shapes
|
||||
|
||||
|
||||
def pick_frame(con, suffix, src_path=None):
|
||||
"""导出骨架挑选(补记十二,主人定「按字段最近的一个」):
|
||||
①该后缀的固定骨架(is_frame=1,可多件并存)里有源文件可比 → 结构字段集 Jaccard
|
||||
相似度最高者;②无源文件(手工工程)/比不了 → 最近收藏的骨架(extracted_at 最新,
|
||||
即主人最后指定的那件)。没有固定骨架 → 退 rep_for(老行为)。"""
|
||||
_ensure_table(con)
|
||||
row = con.execute("""SELECT * FROM classic_skel WHERE suffix=?
|
||||
ORDER BY n_nodes DESC, extracted_at DESC LIMIT 1""",
|
||||
(suffix,)).fetchone()
|
||||
return dict(row) if row else None
|
||||
try:
|
||||
frames = con.execute("SELECT * FROM classic_skel WHERE suffix=? AND is_frame=1",
|
||||
(suffix,)).fetchall()
|
||||
except Exception:
|
||||
frames = []
|
||||
if not frames:
|
||||
return rep_for(con, suffix)
|
||||
if len(frames) == 1:
|
||||
return dict(frames[0])
|
||||
if src_path and os.path.exists(src_path):
|
||||
try:
|
||||
with zipfile.ZipFile(src_path) as z:
|
||||
src_shapes = set()
|
||||
for nm in z.namelist():
|
||||
if not nm.lower().endswith(".xml"):
|
||||
continue
|
||||
s, _n = _shapes_of_bytes(z.read(nm))
|
||||
src_shapes |= s
|
||||
except Exception:
|
||||
src_shapes = set()
|
||||
if src_shapes:
|
||||
best, best_j = None, -1.0
|
||||
for f in frames:
|
||||
fsh = _shapes_of_blob(f["blob"])
|
||||
j = len(src_shapes & fsh) / max(1, len(src_shapes | fsh))
|
||||
if j > best_j:
|
||||
best, best_j = f, j
|
||||
if best is not None:
|
||||
return dict(best)
|
||||
return dict(max(frames, key=lambda f: (f["extracted_at"] or "", f["n_nodes"] or 0)))
|
||||
|
||||
|
||||
def stats(con):
|
||||
|
||||
Reference in New Issue
Block a user