- 엑셀 판 기계 정체: view ↔ importView 가 기계 줄 숫자 연결에 입력 · 잠금(값 · 조종원 · 잡품) 남김 ·
작업량 엔진 기계 찾기(_machine_of ↔ machineOf)가 숫자 + 입력 기계도 봄 → 엑셀 판 작업량 줄 0 → 1,634 블록
- 가져온 값 층(새 M02_Doc_Import_Carry): 경쟁사 Q 식(굴착기 8-2-3 · 덤프 8-2-8 단일 구간) 변수 →
수식.위덮음 {단가 기호: {작업량: {식, 변수}}}(방식 없음) · 우리 식 Q = 경쟁사 Q 인 줄만(759 줄 · 571 블록) ·
M02_Doc_Import.run(upper) · M02_Doc_Sk copy_of · run 이 실음
- 비목곱: 금액 줄 선택 칸 {비목: 식} — 비목 수량 = 수량 × 곱(계산기 짝 master_doc_calc ↔ calc.ts) ·
M02_Doc_Import_Shape 세 줄 합침이 「두 줄 같은 수량 + 더한 인수」 꼴도 한 줄로(301 줄)
- 단위 정규화 「톤/hr」 = 「ton/hr」(norm_unit ↔ normUnit)
- 굳히기: 수식 고침 저장한 가져온 식 SK 사본 = 가져온 식 셈 유지(M02_Item_Sangeun · 전엔 사본 0 원) → B09 = M02
- 시험 test_848_7_import_carry 14(거울 · 견본 · 임시 storage B09)
- 전수 3,241 블록 엑셀 · 우리 판 계 · 비목 · 검산 전후 다름 0
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
273 lines
12 KiB
Python
273 lines
12 KiB
Python
"""M02 가져온 식 수식 문서 다듬기(PLAN 809-1 · 요구 704) — `M02_Doc_Import.derive` 끝에 부름 · 금액 그대로.
|
||
|
||
① 소계 · 합계 성격 줄(덧줄 · 단위 「식」 · 수량 없음 · 단가 = 다른 줄 기호 합) 이름 = 엑셀 원 글 머리
|
||
(`가져온.원본.글줄` 자리 = 그 줄 이름 · 「:」 앞 글 · 글이 수식뿐이면 바로 위 설명 글 ·
|
||
「합 계」 절 아래 비목 = 「노무비 합계」 꼴) — 「줄_2 · 노무비_2 · 계_2」 같은 지은 이름 걷음
|
||
② EST 기계 세 줄(같은 자원 · 같은 기계 이음 · 같은 수량 · 노무비 · 재료비 · 경비 하나씩) = 한 기계 줄
|
||
(이름 = 자원 이름 · 비목 묶음 · 단가 = 기계 이음 칸 없음 · 엑셀 = 값묶음) · 세 줄 기호를 부르던 식 =
|
||
그 줄 비목 칸(`C_크레인.노무비`) — 셈 값 그대로(줄 끝수 = 비목마다 같은 규칙) ·
|
||
848-7 두 줄 수량 A 가 같고 남은 줄 수량 = A 의 곱 · 나눔 인수 + 더한 인수(재료비 운전 시간율 · OH ÷ Q · × 0) =
|
||
한 줄 수량 A + `비목곱` {비목: 더한 인수}
|
||
엑셀 원 글 · 엔진 줄은 안 고침(문서 겉꼴만) · 못 맞추면 그대로 둠.
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import re
|
||
from decimal import Decimal
|
||
from typing import Any
|
||
|
||
from M02_MasterTemplete.M02_Doc_Copy import md, mw
|
||
|
||
COSTS = ("노무비", "재료비", "경비")
|
||
# 지은 이름 — 「줄_2 · 노무비_2」 · 읽기기 율 줄 「율」
|
||
_MADE = re.compile(r"^(?:(?:줄|노무비|재료비|경비|계|소계|합계)?_\d+|율(?:_\d+)?)$")
|
||
_SECTION = re.compile(r"^\s*\d+\s*\.\s*(.+?)\s*$")
|
||
|
||
|
||
def _flat(text: Any) -> str:
|
||
return re.sub(r"\s+", "", str(text or ""))
|
||
|
||
|
||
def _head(text: str) -> str:
|
||
"""「노무비 : 13,944.5 + …」 → 「노무비」 · 머리 없음 = 빈 글."""
|
||
head = text.split(":", 1)[0] if ":" in text else ""
|
||
return _flat(head)
|
||
|
||
|
||
def names(doc: dict, lines: list[dict]) -> None:
|
||
"""① 지은 이름(「줄_2 · 노무비_2 · 계_2」) → 엑셀 원 글 머리."""
|
||
section, note, by = "", "", {}
|
||
for one in lines:
|
||
text = str(one.get("글") or "")
|
||
if hit := _SECTION.match(text):
|
||
section, note = _flat(hit.group(1)), ""
|
||
continue
|
||
spot = one.get("자리")
|
||
if not spot:
|
||
note = text.strip()
|
||
continue
|
||
head = _head(text) or note
|
||
if head and section in ("합계", "계") and head in COSTS:
|
||
head = f"{head} 합계"
|
||
by[str(spot)] = head
|
||
note = ""
|
||
taken = {str(x.get("이름")) for x in doc.get("금액") or []}
|
||
for line in doc.get("금액") or []:
|
||
name = str(line.get("이름") or "")
|
||
new = by.get(name)
|
||
if _MADE.match(name) and new and new not in taken: # 단계 · 다른 줄 이름과 안 겹칠 때만
|
||
line["이름"] = new
|
||
taken.add(new)
|
||
_rename_steps(doc, {name: new})
|
||
|
||
|
||
def _rename_steps(doc: dict, swap: dict[str, str]) -> None:
|
||
"""단계 줄 목록(이름으로 묶음) — 바뀐 이름 따라감 · 합친 줄 = 한 번."""
|
||
for st in doc.get("단계") or []:
|
||
got = []
|
||
for n in st.get("줄") or []:
|
||
n = swap.get(n, n)
|
||
if n not in got:
|
||
got.append(n)
|
||
st["줄"] = got
|
||
|
||
|
||
def _bundle_of(doc: dict, at: int, who: dict[str, str] | None = None) -> list[int] | None:
|
||
"""at 에서 시작하는 기계 세 줄(노무비 · 재료비 · 경비 · 차례 아무렇게) — 같은 기계 이음 · 같은 수량 ·
|
||
시간당 경비 이음 없음(숫자)이면 같은 기계 자원(`who` 줄 이름 → 자원 · 814-1)."""
|
||
money = doc.get("금액") or []
|
||
three = money[at : at + 3]
|
||
if len(three) < 3 or sorted(str(x.get("비목")) for x in three) != sorted(COSTS):
|
||
return None
|
||
vars_ = {v.get("열쇠"): v for v in doc.get("변수") or []}
|
||
links = [(vars_.get(str(x.get("단가"))) or {}).get("연결") or {} for x in three]
|
||
if all(x.get("갈래") == "숫자" and not isinstance(x.get("값"), dict) for x in links):
|
||
mine = {(who or {}).get(str(x.get("이름"))) for x in three}
|
||
if len(mine) != 1 or None in mine:
|
||
return None
|
||
elif any(x.get("갈래") != "단가산출" or "기계" not in (x.get("입력") or {}) for x in links):
|
||
return None
|
||
elif len({(x.get("키"), str(x.get("입력"))) for x in links}) != 1 or [
|
||
x.get("칸") for x in links
|
||
] != [x.get("비목") for x in three]:
|
||
return None
|
||
for k in ("규격", "자리", "합밖", "번지"):
|
||
if len({str(x.get(k) or "") for x in three}) != 1:
|
||
return None
|
||
if _times(three) is None:
|
||
return None
|
||
names = {str(x.get("이름")) for x in three}
|
||
for st in doc.get("단계") or []: # 단계가 셋 중 일부만 담으면 못 합침
|
||
if 0 < len(names & set(st.get("줄") or [])) < 3:
|
||
return None
|
||
return [at, at + 1, at + 2]
|
||
|
||
|
||
def _factors(text: str) -> list | None:
|
||
try:
|
||
return [f for f in mw._factors(md.parse(text)) if f != (1, ("num", Decimal(1)))]
|
||
except md.DocError:
|
||
return None
|
||
|
||
|
||
def _times(three: list[dict]) -> tuple[str, dict[str, str]] | None:
|
||
"""848-7 세 줄 수량 → (한 줄 수량 A, {비목: 곱 글}) — 모두 같음 = 곱 없음 · 두 줄이 A 이고 남은 줄 =
|
||
A 의 곱 · 나눔 인수를 모두 담고 인수를 더함 = 그 더한 인수 · 그 밖 = None(못 합침)."""
|
||
texts = [str(x.get("수량") or "") for x in three]
|
||
base = max(texts, key=texts.count)
|
||
low = _factors(base)
|
||
if texts.count(base) < 2 or low is None:
|
||
return None
|
||
out: dict[str, str] = {}
|
||
for x, text in zip(three, texts):
|
||
if text == base:
|
||
continue
|
||
rest = _factors(text)
|
||
for f in low:
|
||
if rest is None or f not in rest:
|
||
return None
|
||
rest.remove(f)
|
||
if not rest:
|
||
return None
|
||
out[str(x["비목"])] = md.text_of(mw._build(rest))
|
||
return base, out
|
||
|
||
|
||
def _place(doc: dict, name: str, used: set[str]) -> str:
|
||
"""기계 줄 번지 — 자원 이름 첫 낱말(괄호 · 규격 · 기호 뺌) · 겹치면 숫자."""
|
||
word = re.sub(r"[^\w]|_", "", re.split(r"[\s(]", name.strip())[0]) or "기계"
|
||
if word[0].isdigit():
|
||
word = f"기계{word}"
|
||
have = used | {str(x.get("번지")) for x in doc.get("금액") or [] if x.get("번지")}
|
||
got, n = word, 1
|
||
while got in have:
|
||
n += 1
|
||
got = f"{word}{n}"
|
||
used.add(got)
|
||
return got
|
||
|
||
|
||
def _rewrite(text: Any, swap: dict[str, str]) -> Any:
|
||
if not isinstance(text, str) or not swap:
|
||
return text
|
||
keys = "|".join(re.escape(k) for k in sorted(swap, key=len, reverse=True))
|
||
pat = re.compile(rf"(?<![^\W_])(?:{keys})(?![^\W_]|[′'.])")
|
||
return pat.sub(lambda m: swap[m.group(0)], text)
|
||
|
||
|
||
AFTER = "_뒤" # 옮긴 조합 다음 줄 — 차림이 원본 행 차례로 다시 섞지 않게(다듬기 사이 임시)
|
||
|
||
|
||
def regroup(doc: dict, who: dict[str, str], parts: set[str]) -> None:
|
||
"""814-1 — 조합 기계 비목 세 줄 사이에 낀 다음 줄(값 0)을 세 줄 뒤로 · 같은 기계끼리 모음
|
||
(첫 줄 · 다음 줄이 엇갈려 항목이 겹치지 않게 · 값 0 줄이라 셈 그대로). `parts` = 조합 다음 줄 이름."""
|
||
money = doc.get("금액") or []
|
||
at = 0
|
||
while at < len(money):
|
||
head = who.get(str(money[at].get("이름")))
|
||
if head is None or str(money[at].get("이름")) in parts:
|
||
at += 1
|
||
continue
|
||
end, mains, rest = at, [], []
|
||
while end < len(money):
|
||
name = str(money[end].get("이름"))
|
||
if name in parts:
|
||
rest.append(money[end])
|
||
elif who.get(name) == head and len(mains) < 3:
|
||
mains.append(money[end])
|
||
else:
|
||
break
|
||
end += 1
|
||
if len(mains) > 1 and rest:
|
||
seen = list(dict.fromkeys(who.get(str(x.get("이름"))) for x in rest))
|
||
rest.sort(key=lambda x: seen.index(who.get(str(x.get("이름"))))) # 같은 기계끼리
|
||
for x in rest:
|
||
x[AFTER] = True # 차림(`NativeLayout.apply`) 원본 행 = 앞 줄 행 뒤
|
||
money[at:end] = mains + rest
|
||
at = max(end, at + 1)
|
||
|
||
|
||
def machines(doc: dict, title: dict[str, str], who: dict[str, str] | None = None) -> None:
|
||
"""② 기계 세 줄 → 한 기계 줄(비목 묶음) · 부르던 식 = 비목 칸. `title` = {줄 이름: 자원 이름} ·
|
||
`who` = {줄 이름: 기계 자원 열쇠}(시간당 경비 이음 없는 세 줄 · 814-1)."""
|
||
money = doc.get("금액") or []
|
||
vars_ = {v.get("열쇠"): v for v in doc.get("변수") or []}
|
||
out, swap, at, used = [], {}, 0, set()
|
||
while at < len(money):
|
||
idx = _bundle_of(doc, at, who)
|
||
if idx is None:
|
||
out.append(money[at])
|
||
at += 1
|
||
continue
|
||
three = sorted((money[i] for i in idx), key=lambda x: COSTS.index(x["비목"]))
|
||
first = vars_[str(money[idx[0]]["단가"])]
|
||
if first["연결"].get("갈래") == "숫자": # 시간당 경비 이음 없음 = 엑셀 값 묶음
|
||
link = {
|
||
"갈래": "숫자",
|
||
"값": {c: (vars_[str(x["단가"])]["연결"]).get("값") for c, x in zip(COSTS, three)},
|
||
}
|
||
else:
|
||
link = {k: v for k, v in first["연결"].items() if k not in ("칸", "엑셀")}
|
||
link["엑셀"] = {
|
||
c: Decimal(str((vars_[str(x["단가"])]["연결"]).get("엑셀") or 0))
|
||
for c, x in zip(COSTS, three)
|
||
}
|
||
lead = money[idx[0]]
|
||
name = title.get(str(lead.get("이름"))) or str(lead.get("이름"))
|
||
first["연결"], first["이름"] = link, f"{name} 단가"
|
||
if name in {str(x.get("이름")) for x in money} - {str(x.get("이름")) for x in three}:
|
||
name = f"{name} ({at + 1})" # 같은 이름 줄이 있으면 겹치지 않게
|
||
line = {k: v for k, v in lead.items() if k != "기호"} | {"이름": name, "비목": "묶음"}
|
||
line["단가"] = lead["단가"]
|
||
qty, times = _times(three)
|
||
if times: # 848-7 비목곱
|
||
line["수량"], line["비목곱"] = qty, times
|
||
_rename_steps(doc, {str(x.get("이름")): name for x in three})
|
||
if any(x.get("기호") for x in three): # 부르던 식 = 번지 칸(엔진 `번지.X.노무비`)
|
||
place = _place(doc, name, used)
|
||
line["번지"] = place
|
||
for c, x in zip(COSTS, three):
|
||
if x.get("기호"):
|
||
swap[str(x["기호"])] = f"번지.{place}.{c}"
|
||
doc["변수"] = [
|
||
v
|
||
for v in doc["변수"]
|
||
if v is first
|
||
or v.get("열쇠") not in {str(x["단가"]) for x in three} - {str(lead["단가"])}
|
||
]
|
||
out.append(line)
|
||
at += 3
|
||
doc["금액"] = out
|
||
for line in out:
|
||
for k in ("단가", "수량"):
|
||
line[k] = _rewrite(line.get(k), swap)
|
||
if line.get("비목곱"):
|
||
line["비목곱"] = {k: _rewrite(v, swap) for k, v in line["비목곱"].items()}
|
||
for one in doc.get("식") or []:
|
||
one["식"] = _rewrite(one.get("식"), swap)
|
||
|
||
|
||
def shape(doc: dict, meta: dict) -> dict:
|
||
"""derive 문서 다듬기 — 원본 글줄 · 엔진 줄 자원 이름."""
|
||
from M02_MasterTemplete import M02_Doc_NativeLayout as lay
|
||
|
||
src = meta.get("원본") or {}
|
||
lay.tag(doc, meta) # 812-3 원파일 차림 — 금액 줄 ↔ 로직 줄(끝에 `apply`)
|
||
names(doc, list(src.get("글줄") or []))
|
||
title, who, parts = {}, {}, set()
|
||
for item in (meta.get("로직") or {}).get("호표") or []:
|
||
res = item.get("자원") or {}
|
||
if res.get("갈래") == "기계" and res.get("이름"):
|
||
title[str(item.get("이름"))] = " ".join(x for x in (res["이름"], res.get("규격")) if x)
|
||
key = (res.get("코드"), res.get("이름"), res.get("규격"))
|
||
who[str(item.get("이름"))] = repr(key)
|
||
if res.get("조합"):
|
||
parts.add(str(item.get("이름")))
|
||
regroup(doc, who, parts)
|
||
machines(doc, title, who)
|
||
lay.apply(doc, meta)
|
||
for line in doc.get("금액") or []:
|
||
line.pop(AFTER, None)
|
||
return doc
|