Skip to content

Official Codebase of 3DCodeVerse

Python

conventions: PascalCase accepts all-caps acronyms (LED, TV, TVStand, CPU_2) — one rule, the wrapper copy pinned to it

gaoypengb1636646e214
diff --git a/harness/codeverse3d/conventions.py b/harness/codeverse3d/conventions.py
index d5df0713..7e56f921 100644
--- a/harness/codeverse3d/conventions.py
+++ b/harness/codeverse3d/conventions.py
@@ -177,13 +177,27 @@ def to_snake(name: str) -> str:
return s or "part"
-def to_pascal(name: str) -> str:
- """``seat_cushion`` / ``seat cushion`` → ``SeatCushion``."""
- return "".join(w[:1].upper() + w[1:] for w in to_snake(name).split("_") if w) or "Part"
+#: a part / assembly name as the contracts require it: ``SeatCushion``, instances ``SeatCushion_3``.
+#: All-caps acronyms are valid PascalCase (owner, 2026-09-22): ``LED``, ``TV``, ``A``,
+#: ``TVStand``, ``LEDStrip``, ``HDMIPort1``, ``CPU_2``. The stdlib-only wrapper
+#: ``languages/wrappers/run_cq.py`` keeps a copy (it cannot import this package);
+#: ``tests/core/test_names.py`` pins the two together.
+PASCAL_RE = re.compile(r"^[A-Z][A-Za-z0-9]*(?:_\d+)?$")
-#: a part / assembly name as the contracts require it: ``SeatCushion``, instances ``SeatCushion_3``
-PASCAL_RE = re.compile(r"^[A-Z][a-z0-9]+(?:[A-Z][a-z0-9]*)*(?:_\d+)?$")
+def to_pascal(name: str) -> str:
+ """``seat_cushion`` / ``seat cushion`` → ``SeatCushion``; a name that is already
+ PascalCase (no underscore) comes back unchanged, so ``TVStand`` stays ``TVStand``.
+
+ For a name without an instance suffix ``to_snake(to_pascal(n)) == to_snake(n)`` — the
+ snake name is the key every gate matches parts by (``CPU_2`` / ``Leg_0`` fold to
+ ``Cpu2`` / ``Leg0``: an instance name is not a plan name). The reverse loses acronyms: ``to_pascal("tv_stand")``
+ is ``TvStand``, so a name rebuilt from a snake key (a file stem, a GLB key, a zone
+ module) is ``TvStand`` — code that compares such a name must compare ``to_snake``.
+ """
+ if "_" not in name and PASCAL_RE.match(name):
+ return name
+ return "".join(w[:1].upper() + w[1:] for w in to_snake(name).split("_") if w) or "Part"
def slugify(text: str, max_len: int = 48) -> str:
diff --git a/harness/codeverse3d/languages/threejs/__init__.py b/harness/codeverse3d/languages/threejs/__init__.py
index 49e8dd85..ba779451 100644
--- a/harness/codeverse3d/languages/threejs/__init__.py
+++ b/harness/codeverse3d/languages/threejs/__init__.py
@@ -209,8 +209,10 @@ def lint_workspace(ws: Workspace) -> GateReport:
findings.append(GateFinding(gate=GATE, severity=Severity.ERROR, target=rel,
message="src/object.js must `export function build(THREE)`", fix_hint="add `export function build(THREE) { const root = new THREE.Group(); ...; return root; }`"))
elif path.parent == ws.src / "parts":
+ # matched by snake: the plan's `TVStand` lives in tv_stand.js and exports buildTVStand
expected = "build" + to_pascal(path.stem)
- if expected not in exports:
+ if expected not in exports and not any(
+ e.startswith("build") and len(e) > 5 and to_snake(e[5:]) == path.stem for e in exports):
named = sorted(e for e in exports if e != "build")
sev = Severity.WARN if any(e != "build" for e in exports) else Severity.ERROR
findings.append(GateFinding(gate=GATE, severity=sev, target=rel,
diff --git a/harness/tests/core/test_names.py b/harness/tests/core/test_names.py
new file mode 100644
index 00000000..b72afce2
--- /dev/null
+++ b/harness/tests/core/test_names.py
@@ -0,0 +1,105 @@
+"""The naming rule: one PascalCase rule (``conventions.PASCAL_RE``) that accepts all-caps
+acronyms (owner, 2026-09-22), the stdlib-only wrapper copies pinned to it, and the
+snake round trip every gate matches parts by."""
+
+from __future__ import annotations
+
+import ast
+import re
+from pathlib import Path
+
+import pytest
+
+from codeverse3d.contracts.common import Track
+from codeverse3d.contracts.plan import StaticPlan
+from codeverse3d.conventions import PASCAL_RE, to_pascal, to_snake
+from codeverse3d.languages.blender import lint_blender_source
+from codeverse3d.languages.cadquery import lint_cadquery_source
+from codeverse3d.languages.urdf import _IDENT as URDF_LINK_RE
+from codeverse3d.tracks.planner import normalise_names, plan_example
+
+WRAPPERS = Path(__file__).resolve().parents[2] / "codeverse3d" / "languages" / "wrappers"
+
+VALID = ["LED", "TV", "A", "TVStand", "LEDStrip", "USBPort", "HDMIPort1", "CPU_2", "SeatCushion", "Leg_0"]
+INVALID = ["led", "tvStand", "seat_cushion", "TV Stand", "Seat Cushion", "TV-Stand", "Seat-Cushion", "", "_Leg", "Leg_", "3D"]
+#: acronym names: snake key, and what to_pascal rebuilds from that key (the acronym is lost)
+SNAKE = {"LED": ("led", "Led"), "TV": ("tv", "Tv"), "A": ("a", "A"), "TVStand": ("tv_stand", "TvStand"),
+ "LEDStrip": ("led_strip", "LedStrip"), "USBPort": ("usb_port", "UsbPort"),
+ "HDMIPort1": ("hdmi_port1", "HdmiPort1"), "CPU_2": ("cpu_2", "Cpu2")}
+
+
+def _wrapper_assign(file: str, name: str) -> ast.AST:
+ tree = ast.parse((WRAPPERS / file).read_text())
+ return next(n for n in tree.body if isinstance(n, (ast.Assign, ast.FunctionDef))
+ and (getattr(n, "name", None) == name or any(getattr(t, "id", None) == name for t in getattr(n, "targets", ()))))
+
+
+def _wrapper_pascal_re() -> re.Pattern[str]:
+ node = _wrapper_assign("run_cq.py", "PASCAL_RE")
+ return re.compile(ast.literal_eval(node.value.args[0])) # type: ignore[attr-defined]
+
+
+def _wrapper_snake():
+ ns: dict = {"re": re}
+ exec(compile(ast.Module(body=[_wrapper_assign("run_bpy_links.py", "_snake")], type_ignores=[]), "run_bpy_links", "exec"), ns)
+ return ns["_snake"]
+
+
[email protected]("name", VALID)
+def test_acronym_names_are_valid_pascal(name):
+ assert PASCAL_RE.match(name)
+
+
[email protected]("name", INVALID)
+def test_lowercase_first_spaces_hyphens_and_empty_are_rejected(name):
+ assert not PASCAL_RE.match(name)
+
+
+def test_the_cadquery_wrapper_copy_is_the_conventions_rule():
+ """run_cq.py cannot import codeverse3d, so it keeps a copy — the same pattern, same verdicts."""
+ wrap = _wrapper_pascal_re()
+ assert wrap.pattern == PASCAL_RE.pattern
+ assert [bool(wrap.match(n)) for n in VALID + INVALID] == [bool(PASCAL_RE.match(n)) for n in VALID + INVALID]
+
+
+def test_the_links_wrapper_snake_copy_is_to_snake():
+ snake = _wrapper_snake()
+ for n in [*VALID, *INVALID[:-3], "LPCompressor", "Seat2Cushion"]:
+ if n.strip():
+ assert snake(n) == to_snake(n), n
+
+
[email protected]("name", list(SNAKE))
+def test_snake_round_trip_of_acronym_names(name):
+ snake, rebuilt = SNAKE[name]
+ assert to_snake(name) == snake
+ assert to_pascal(snake) == rebuilt # rebuilding from the key loses the acronym
+ if "_" not in name: # ... but for a plan name never the key itself
+ assert to_snake(to_pascal(name)) == snake == to_snake(rebuilt)
+
+
+def test_to_pascal_keeps_a_valid_name_and_still_normalises_the_rest():
+ assert [to_pascal(n) for n in ("TVStand", "LED", "A", "HDMIPort1", "SeatCushion")] == ["TVStand", "LED", "A", "HDMIPort1", "SeatCushion"]
+ # an instance suffix / underscores / spaces / hyphens / lowercase still go through to_snake
+ # (an instance name is not a plan name: to_pascal folds its suffix, CPU_2 -> Cpu2 -> key cpu2,
+ # exactly as Leg_0 -> Leg0 always has)
+ assert [to_pascal(n) for n in ("CPU_2", "Leg_0", "tv stand", "LED strip", "seat-cushion", "tvStand", "")] == \
+ ["Cpu2", "Leg0", "TvStand", "LedStrip", "SeatCushion", "TvStand", "Part"]
+
+
+def test_planner_normalisation_keeps_acronym_names():
+ d = plan_example(Track.STATIC_OBJECT)
+ d["parts"][0]["name"], d["parts"][1]["name"] = "TVStand", "led strip"
+ d["parts"][1]["attach_to"] = d["parts"][2]["attach_to"] = "TVStand"
+ plan = normalise_names(StaticPlan.model_validate(d))
+ assert [p.name for p in plan.parts[:2]] == ["TVStand", "LedStrip"] and plan.parts[1].attach_to == "TVStand"
+
+
+def test_the_part_name_lints_accept_acronyms():
+ src = "import cadquery as cq\nresult = cq.Assembly()\n" + "".join(
+ f"result.add(cq.Workplane().box(1, 1, 1), name='{n}', color=cq.Color(1, 0, 0))\n" for n in ("TVStand", "LED", "CPU_2"))
+ assert not any("not PascalCase" in f.message for f in lint_cadquery_source(src).findings)
+ rep = lint_blender_source("import bpy\nobj = bpy.context.object\nobj.name = 'USBPort'\n")
+ assert any("named objects" in f.message and "USBPort" in f.message for f in rep.findings)
+ assert all(URDF_LINK_RE.match(n) for n in ("TVStand", "LED", "CPU_2", "A"))
+
diff --git a/harness/tests/threejs_render/test_lint.py b/harness/tests/threejs_render/test_lint.py
index 1867d069..094b02f2 100644
--- a/harness/tests/threejs_render/test_lint.py
+++ b/harness/tests/threejs_render/test_lint.py
@@ -117,3 +117,11 @@ def test_one_node_checks_every_file_and_a_parsed_file_is_not_sent_again(tmp_path
assert len(calls) == 1 # one process for four files
assert js_lint.node_check_syntax(good) == {} and len(calls) == 1 # all three parsed already: no node at all
assert list(js_lint.node_check_syntax([bad])) == [bad] and len(calls) == 2 # a failure is never cached
+
+
+def test_an_acronym_part_export_is_matched_by_snake(stool_ws: Workspace, no_node_syntax):
+ """The plan's ``TVStand`` lives in tv_stand.js and exports buildTVStand (to_pascal keeps
+ it); the lint must not ask for the snake-rebuilt ``buildTvStand`` instead."""
+ (stool_ws.src / "parts" / "tv_stand.js").write_text(
+ "import * as THREE from 'three';\nexport function buildTVStand(THREE_) { return new THREE.Group(); }\n")
+ assert not any("tv_stand.js: expected" in m for m in _msgs(lint_workspace(stool_ws)))