Skip to content

Commit 3361088

Browse files
authored
Merge pull request mixxxdj#16118 from xARSENICx/add-ui-buddy-check
build: Add pre-commit hook to detect invalid QLabel buddy properties
2 parents 002e0e9 + e1a6ef5 commit 3361088

2 files changed

Lines changed: 59 additions & 0 deletions

File tree

.pre-commit-config.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,10 @@ repos:
189189
additional_dependencies:
190190
- lxml==5.3.0
191191
files: ^(res/translations/.*\.ts)$
192+
- id: check-ui-buddies
193+
name: check-ui-buddies
194+
description: "Detect invalid or self-referential QLabel buddies in Qt UI files"
195+
entry: python tools/check_ui_buddies.py
196+
language: python
197+
types: [text]
198+
files: ^.*\.ui$

tools/check_ui_buddies.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env python3
2+
import sys
3+
import xml.etree.ElementTree as ET
4+
5+
6+
def check_file(filepath):
7+
try:
8+
tree = ET.parse(filepath)
9+
except ET.ParseError:
10+
# Let check-xml pre-commit hook handle malformed XML
11+
return False
12+
13+
root = tree.getroot()
14+
has_error = False
15+
16+
# Collect all widget names in the UI file
17+
all_widgets = set()
18+
for widget in root.iter("widget"):
19+
if "name" in widget.attrib:
20+
all_widgets.add(widget.attrib["name"])
21+
22+
# Check QLabel buddies
23+
for widget in root.iter("widget"):
24+
if widget.attrib.get("class") == "QLabel":
25+
label_name = widget.attrib.get("name", "<unnamed>")
26+
27+
for prop in widget.findall("property"):
28+
if prop.attrib.get("name") == "buddy":
29+
cstring = prop.find("cstring")
30+
if cstring is not None and cstring.text:
31+
buddy_name = cstring.text
32+
33+
if buddy_name == label_name:
34+
msg = (
35+
f"{filepath}: Error: "
36+
f"QLabel '{label_name}' is its own buddy!"
37+
)
38+
print(msg, file=sys.stderr)
39+
has_error = True
40+
elif buddy_name not in all_widgets:
41+
msg = (
42+
f"{filepath}: Error: Buddy '{buddy_name}' "
43+
f"for QLabel '{label_name}' does not exist!"
44+
)
45+
print(msg, file=sys.stderr)
46+
has_error = True
47+
48+
return has_error
49+
50+
51+
if __name__ == "__main__":
52+
sys.exit(int(any(check_file(arg) for arg in sys.argv[1:])))

0 commit comments

Comments
 (0)