by adom
Publish molecule sources to the Adom wiki (STEP, board, schematic, <slug>-fab.zip, export.json); the wiki's 3D model registry builds the GLB, footprint and symbol. Checks the bundle the way the registry does, publishes private, waits for the build, sets the hero from the built model, then makes the page public. Board mode still converts plain boards. Also writes APM render assets and component dependencies. Companion to adom-wiki model import.
master
AdityaAngajala
Preserve source for molecule-publish-3.0.0
33f54d8
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
"""component_graph.py reads an Altium .PcbDoc through pcbdoc.py beside it and takes machine
pins and contacts from the pin-name census of pin_names.py beside it; run with unittest.
fixtures/ams1117-3v3_Molecule.PcbDoc is a byte copy of git-wiki's
test/registry/fixtures/altium/ams1117-3v3_Molecule.PcbDoc (sha256 fbb53fd7...0a06546),
the Altium molecule page adom/ams1117-3v3-molecule.
"""
import importlib.util
import json
import subprocess
import sys
import tempfile
import unittest
from pathlib import Path
SCRIPTS = Path(__file__).resolve().parents[1] / "scripts"
FIXTURE = Path(__file__).resolve().parent / "fixtures" / "ams1117-3v3_Molecule.PcbDoc"
spec = importlib.util.spec_from_file_location("component_graph", SCRIPTS / "component_graph.py")
cg = importlib.util.module_from_spec(spec)
spec.loader.exec_module(cg)
class PcbDocBomTests(unittest.TestCase):
def test_parse_bom_reads_a_pcbdoc_as_its_kicad_board(self):
rows = {r["ref"]: (r["footprint"], r["value"]) for r in cg.parse_bom(str(FIXTURE))}
self.assertEqual(rows, {
"U1": ("SOT229P700X180-4N", "AMS1117-3.3"),
"C1": ("C0603", "C_10uF_0603"),
"C2": ("C0805", "C_22uF_0805"),
"J1": ("MachinePinMediumShort", "MachinePinMediumShort"),
"J2": ("MachinePinMediumShort", "MachinePinMediumShort"),
"J3": ("MachinePinMediumShort", "MachinePinMediumShort"),
"J4": ("MachinePinMediumShort", "MachinePinMediumShort"),
})
def test_plan_on_a_pcbdoc(self):
with tempfile.TemporaryDirectory() as d:
out = Path(d) / "plan.json"
run = subprocess.run([sys.executable, "-B", str(SCRIPTS / "component_graph.py"), "plan", str(FIXTURE),
"--offline", "--out", str(out)], capture_output=True, text=True, timeout=60)
plan = json.loads(out.read_text(encoding="utf-8"))
self.assertEqual(run.returncode, 1, run.stderr) # C1 and C2 carry value strings, not MPNs (#142)
self.assertEqual(plan["board"], FIXTURE.name)
parts = {p["ref"]: p for p in plan["parts"]}
self.assertEqual((parts["U1"]["kind"], parts["U1"]["mpn"], parts["U1"]["status"]), ("electrical", "AMS1117-3.3", "create"))
self.assertEqual({parts[r]["status"] for r in ("C1", "C2")}, {"flagged"})
self.assertEqual(plan["counts"]["electrical"], 3)
def test_machine_pins_come_from_the_census_even_when_it_refuses_the_board(self):
# J1 to J4 sit on machine-pin holes with a machine-pin footprint but are not named
# MP1 to MP4: the census warns (not-machine-pins) and refuses the board (pin-count),
# and they are still machine furniture, not electrical parts.
furniture, refused = cg.census_furniture(str(FIXTURE))
self.assertEqual(furniture, {"J1", "J2", "J3", "J4"})
self.assertEqual(refused, ["pin-count"])
rows = {r["ref"]: r for r in cg.parse_bom(str(FIXTURE))}
self.assertEqual({ref: cg.classify(r, furniture) for ref, r in rows.items()},
{"U1": "electrical", "C1": "electrical", "C2": "electrical",
"J1": "furniture", "J2": "furniture", "J3": "furniture", "J4": "furniture"})
self.assertEqual(cg.classify(rows["J1"]), "electrical", "the designator rule alone misses them")
with tempfile.TemporaryDirectory() as d:
out = Path(d) / "plan.json"
run = subprocess.run([sys.executable, "-B", str(SCRIPTS / "component_graph.py"), "plan", str(FIXTURE),
"--offline", "--out", str(out)], capture_output=True, text=True, timeout=60)
plan = json.loads(out.read_text(encoding="utf-8"))
self.assertEqual(plan["furnitureRule"], {"source": "census", "furniture": ["J1", "J2", "J3", "J4"], "refused": ["pin-count"]})
self.assertEqual(plan["counts"]["furniture"], 4)
self.assertIn("the census refuses this board (pin-count)", run.stdout)
def test_designator_rule_when_the_census_cannot_read_the_board(self):
with tempfile.TemporaryDirectory() as d:
fp = Path(d) / "pin.kicad_mod"
fp.write_text('(footprint "MachinePinMediumShort" (property "Reference" "MP1") (property "Value" "x"))\n'
'(footprint "SOIC-8" (property "Reference" "U1") (property "Value" "LM358"))\n', encoding="utf-8")
furniture, reason = cg.census_furniture(str(fp))
self.assertIsNone(furniture)
self.assertIn("no board file", reason)
out = Path(d) / "plan.json"
run = subprocess.run([sys.executable, "-B", str(SCRIPTS / "component_graph.py"), "plan", str(fp),
"--offline", "--out", str(out)], capture_output=True, text=True, timeout=60)
plan = json.loads(out.read_text(encoding="utf-8"))
self.assertEqual(run.returncode, 0, run.stderr)
self.assertEqual(plan["furnitureRule"]["source"], "designators")
self.assertIn("by designator only", run.stdout)
self.assertEqual({p["ref"]: p["kind"] for p in plan["parts"]}, {"MP1": "furniture", "U1": "electrical"})
def test_an_unreadable_pcbdoc_is_a_usage_error(self):
with tempfile.TemporaryDirectory() as d:
board = Path(d) / "x.PcbDoc"
board.write_bytes(b"not a compound file")
run = subprocess.run([sys.executable, "-B", str(SCRIPTS / "component_graph.py"), "plan", str(board),
"--offline", "--out", str(Path(d) / "plan.json")], capture_output=True, text=True, timeout=60)
self.assertEqual(run.returncode, 2)
self.assertIn("could not be read", run.stderr)
if __name__ == "__main__":
unittest.main()