From 9ef07f86afb1159889ebaae5994c022267c3b2d0 Mon Sep 17 00:00:00 2001 From: umsangdon Date: Tue, 17 Nov 2026 16:01:54 +0900 Subject: [PATCH] =?UTF-8?q?fix(854-1):=20=EB=8B=A8=EA=B0=80=EC=82=B0?= =?UTF-8?q?=EC=B6=9C=20=C2=B7=20=EC=9D=BC=EC=9C=84=EB=8C=80=EA=B0=80=20?= =?UTF-8?q?=C2=B7=20=EB=82=B4=20=EC=9E=AC=EB=A3=8C=20=EB=B2=88=ED=98=B8=20?= =?UTF-8?q?=E2=80=94=20=EC=A7=80=EC=9A=B4=20=EB=81=9D=20=EB=B2=88=ED=98=B8?= =?UTF-8?q?=20=EB=8B=A4=EC=8B=9C=20=EC=95=88=20=EC=94=80=20=E2=80=94=20?= =?UTF-8?q?=EB=AC=B6=EC=9D=8C=20=E2=91=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - SK · SC(`M02_Labor_Ref.next_key`) = 층 뿌리 장부 `_번호.json` {종류: 발급한 가장 큰 번호} · 다음 = 파일 · 장부 가운데 큰 번호 + 1 · 만들기 성공 뒤 장부에 적음(store 잠금 안 · 더 클 때만) - 내 재료 PM(`M02_Material_Own._last`) = 층 파일 `개인자재.json` 의 `마지막` 칸도 셈 · 새로 넣을 때 적음 - 내 기계 EQP 는 이미 `_다음` 카운터라 그대로 · 있는 번호 · 파일은 안 건드림(층마다 같은 SK 번호 = 층 따로라 정상) - 시험 test_854_1 ⑤ — 끝 번호 지운 뒤 새로 만들기 = 새 번호(옛 코드 = 지운 번호 다시 씀) Co-Authored-By: Claude Opus 5.5 --- M02_MasterTemplete/M02_Labor_Ref.py | 32 +++++++++++++++++-- M02_MasterTemplete/M02_Material_Own.py | 20 ++++++------ .../tester/test_854_1_my_machine_lock.py | 21 ++++++++++++ 3 files changed, 61 insertions(+), 12 deletions(-) diff --git a/M02_MasterTemplete/M02_Labor_Ref.py b/M02_MasterTemplete/M02_Labor_Ref.py index 1afa5198c..2306ad99f 100644 --- a/M02_MasterTemplete/M02_Labor_Ref.py +++ b/M02_MasterTemplete/M02_Labor_Ref.py @@ -352,11 +352,34 @@ def _no_loop(ref: Ref, trail: list[Ref]) -> None: _no_loop(child, [*trail, child]) +#: 층 뿌리 번호 장부 {종류: 발급한 가장 큰 번호} — 지운 끝 번호를 다시 안 씀(854-1 · M01 키대장 규칙과 같은 뜻) +ISSUED = "_번호.json" + + +def _issued() -> dict[str, Any]: + try: + got = json.loads((store.root() / ISSUED).read_bytes()) + except (OSError, ValueError): + return {} + return got if isinstance(got, dict) else {} + + def next_key(kind: str) -> str: - """지금 층 그 종류의 다음 키(가장 큰 번호 + 1).""" + """지금 층 그 종류의 다음 키(파일 · 장부 가운데 가장 큰 번호 + 1).""" folder = store.root() / kind have = [int(m.group(2)) for p in folder.glob("*.json") if (m := _KEY.match(p.stem))] - return f"{PREFIX[kind]}-{max(have, default=0) + 1:06d}" + return f"{PREFIX[kind]}-{max(*have, int(_issued().get(kind) or 0), 0) + 1:06d}" + + +def _keep_issued(kind: str, key: str) -> None: + """장부에 발급한 번호(더 클 때만).""" + from common_util.common_util_json import atomic_write_json + + n = int(_KEY.match(key).group(2)) + with store._LOCK: + got = _issued() + if int(got.get(kind) or 0) < n: + atomic_write_json(store.root() / ISSUED, {**got, kind: n}) def create(kind: str, doc: dict[str, Any]) -> dict[str, Any]: @@ -364,10 +387,13 @@ def create(kind: str, doc: dict[str, Any]) -> dict[str, Any]: for _ in range(5): key = next_key(kind) try: - return store.write(kind, key, "", {**doc, "키": key}) + got = store.write(kind, key, "", {**doc, "키": key}) except store.StoreError as e: if e.status != 409 or not isinstance(e.detail, dict): raise + else: + _keep_issued(kind, key) + return got raise _bad("새 키를 못 잡음 — 다시 누름", 409) diff --git a/M02_MasterTemplete/M02_Material_Own.py b/M02_MasterTemplete/M02_Material_Own.py index b5885a4cb..789962c58 100644 --- a/M02_MasterTemplete/M02_Material_Own.py +++ b/M02_MasterTemplete/M02_Material_Own.py @@ -49,14 +49,14 @@ def rows(folder: str | Path) -> list[dict[str, Any]]: def _last() -> int: - """모든 개인 층 파일의 가장 큰 PM 번호 — 사람 사이 번호 안 겹침.""" - found = [ - int(m.group(1)) - for path in layers.storage_root().glob(f"*/*/{layers.TEMPLATES_DIRNAME}/{FILE}") - for row in read(path.parent)["줄"] - if (m := KEY.match(str(row.get("키") or ""))) - ] - return max(found, default=0) + """모든 개인 층 파일의 가장 큰 PM 번호 — 사람 사이 번호 안 겹침 · 파일마다 `마지막`(발급한 가장 큰 번호)도 + 셈(지운 끝 번호를 다시 안 씀 · 854-1).""" + found = [0] + for path in layers.storage_root().glob(f"*/*/{layers.TEMPLATES_DIRNAME}/{FILE}"): + doc = read(path.parent) + found.append(int(doc.get("마지막") or 0)) + found += [int(m.group(1)) for r in doc["줄"] if (m := KEY.match(str(r.get("키") or "")))] + return max(found) def _text(v: Any) -> str: @@ -114,8 +114,10 @@ def add(folder: str | Path, body: dict[str, Any]) -> dict[str, Any]: with store._LOCK: doc = read(folder) row = _checked(body, doc["줄"]) - got = {"키": f"{PREFIX}{_last() + 1:06d}", **row} + n = _last() + 1 + got = {"키": f"{PREFIX}{n:06d}", **row} doc["줄"].append(got) + doc["마지막"] = n atomic_write_json(Path(folder) / FILE, doc) return got diff --git a/resources/tester/test_854_1_my_machine_lock.py b/resources/tester/test_854_1_my_machine_lock.py index ede2f0ada..0e3c84833 100644 --- a/resources/tester/test_854_1_my_machine_lock.py +++ b/resources/tester/test_854_1_my_machine_lock.py @@ -96,3 +96,24 @@ def test_brief_cache_follows_index(monkeypatch): assert all(v[0] is other for v in mh._BRIEF.values()) monkeypatch.setattr(m01, "loaded", lambda: base) assert mh.brief([key])[0] == first + + +def test_deleted_last_key_not_reused(world): + """⑤ 단가산출(SK) · 내 재료(PM) — 끝 번호를 지운 뒤 새로 만들면 새 번호(지운 번호 안 다시 씀).""" + from M02_MasterTemplete import M02_Labor_Ref as refs + from M02_MasterTemplete import M02_Material_Own as own + + me = world["personal"] + body = {"단위": "시간", "더한줄": []} + made = [ + _at(me, refs.create, "sangeun", {**body, "이름": f"sub4임시{i}"})["이름"] for i in range(2) + ] + assert made == ["SK-000001", "SK-000002"] + _at(me, store.delete, "sangeun", made[-1]) + assert _at(me, refs.create, "sangeun", {**body, "이름": "sub4임시3"})["이름"] == "SK-000003" + + bolt = {"이름": "sub4임시볼트", "규격": "M16", "단위": "EA", "값": 1200} + keys = [own.add(me, {**bolt, "규격": f"M{i}"})["키"] for i in (16, 20)] + assert keys == ["PM000001", "PM000002"] + own.remove(me, keys[-1]) + assert own.add(me, {**bolt, "규격": "M24"})["키"] == "PM000003"