Skip to content

Commit c126e2e

Browse files
committed
Updated duplicate device names to use PVs to identify
1 parent cecc500 commit c126e2e

File tree

3 files changed

+72
-28
lines changed

3 files changed

+72
-28
lines changed

src/techui_builder/builder.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -395,24 +395,26 @@ def _fix_duplicate_names(self, node: JsonMap) -> None:
395395
if not node.children:
396396
return
397397

398-
# Count occurrences of each display_name
399-
name_counts: defaultdict[str | None, int] = defaultdict(int)
398+
# group by display_name
399+
name_groups: defaultdict[str | None, list] = defaultdict(list)
400400
for child in node.children:
401-
if child.display_name:
402-
name_counts[child.display_name] += 1
403-
404-
# Track which number we're on for each duplicate name
405-
name_indices: defaultdict[str | None, int] = defaultdict(int)
406-
407-
# Update display names for duplicates
401+
name_groups[child.display_name].append(child)
402+
403+
# fix duplicates by appending identifiers
404+
for name, children in name_groups.items():
405+
if name and len(children) > 1:
406+
# append pv names when present
407+
for child in children:
408+
if "P" in child.macros:
409+
child.display_name = f"{name} ({child.macros['P']})"
410+
411+
# append NO PV NAME and enumeration when there is no pv name
412+
no_pv_children = [c for c in children if "P" not in c.macros]
413+
for i, child in enumerate(no_pv_children, 1):
414+
child.display_name = f"{name} (NO PV NAME {i})"
415+
416+
# recursively fix children
408417
for child in node.children:
409-
if child.display_name and name_counts[child.display_name] > 1:
410-
name_indices[child.display_name] += 1
411-
child.display_name = (
412-
f"{child.display_name} {name_indices[child.display_name]}"
413-
)
414-
415-
# Recursively fix children
416418
self._fix_duplicate_names(child)
417419

418420
def write_json_map(

tests/conftest.py

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,48 @@ def example_json_map():
5959
@pytest.fixture
6060
def example_display_names_json():
6161
# Create test json map with correct display names
62-
test_map_det1 = JsonMap("test_child_bob.bob", "Detector 1", exists=False)
63-
test_map_det2 = JsonMap("test_child_bob.bob", "Detector 2", exists=False)
64-
test_map_dev1 = JsonMap("test_child_bob.bob", "Device 1", exists=False)
65-
test_map_dev2 = JsonMap("test_child_bob.bob", "Device 2", exists=False)
62+
test_map_det1 = JsonMap(
63+
"test_child_bob.bob",
64+
"Detector (PV-DET-01)",
65+
macros={"P": "PV-DET-01"},
66+
exists=False,
67+
)
68+
test_map_det2 = JsonMap(
69+
"test_child_bob.bob",
70+
"Detector (PV-DET-02)",
71+
macros={"P": "PV-DET-02"},
72+
exists=False,
73+
)
74+
test_map_det3 = JsonMap(
75+
"test_child_bob.bob",
76+
"Detector (PV-DET-03)",
77+
macros={"P": "PV-DET-03"},
78+
exists=False,
79+
)
80+
test_map_det4 = JsonMap(
81+
"test_child_bob.bob",
82+
"Detector (NO PV NAME 1)",
83+
macros={"R": "NON-P-MACRO"},
84+
exists=False,
85+
)
86+
test_map_dev1 = JsonMap(
87+
"test_child_bob.bob",
88+
"Device (PV-DEV-01)",
89+
macros={"P": "PV-DEV-01"},
90+
exists=False,
91+
)
92+
test_map_dev2 = JsonMap(
93+
"test_child_bob.bob",
94+
"Device (PV-DEV-02)",
95+
macros={"P": "PV-DEV-02"},
96+
exists=False,
97+
)
6698
test_map = JsonMap("test_bob.bob", "Beamline")
6799

68100
test_map_dev1.children.append(test_map_det1)
69101
test_map_dev1.children.append(test_map_det2)
70-
test_map_dev2.children.append(test_map_det1)
71-
test_map_dev2.children.append(test_map_det2)
102+
test_map_dev2.children.append(test_map_det3)
103+
test_map_dev2.children.append(test_map_det4)
72104
test_map.children.append(test_map_dev1)
73105
test_map.children.append(test_map_dev2)
74106

tests/test_builder.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -345,19 +345,29 @@ def test_fix_duplicate_names_recursive(builder, example_display_names_json):
345345
)
346346

347347
test_display_names_json_det1 = JsonMap(
348-
"test_child_bob.bob", "Detector", exists=False
348+
"test_child_bob.bob", "Detector", macros={"P": "PV-DET-01"}, exists=False
349349
)
350350
test_display_names_json_det2 = JsonMap(
351-
"test_child_bob.bob", "Detector", exists=False
351+
"test_child_bob.bob", "Detector", macros={"P": "PV-DET-02"}, exists=False
352+
)
353+
test_display_names_json_det3 = JsonMap(
354+
"test_child_bob.bob", "Detector", macros={"P": "PV-DET-03"}, exists=False
355+
)
356+
test_display_names_json_det4 = JsonMap(
357+
"test_child_bob.bob", "Detector", macros={"R": "NON-P-MACRO"}, exists=False
358+
)
359+
test_display_names_json_dev1 = JsonMap(
360+
"test_child_bob.bob", "Device", macros={"P": "PV-DEV-01"}, exists=False
361+
)
362+
test_display_names_json_dev2 = JsonMap(
363+
"test_child_bob.bob", "Device", macros={"P": "PV-DEV-02"}, exists=False
352364
)
353-
test_display_names_json_dev1 = JsonMap("test_child_bob.bob", "Device", exists=False)
354-
test_display_names_json_dev2 = JsonMap("test_child_bob.bob", "Device", exists=False)
355365
test_display_names_json = JsonMap("test_bob.bob", "Beamline")
356366

357367
test_display_names_json_dev1.children.append(test_display_names_json_det1)
358368
test_display_names_json_dev1.children.append(test_display_names_json_det2)
359-
test_display_names_json_dev2.children.append(test_display_names_json_det1)
360-
test_display_names_json_dev2.children.append(test_display_names_json_det2)
369+
test_display_names_json_dev2.children.append(test_display_names_json_det3)
370+
test_display_names_json_dev2.children.append(test_display_names_json_det4)
361371
test_display_names_json.children.append(test_display_names_json_dev1)
362372
test_display_names_json.children.append(test_display_names_json_dev2)
363373

0 commit comments

Comments
 (0)