# -*- coding: utf-8 -*- """造最小可解析的 .cos4 样例工程(与 app/tests/api_harness.py 的 make_cos4 同口径)。 只在本工具的临时目录里落盘,绝不碰工程目录与生产数据。 """ import zipfile from xml.sax.saxutils import quoteattr def make_cos4(path, proj_name, bills, lmes=(), build_unit="测试建设单位"): """构造最小 .cos4(zip 内 Project.xml + 单位工程 xml)。 bills: [(编码, 名称, 特征, 单位, 工程量, 单价)] —— 分部分项清单(自动配 1 条定额 + 定额消耗) lmes: [(编码, 名称, 规格, 单位, 不含税价, 含税价)] —— 工料机 另固定带:1 条措施清单(安全文明施工费)、项目级工料机汇总、总造价 1234567.89。 """ lme_proj = "".join( '' % (quoteattr(it[0]), quoteattr(it[1]), quoteattr(it[2]), quoteattr(it[3]), quoteattr(it[4]), quoteattr(it[5])) for it in lmes) proj = ( '' '' '' '' '' '%s' % (quoteattr(proj_name), quoteattr(build_unit), lme_proj)) def _bill_xml(i, b): num, name, attr, unit, qty, price = b qty, price = float(qty), float(price) lme0 = lmes[0][0] if lmes else "000001" return ( '' '' '' '' % (quoteattr(num), quoteattr(name), quoteattr(attr), quoteattr(unit), quoteattr(str(qty)), quoteattr(str(price)), quoteattr(str(qty * price)), i + 1, quoteattr(name + "安装"), quoteattr(unit), quoteattr(str(qty)), quoteattr(str(qty * 80)), quoteattr(lme0))) uw = ( '' '' '%s' '' '%s' % ("".join(_bill_xml(i, b) for i, b in enumerate(bills)), "".join( '' % (quoteattr(it[0]), quoteattr(it[1]), quoteattr(it[2]), quoteattr(it[3]), quoteattr(it[4]), quoteattr(it[5])) for it in lmes))) with zipfile.ZipFile(path, "w", zipfile.ZIP_DEFLATED) as z: z.writestr("Project.xml", proj.encode("utf-8")) z.writestr("UW1.xml", uw.encode("utf-8")) return path