|
| 1 | +"""Make an accessibility-tree dump readable and addressable. |
| 2 | +
|
| 3 | +``dump_accessibility_tree`` emits nodes with the platform's *raw* role — |
| 4 | +on Windows that is the bare UI Automation ControlType id, e.g. |
| 5 | +``"ControlType_50000"`` for a button. That is unreadable, and the dump |
| 6 | +carries no stable per-node identity (UIA RuntimeId needs the live element, |
| 7 | +which a serialised dump has thrown away). ``ax_tree_walk`` adds the pure, |
| 8 | +platform-agnostic post-processing the dump lacks: |
| 9 | +
|
| 10 | +* :func:`control_type_name` / :func:`humanize_role` — translate a UIA |
| 11 | + ControlType id (or ``"ControlType_NNNNN"`` string) to a friendly name, |
| 12 | +* :func:`humanize_tree` — a deep copy of the tree with every role humanised, |
| 13 | +* :func:`assign_node_paths` — a deep copy stamping each node with a stable |
| 14 | + positional ``path`` (``"0.2.1"``) — a pure stand-in for RuntimeId identity, |
| 15 | +* :func:`find_by_path` — resolve a node back from its path. |
| 16 | +
|
| 17 | +Pure-stdlib over :class:`AXTreeNode` values; no device or backend access, no |
| 18 | +``PySide6``. Compose it on top of any ``dump_accessibility_tree`` output. |
| 19 | +""" |
| 20 | +from typing import Optional, Union |
| 21 | + |
| 22 | +from je_auto_control.utils.accessibility.tree import AXTreeNode |
| 23 | + |
| 24 | +# UIA ControlType ids → friendly names (UIAutomationClient ControlTypeId range). |
| 25 | +_CONTROL_TYPE_NAMES = { |
| 26 | + 50000: "Button", 50001: "Calendar", 50002: "CheckBox", 50003: "ComboBox", |
| 27 | + 50004: "Edit", 50005: "Hyperlink", 50006: "Image", 50007: "ListItem", |
| 28 | + 50008: "List", 50009: "Menu", 50010: "MenuBar", 50011: "MenuItem", |
| 29 | + 50012: "ProgressBar", 50013: "RadioButton", 50014: "ScrollBar", |
| 30 | + 50015: "Slider", 50016: "Spinner", 50017: "StatusBar", 50018: "Tab", |
| 31 | + 50019: "TabItem", 50020: "Text", 50021: "ToolBar", 50022: "ToolTip", |
| 32 | + 50023: "Tree", 50024: "TreeItem", 50025: "Custom", 50026: "Group", |
| 33 | + 50027: "Thumb", 50028: "DataGrid", 50029: "DataItem", 50030: "Document", |
| 34 | + 50031: "SplitButton", 50032: "Window", 50033: "Pane", 50034: "Header", |
| 35 | + 50035: "HeaderItem", 50036: "Table", 50037: "TitleBar", 50038: "Separator", |
| 36 | + 50039: "SemanticZoom", 50040: "AppBar", |
| 37 | +} |
| 38 | +_ROLE_PREFIX = "ControlType_" |
| 39 | + |
| 40 | + |
| 41 | +def control_type_name(control_type: int) -> str: |
| 42 | + """Return the friendly name for a UIA ControlType id (e.g. ``50000`` → ``Button``). |
| 43 | +
|
| 44 | + Unknown ids fall back to ``"ControlType_<id>"`` so nothing is lost. |
| 45 | + """ |
| 46 | + cid = int(control_type) |
| 47 | + return _CONTROL_TYPE_NAMES.get(cid, f"{_ROLE_PREFIX}{cid}") |
| 48 | + |
| 49 | + |
| 50 | +def humanize_role(role: Union[str, int]) -> str: |
| 51 | + """Map a raw UIA role to a friendly name. |
| 52 | +
|
| 53 | + Accepts an int id (``50000``), a ``"ControlType_50000"`` string, or a bare |
| 54 | + ``"50000"`` string. Any role that is not a recognised ControlType — already |
| 55 | + friendly (``"Button"``) or a non-UIA role (``"AXApplication"``) — is returned |
| 56 | + unchanged. |
| 57 | + """ |
| 58 | + if isinstance(role, int): |
| 59 | + return control_type_name(role) |
| 60 | + text = str(role) |
| 61 | + digits = text[len(_ROLE_PREFIX):] if text.startswith(_ROLE_PREFIX) else text |
| 62 | + if digits.isdigit(): |
| 63 | + return control_type_name(int(digits)) |
| 64 | + return text |
| 65 | + |
| 66 | + |
| 67 | +def humanize_tree(node: AXTreeNode) -> AXTreeNode: |
| 68 | + """Return a deep copy of ``node`` with every role run through :func:`humanize_role`.""" |
| 69 | + return AXTreeNode( |
| 70 | + name=node.name, role=humanize_role(node.role), bounds=node.bounds, |
| 71 | + app_name=node.app_name, process_id=node.process_id, |
| 72 | + attributes=dict(node.attributes), |
| 73 | + children=[humanize_tree(child) for child in node.children], |
| 74 | + ) |
| 75 | + |
| 76 | + |
| 77 | +def assign_node_paths(node: AXTreeNode, prefix: str = "0") -> AXTreeNode: |
| 78 | + """Return a deep copy stamping each node with a stable positional ``path``. |
| 79 | +
|
| 80 | + The root is ``"0"``; its third child is ``"0.2"``, and so on. The path is a |
| 81 | + pure stand-in for a RuntimeId: stable for a given tree shape and re-resolvable |
| 82 | + with :func:`find_by_path`. Stored under ``attributes["path"]``. |
| 83 | + """ |
| 84 | + attributes = dict(node.attributes) |
| 85 | + attributes["path"] = prefix |
| 86 | + children = [assign_node_paths(child, f"{prefix}.{index}") |
| 87 | + for index, child in enumerate(node.children)] |
| 88 | + return AXTreeNode( |
| 89 | + name=node.name, role=node.role, bounds=node.bounds, |
| 90 | + app_name=node.app_name, process_id=node.process_id, |
| 91 | + attributes=attributes, children=children, |
| 92 | + ) |
| 93 | + |
| 94 | + |
| 95 | +def find_by_path(root: AXTreeNode, path: str) -> Optional[AXTreeNode]: |
| 96 | + """Resolve the node addressed by ``path`` (e.g. ``"0.2.1"``); ``None`` if absent.""" |
| 97 | + parts = str(path).split(".") |
| 98 | + if not parts or parts[0] != "0": |
| 99 | + return None |
| 100 | + node = root |
| 101 | + for part in parts[1:]: |
| 102 | + if not part.isdigit(): |
| 103 | + return None |
| 104 | + index = int(part) |
| 105 | + if index >= len(node.children): |
| 106 | + return None |
| 107 | + node = node.children[index] |
| 108 | + return node |
0 commit comments