feat(import): 决策265 解析器支持 ESCC2.0 广东省造价数据标准(明文 XML)
- sniff 加 esc 分支(头部 <Projects);parse_file 分流;parse_esc 照 parse_swt 同款 iterparse+out 容器(约 200 行) - 覆盖:分部/清单(措施 Kind=prelim)/定额 WorkContent 全收/定额消耗/工程量表达式/项目+单位级 Resource(决策122A 价口径)/uw_summary/项目特征;重名单位工程 Number 自动去重 - s_lme_proj 99、s_lme_uw 391 = 决策201「未引用即删」正确行为,s_lme_lib 126 全量兜底(测试按此断言) - 推翻 2026-10-01「不再支持 xml」ESCC 部分;其他散装 XML 仍拒(test_api_p0 改无名根样料) - test_esc_import.py 3 项留库;369 passed / 3 skipped;IAB 全链路实测(样本 y程ESCC2.xml)
This commit is contained in:
@@ -109,6 +109,10 @@ def sniff(path):
|
||||
# 认出来单独走 parse_swt;其余散装 xml 仍按原口径拒绝(决策:不再支持 xml 格式)。
|
||||
if b"<ConstructProject" in head:
|
||||
return "swt"
|
||||
# 2026-11-03 主人定(推翻 2026-10-01「不再支持 xml」中 ESCC 部分):广东省造价数据
|
||||
# 标准 ESCC2.0 明文 XML,根节点 Projects——认出来走 parse_esc;其他散装 xml 仍拒绝。
|
||||
if b"<Projects" in head:
|
||||
return "esc"
|
||||
return "xml"
|
||||
return "unknown"
|
||||
|
||||
@@ -939,6 +943,256 @@ def parse_swt(path, out):
|
||||
return "SWT"
|
||||
|
||||
|
||||
# ---------- 解析:ESCC2.0(广东省建设工程造价数据标准,DBJ/T 15-145-2018,明文 XML)----------
|
||||
# 2026-11-03 主人定:恢复散装 xml 中 ESCC2.0 的支持(样本 y程ESCC2.xml;推翻 2026-10-01
|
||||
# 「不再支持 xml」中 ESCC 部分——其他散装 XML 仍拒绝)。
|
||||
# 层级(实测 珠海高新区2024地灾治理):
|
||||
# Projects(项目级:TotalCosts/InfoList/AttrInfos/Resource 价格表)
|
||||
# └ ProjectGroup(单项工程)→ ProjectItem(单位工程)
|
||||
# ├ ProjectCosts(单位费用 CostItem) ├ Summary(单位汇总分析 SummaryItem 树)
|
||||
# ├ BillTable/Branch(分部)→ Bill(清单)→ Norm(定额,WorkContent 组内多条全收)
|
||||
# │ └ ResElements/ResElementItem(定额消耗) ├ ExpressElement(工程量表达式)
|
||||
# └ MeasureTable/Branch→Bill(措施项目,Kind='prelim')
|
||||
# 与 parse_std/parse_swt 完全相同的 out 结构,save() 原样复用。
|
||||
# ResourceItem 的 Price=除税预算价、OrgPrice=除税 org —— ESCC 无含税价列,照 SWT 决策 122
|
||||
# A 口径:NoTaxPrice=TaxPrice=Price、TaxRate=0;项目级 Resource 全量进 lme_proj/lme_lib,
|
||||
# 各 ProjectItem 若自带 Resource 则本单位另存(与 parse_std 的单位级汇总同口径)。
|
||||
|
||||
def parse_esc(path, out):
|
||||
out.setdefault("lme_res", {})
|
||||
out.setdefault("norm_res", [])
|
||||
out.setdefault("lme_uw", [])
|
||||
out.setdefault("lme_lib", {})
|
||||
out.setdefault("lme_proj", {})
|
||||
out.setdefault("lme_name", {})
|
||||
out.setdefault("divides", [])
|
||||
out.setdefault("sections", [])
|
||||
out.setdefault("uw_cost", {})
|
||||
out.setdefault("uw_spec", {})
|
||||
|
||||
st = {"sp": None, "unit": None, "in_bill": False, "in_measure": False,
|
||||
"div_stack": [], "mdiv_stack": [], "cur_bill": -1, "cur_norm": -1,
|
||||
"sec_seen": set(), "uw_nums": set()}
|
||||
|
||||
def add_divide(kind, number, name, total, parent=None):
|
||||
u = st["unit"]
|
||||
idx = len(out["divides"])
|
||||
out["divides"].append({"uw": u["idx"] if u else -1, "parent": parent,
|
||||
"kind": kind, "number": number, "name": name,
|
||||
"total": total, "code": None, "seq": idx})
|
||||
return idx
|
||||
|
||||
def push_bill(a, kind):
|
||||
out["bill_raw"] = out.get("bill_raw", 0) + 1
|
||||
out["bills"].append((a.get("Code", ""), a.get("Name", ""), a.get("Attr", ""),
|
||||
(a.get("Unit") or "").strip(), f(a.get("Quantity")),
|
||||
f(a.get("Price")), f(a.get("Total")),
|
||||
(a.get("Remark") or "").strip()))
|
||||
st["cur_bill"] = len(out["bills"]) - 1
|
||||
out.setdefault("bill_uw", []).append(st["unit"]["idx"] if st["unit"] else -1)
|
||||
out.setdefault("bill_kind", []).append(kind)
|
||||
out.setdefault("bill_div", []).append(
|
||||
(st["mdiv_stack"][-1] if st["mdiv_stack"] else None) if kind == "prelim"
|
||||
else (st["div_stack"][-1] if st["div_stack"] else None))
|
||||
st["cur_norm"] = -1
|
||||
|
||||
def costs_from(container):
|
||||
"""CostItem 列表 → COST_KEYS 六值(ESCC Code 直接对齐)。"""
|
||||
by = {(x.get("Code") or "").strip(): f(x.get("Total")) for x in container}
|
||||
c = {k: by.get(_k) for k, _k in (
|
||||
("DivisionalAndElementalWorks", "QDF"), ("Preliminaries", "QSF"),
|
||||
("SundryCosts", "QTF"), ("Tax", "SJ"))}
|
||||
c["CostForHSE"] = by.get("AQFHWMSG")
|
||||
qdf, qsf, qtf, gf = c["DivisionalAndElementalWorks"], c["Preliminaries"], c["SundryCosts"], by.get("GF")
|
||||
c["Construction"] = (qdf + qsf + qtf + gf) if None not in (qdf, qsf, qtf, gf) else (
|
||||
by.get("SQGCZJ"))
|
||||
c["_sc_total"] = by.get("ZZJ")
|
||||
return c
|
||||
|
||||
for event, el in ET.iterparse(path, events=("start", "end")):
|
||||
tag = _local_tag(el.tag)
|
||||
a = el.attrib
|
||||
if event == "start":
|
||||
if tag == "Projects":
|
||||
out["proj_name"] = a.get("Name", "")
|
||||
out["total"] = f(a.get("Total"))
|
||||
out["info"] = {k: v for k, v in a.items() if (v or "").strip()}
|
||||
out.setdefault("file_kind", None)
|
||||
out["bulid_unit"] = a.get("BulidUnit", "") or ""
|
||||
elif tag == "ProjectGroup":
|
||||
st["sp"] = {"number": (a.get("Name") or "").strip(),
|
||||
"name": (a.get("Name") or "").strip(),
|
||||
"total": f(a.get("Total")), "ratios": f(a.get("Ratios")),
|
||||
"uw_nums": []}
|
||||
elif tag == "ProjectItem":
|
||||
idx = len(out["uw"])
|
||||
base = (a.get("Number") or "").strip() or ("UW%d" % (idx + 1))
|
||||
num, k = base, 2
|
||||
while num in st["uw_nums"]: # 样本 Number 有重码,必须去重
|
||||
num, k = "%s-%d" % (base, k), k + 1
|
||||
st["uw_nums"].add(num)
|
||||
out["uw"].append((num, a.get("Name", ""), f(a.get("Total"))))
|
||||
out["uw_spec"][idx] = (a.get("Name") or "").strip() or None
|
||||
st["unit"] = {"idx": idx, "num": num}
|
||||
st["in_bill"] = st["in_measure"] = False
|
||||
st["div_stack"] = []
|
||||
st["mdiv_stack"] = []
|
||||
st["cur_bill"] = st["cur_norm"] = -1
|
||||
if st["sp"] is not None:
|
||||
st["sp"]["uw_nums"].append(num)
|
||||
elif tag == "BillTable":
|
||||
st["in_bill"] = True
|
||||
elif tag == "MeasureTable":
|
||||
st["in_measure"] = True
|
||||
elif tag in ("Branch",):
|
||||
if st["in_measure"]:
|
||||
st["mdiv_stack"].append(add_divide(
|
||||
"prelim", "", (a.get("Name") or "").strip(), f(a.get("Total")),
|
||||
st["mdiv_stack"][-1] if st["mdiv_stack"] else None))
|
||||
elif st["in_bill"]:
|
||||
st["div_stack"].append(add_divide(
|
||||
"item", "", (a.get("Name") or "").strip(), f(a.get("Total")),
|
||||
st["div_stack"][-1] if st["div_stack"] else None))
|
||||
elif tag == "Bill":
|
||||
push_bill(a, "prelim" if st["in_measure"] else None)
|
||||
elif tag == "Norm":
|
||||
if st["cur_bill"] >= 0:
|
||||
st["cur_norm"] = len(out["norms"])
|
||||
out["norms"].append((st["cur_bill"], a.get("Code", ""), a.get("Name", ""),
|
||||
(a.get("Unit") or "").strip(), f(a.get("Quantity")),
|
||||
f(a.get("Price")), f(a.get("Total"))))
|
||||
elif tag == "ResElementItem":
|
||||
# 定额消耗(在 Norm 内)或清单级资源引用(ResElements 直挂 Bill)——后者跳过
|
||||
if st["cur_norm"] >= 0:
|
||||
out["norm_res"].append((st["cur_norm"], (a.get("ResCode") or "").strip(),
|
||||
f(a.get("Quantity")), None))
|
||||
elif tag == "ExpressItem":
|
||||
# 工程量表达式(清单/定额都可有):GCL 公式原样(会话库 qty_expr 通道)
|
||||
if st["cur_norm"] < 0 and st["cur_bill"] >= 0:
|
||||
ex = (a.get("Express") or "").strip()
|
||||
if ex:
|
||||
out.setdefault("bill_expr", {})[st["cur_bill"]] = ex
|
||||
elif tag == "ResourceItem":
|
||||
code = (a.get("Code") or "").strip()
|
||||
if not code:
|
||||
pass
|
||||
elif st["unit"] is not None and st.get("_uw_res"):
|
||||
# 单位工程自带 Resource(样本未见,防未来变体):本单位另存一份
|
||||
price = f(a.get("Price"))
|
||||
qty = f(a.get("Quantity"))
|
||||
nm, sp, un = (a.get("Name") or "").strip(), (a.get("Specification") or "").strip(), (a.get("Unit") or "").strip()
|
||||
kd = (a.get("Kind") or "").strip() or None
|
||||
out["lmes"].append((code, nm, sp, un, price, 0.0, price, qty, kd))
|
||||
out["lme_uw"].append((st["unit"]["idx"], code, nm, sp, un, price, 0.0,
|
||||
price, qty, kd, None, None, None, None))
|
||||
elif st["unit"] is None:
|
||||
# 项目级 Resource 价格表 → lme_lib/lme_proj(决策 122 A:除税=含税=Price)
|
||||
price, org = f(a.get("Price")), f(a.get("OrgPrice"))
|
||||
qty = f(a.get("Quantity"))
|
||||
nm, sp, un = (a.get("Name") or "").strip(), (a.get("Specification") or "").strip(), (a.get("Unit") or "").strip()
|
||||
kd = (a.get("Kind") or "").strip() or None
|
||||
out["lme_lib"].setdefault(code, [nm, sp, un, kd, org, price])
|
||||
out["lme_name"].setdefault(code, nm)
|
||||
p = out["lme_proj"].get(code)
|
||||
if p is None:
|
||||
out["lme_proj"][code] = [nm, sp, un, kd, qty or 0.0, price, price, 0.0]
|
||||
else:
|
||||
p[4] = (p[4] or 0.0) + (qty or 0.0)
|
||||
p[5] = price
|
||||
p[6] = price
|
||||
elif tag == "Resource":
|
||||
st["_uw_res"] = st["unit"] is not None # 区分项目级/单位级 Resource 容器
|
||||
elif tag == "SummaryItem":
|
||||
# 单位汇总分析树:文档序(父先子后),kind/number 按层级——
|
||||
# ESCC 无 Number,用组/行区分(kind='grp' 有子级不行——iterparse 无法预知,
|
||||
# 统一 kind='item'+number 按 Top 级编码;前端按 Formula 层级不依赖 number 归层,
|
||||
# 与 parse_std 的 proj_summary 同结构入库(uw 层走 uw_summary)。
|
||||
rec = {"kind": "item", "number": (a.get("Code") or "").strip() or None,
|
||||
"code": (a.get("Code") or "").strip() or None,
|
||||
"name": a.get("Name"), "formula": a.get("Formula"),
|
||||
"rate": f(a.get("Rate")), "total": f(a.get("Total")),
|
||||
"remark": (a.get("Remark") or "").strip()}
|
||||
if st["unit"] is not None:
|
||||
out.setdefault("uw_summary", {}).setdefault(st["unit"]["idx"], []).append(rec)
|
||||
else: # end
|
||||
if tag == "ProjectItem":
|
||||
u = st["unit"]
|
||||
if u is not None:
|
||||
pc = pi_cost = None
|
||||
pc = costs_from(el.find("ProjectCosts")) if el.find("ProjectCosts") is not None else {}
|
||||
if pc:
|
||||
out["uw_cost"][u["idx"]] = pc
|
||||
st["unit"] = None
|
||||
st["in_bill"] = st["in_measure"] = False
|
||||
st["div_stack"] = []
|
||||
st["mdiv_stack"] = []
|
||||
st["cur_bill"] = st["cur_norm"] = -1
|
||||
elif tag == "ProjectGroup":
|
||||
if st["sp"] is not None and st["sp"]["uw_nums"]:
|
||||
key = (st["sp"]["name"] or "", st["sp"]["number"] or "",
|
||||
tuple(st["sp"]["uw_nums"]))
|
||||
if key not in st["sec_seen"]: # 重名单项组去重(样本 Group Name 全是 "1")
|
||||
st["sec_seen"].add(key)
|
||||
out["sections"].append(st["sp"])
|
||||
st["sp"] = None
|
||||
elif tag == "BillTable":
|
||||
st["in_bill"] = False
|
||||
elif tag == "MeasureTable":
|
||||
st["in_measure"] = False
|
||||
elif tag in ("Branch",):
|
||||
if st["in_measure"] and st["mdiv_stack"]:
|
||||
st["mdiv_stack"].pop()
|
||||
elif st["in_bill"] and st["div_stack"]:
|
||||
st["div_stack"].pop()
|
||||
elif tag == "Bill":
|
||||
st["cur_bill"] = -1
|
||||
st["cur_norm"] = -1
|
||||
elif tag == "Norm":
|
||||
st["cur_norm"] = -1
|
||||
el.clear()
|
||||
|
||||
# 项目级汇总分析(根 Summary:项目层 ZZJ/QDF/…树)→ proj_summary(决策 244 通道)
|
||||
# 根下 Summary 在 ProjectGroup 之外(样本:根直接有一个 Summary;单位级在 ProjectItem 内同名)。
|
||||
# iterparse 已经按单位收走单位级;这里补收项目级:重扫一次太贵——改为上面 start 时
|
||||
# st["unit"] is None 且父链在根 → 其实 iterparse 拿不到父链,改用文件级二遍小解析:
|
||||
# 只取 Projects 直接子 Summary(rfind 用 ElementTree 一次即可,量小)。
|
||||
try:
|
||||
_root = ET.parse(path).getroot()
|
||||
_sum = _root.find("Summary")
|
||||
if _sum is not None:
|
||||
for si in _sum.iter("SummaryItem"):
|
||||
out.setdefault("proj_summary", []).append({
|
||||
"kind": "item", "number": (si.get("Code") or "").strip() or None,
|
||||
"code": (si.get("Code") or "").strip() or None,
|
||||
"name": si.get("Name"), "formula": si.get("Formula"),
|
||||
"rate": f(si.get("Rate")), "total": f(si.get("Total")),
|
||||
"ratios": f(si.get("Rate")), "remark": (si.get("Remark") or "").strip()})
|
||||
# 项目级 AttrInfos → feat(项目特征条目,最多 60,名称去重)
|
||||
for ai in _root.iter("AttrInfoItem"):
|
||||
nm = (ai.get("Name") or ai.get("Code") or "").strip()
|
||||
if not nm:
|
||||
continue
|
||||
_f = out.setdefault("feat", [])
|
||||
if all(x[0] != nm for x in _f) and len(_f) < 60:
|
||||
_f.append((nm, ai.get("Value") or ""))
|
||||
# 项目级 InfoList 的关键条目并入 info(弹框兜底信息)
|
||||
_info = out.setdefault("info", {})
|
||||
for ii in _root.iter("InfoItem"):
|
||||
_c = (ii.get("Code") or "").strip()
|
||||
_v = (ii.get("Value") or "").strip()
|
||||
if _c and _v and _c not in _info:
|
||||
_info[_c] = _v
|
||||
except ET.ParseError:
|
||||
pass
|
||||
|
||||
# 总造价兜底:Projects@Total 缺失时用各单位工程 Total 求和(原文件口径)
|
||||
if out.get("total") is None:
|
||||
vals = [t for _, _, t in out["uw"] if t is not None]
|
||||
if vals:
|
||||
out["total"] = sum(vals)
|
||||
return "ESCC2"
|
||||
|
||||
|
||||
def _zip_swt_member(z):
|
||||
"""在 zip 里找根节点 ConstructProject 的 XML。
|
||||
|
||||
@@ -975,9 +1229,11 @@ def parse_file(path, out):
|
||||
return parse_std(path, out)
|
||||
if st == "swt":
|
||||
return parse_swt(path, out)
|
||||
if st == "esc":
|
||||
return parse_esc(path, out)
|
||||
if st == "xml":
|
||||
raise ValueError("这个 XML 不是已知的计价结构(当前支持:COS/COS4 压缩包、SWT/SWZ 数据交换标准;"
|
||||
"同族新后缀可直接打开,扩展名不拦)")
|
||||
raise ValueError("这个 XML 不是已知的计价结构(当前支持:COS/COS4 压缩包、SWT/SWZ、"
|
||||
"ESCC2.0 广东省标准;同族新后缀可直接打开,扩展名不拦)")
|
||||
if st == "encrypted":
|
||||
raise ValueError("这是加密件,无法解析")
|
||||
raise ValueError("文件头无法识别,不是计价文件")
|
||||
|
||||
Reference in New Issue
Block a user