|
| 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