refactor: API-функции для веб-интеграции
parse_gge_from_zip() — парсинг GGE без CLI generate_ssr_from_data() — генерация ССР без CLI main() теперь делегирует в generate_ssr_from_data() Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -185,6 +185,70 @@ def build_hierarchy(estimates: list[dict]) -> dict:
|
||||
return dict(hierarchy)
|
||||
|
||||
|
||||
def parse_gge_from_zip(zip_path: str, on_progress=None) -> dict:
|
||||
"""Parse GGE ZIP → project_data dict. Callable from Python (no CLI)."""
|
||||
estimates = []
|
||||
errors = []
|
||||
with zipfile.ZipFile(zip_path) as z:
|
||||
gge_infos = [i for i in z.infolist()
|
||||
if i.filename.lower().endswith(".gge") and i.file_size > 100]
|
||||
for i, info in enumerate(gge_infos):
|
||||
if not (info.flag_bits & 0x800):
|
||||
info.filename = fix_zip_encoding(info.filename)
|
||||
content = z.read(info)
|
||||
parsed = parse_gge(content)
|
||||
if parsed:
|
||||
parsed["source_file"] = Path(info.filename).name
|
||||
estimates.append(parsed)
|
||||
else:
|
||||
errors.append(info.filename)
|
||||
if on_progress and (i + 1) % 300 == 0:
|
||||
on_progress(i + 1, len(gge_infos))
|
||||
|
||||
hierarchy = build_hierarchy(estimates)
|
||||
|
||||
grand_totals = {"Строительные": 0, "Монтажные": 0, "Оборудование": 0, "Прочие": 0}
|
||||
for ch_data in hierarchy.values():
|
||||
for wtype, val in ch_data["totals_tys"].items():
|
||||
grand_totals[wtype] += val
|
||||
grand = sum(grand_totals.values())
|
||||
|
||||
result = {
|
||||
"generated": datetime.now().isoformat(),
|
||||
"source": zip_path,
|
||||
"total_estimates": len(estimates),
|
||||
"total_errors": len(errors),
|
||||
"grand_totals_tys": grand_totals,
|
||||
"grand_total_tys": grand,
|
||||
"chapters": {},
|
||||
}
|
||||
for ch_num, ch_data in hierarchy.items():
|
||||
ch_dict = {
|
||||
"name": ch_data["name"],
|
||||
"totals_tys": dict(ch_data["totals_tys"]),
|
||||
"sections": {},
|
||||
}
|
||||
for rz_num, rz_data in ch_data["razrely"].items():
|
||||
rz_dict = {"os_groups": {}}
|
||||
for os_num, os_data in rz_data["os"].items():
|
||||
rz_dict["os_groups"][str(os_num)] = {
|
||||
"totals_tys": dict(os_data["totals_tys"]),
|
||||
"estimates": [
|
||||
{
|
||||
"code": e["code"],
|
||||
"name": e["name"][:100],
|
||||
"totals_tys": e["totals_tys"],
|
||||
"grand_total_tys": e["grand_total_tys"],
|
||||
}
|
||||
for e in os_data["estimates"]
|
||||
],
|
||||
}
|
||||
ch_dict["sections"][str(rz_num)] = rz_dict
|
||||
result["chapters"][str(ch_num)] = ch_dict
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="Parse GGE → project structure")
|
||||
parser.add_argument("zip_file", help="ZIP с GGE-файлами")
|
||||
|
||||
Reference in New Issue
Block a user