Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions src/techui_builder/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,24 +395,26 @@ def _fix_duplicate_names(self, node: JsonMap) -> None:
if not node.children:
return

# Count occurrences of each display_name
name_counts: defaultdict[str | None, int] = defaultdict(int)
# group by display_name
name_groups: defaultdict[str | None, list] = defaultdict(list)
for child in node.children:
if child.display_name:
name_counts[child.display_name] += 1

# Track which number we're on for each duplicate name
name_indices: defaultdict[str | None, int] = defaultdict(int)

# Update display names for duplicates
name_groups[child.display_name].append(child)

# fix duplicates by appending identifiers
for name, children in name_groups.items():
if name and len(children) > 1:
# append pv names when present
for child in children:
if "P" in child.macros:
child.display_name = f"{name} ({child.macros['P']})"

# append NO PV NAME and enumeration when there is no pv name
no_pv_children = [c for c in children if "P" not in c.macros]
for i, child in enumerate(no_pv_children, 1):
child.display_name = f"{name} (NO PV NAME {i})"

# recursively fix children
for child in node.children:
if child.display_name and name_counts[child.display_name] > 1:
name_indices[child.display_name] += 1
child.display_name = (
f"{child.display_name} {name_indices[child.display_name]}"
)

# Recursively fix children
self._fix_duplicate_names(child)

def write_json_map(
Expand Down
44 changes: 38 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,48 @@ def example_json_map():
@pytest.fixture
def example_display_names_json():
# Create test json map with correct display names
test_map_det1 = JsonMap("test_child_bob.bob", "Detector 1", exists=False)
test_map_det2 = JsonMap("test_child_bob.bob", "Detector 2", exists=False)
test_map_dev1 = JsonMap("test_child_bob.bob", "Device 1", exists=False)
test_map_dev2 = JsonMap("test_child_bob.bob", "Device 2", exists=False)
test_map_det1 = JsonMap(
"test_child_bob.bob",
"Detector (PV-DET-01)",
macros={"P": "PV-DET-01"},
exists=False,
)
test_map_det2 = JsonMap(
"test_child_bob.bob",
"Detector (PV-DET-02)",
macros={"P": "PV-DET-02"},
exists=False,
)
test_map_det3 = JsonMap(
"test_child_bob.bob",
"Detector (PV-DET-03)",
macros={"P": "PV-DET-03"},
exists=False,
)
test_map_det4 = JsonMap(
"test_child_bob.bob",
"Detector (NO PV NAME 1)",
macros={"R": "NON-P-MACRO"},
exists=False,
)
test_map_dev1 = JsonMap(
"test_child_bob.bob",
"Device (PV-DEV-01)",
macros={"P": "PV-DEV-01"},
exists=False,
)
test_map_dev2 = JsonMap(
"test_child_bob.bob",
"Device (PV-DEV-02)",
macros={"P": "PV-DEV-02"},
exists=False,
)
test_map = JsonMap("test_bob.bob", "Beamline")

test_map_dev1.children.append(test_map_det1)
test_map_dev1.children.append(test_map_det2)
test_map_dev2.children.append(test_map_det1)
test_map_dev2.children.append(test_map_det2)
test_map_dev2.children.append(test_map_det3)
test_map_dev2.children.append(test_map_det4)
test_map.children.append(test_map_dev1)
test_map.children.append(test_map_dev2)

Expand Down
22 changes: 16 additions & 6 deletions tests/test_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,19 +345,29 @@ def test_fix_duplicate_names_recursive(builder, example_display_names_json):
)

test_display_names_json_det1 = JsonMap(
"test_child_bob.bob", "Detector", exists=False
"test_child_bob.bob", "Detector", macros={"P": "PV-DET-01"}, exists=False
)
test_display_names_json_det2 = JsonMap(
"test_child_bob.bob", "Detector", exists=False
"test_child_bob.bob", "Detector", macros={"P": "PV-DET-02"}, exists=False
)
test_display_names_json_det3 = JsonMap(
"test_child_bob.bob", "Detector", macros={"P": "PV-DET-03"}, exists=False
)
test_display_names_json_det4 = JsonMap(
"test_child_bob.bob", "Detector", macros={"R": "NON-P-MACRO"}, exists=False
)
test_display_names_json_dev1 = JsonMap(
"test_child_bob.bob", "Device", macros={"P": "PV-DEV-01"}, exists=False
)
test_display_names_json_dev2 = JsonMap(
"test_child_bob.bob", "Device", macros={"P": "PV-DEV-02"}, exists=False
)
test_display_names_json_dev1 = JsonMap("test_child_bob.bob", "Device", exists=False)
test_display_names_json_dev2 = JsonMap("test_child_bob.bob", "Device", exists=False)
test_display_names_json = JsonMap("test_bob.bob", "Beamline")

test_display_names_json_dev1.children.append(test_display_names_json_det1)
test_display_names_json_dev1.children.append(test_display_names_json_det2)
test_display_names_json_dev2.children.append(test_display_names_json_det1)
test_display_names_json_dev2.children.append(test_display_names_json_det2)
test_display_names_json_dev2.children.append(test_display_names_json_det3)
test_display_names_json_dev2.children.append(test_display_names_json_det4)
test_display_names_json.children.append(test_display_names_json_dev1)
test_display_names_json.children.append(test_display_names_json_dev2)

Expand Down