1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
|
#!/usr/bin/python
# dump-tree.py
#
# Command-line tool to dump the tree of accessible objects for a given application.
# Usage: dump-tree.py <APP_NAME>
#
# Copyright 2025 Igalia, S.L.
# Author: Joanmarie Diggs <jdiggs@igalia.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., Franklin Street, Fifth Floor,
# Boston MA 02110-1301 USA.
# pylint: disable=invalid-name
# pylint: disable=wrong-import-position
"""Command-line tool to dump the tree of accessible objects for a given application."""
from datetime import datetime
import sys
import gi
gi.require_version("Atspi", "2.0")
from gi.repository import Atspi, GLib
def as_string(obj: Atspi.Accessible, prefix: str) -> str:
"Convert an accessible object to a string representation."
timestamp = f"{datetime.now():%H:%M:%S}"
indent = " " * (len(prefix) + len(timestamp) + 2)
return (f"{timestamp} {prefix} "
f"{get_role_as_string(obj)} ({hex(id(obj))}) "
f"NAME: '{get_name(obj)}' "
f"DESCRIPTION: '{get_description(obj)}'"
f"\n{indent}PARENT: {get_parent_as_string(obj)} "
f"INDEX IN PARENT: {get_index_in_parent(obj)}"
f"\n{indent}ATTRIBUTES: {get_attributes_as_string(obj)}"
f"\n{indent}RECT: {get_rect_as_string(obj)}"
f"\n{indent}STATES: {get_states_as_string(obj)}"
f"\n{indent}RELATIONS: {get_relations_as_string(obj)}"
f"\n{indent}TEXT: {get_text(obj)}"
)
def get_rect_as_string(obj: Atspi.Accessible) -> str:
"Get the rectangle dimensions of an accessible object as a string."
try:
if Atspi.Accessible.get_role(obj) == Atspi.Role.APPLICATION:
return ""
except GLib.GError as error:
print(f"Exception in get_role: {error}")
return ""
try:
rect = Atspi.Component.get_extents(obj, Atspi.CoordType.WINDOW)
except GLib.GError as error:
print(f"Exception in get_extents: {error}")
return ""
return f"X:{rect.x}, Y:{rect.y}, WIDTH:{rect.width}, HEIGHT:{rect.height}"
def get_relations_as_string(obj: Atspi.Accessible) -> str:
"Get the relations of an accessible object as a string."
try:
relations = Atspi.Accessible.get_relation_set(obj)
except GLib.GError as error:
print(f"Exception in get_relation_set: {error}")
return ""
result = []
for relation in relations:
if relation.get_n_targets() == 0:
continue
targets = [relation.get_target(i) for i in range(relation.get_n_targets())]
target_addresses = ", ".join(
[f"{get_role_as_string(target)} ({hex(id(target))})" for target in targets]
)
result.append(f"{relation.get_relation_type().value_nick}: {target_addresses}")
return " ".join(result) or "(none)"
def get_states_as_string(obj: Atspi.Accessible) -> str:
"Get the states of an accessible object as a string."
try:
state_set = Atspi.Accessible.get_state_set(obj)
except GLib.GError as error:
print(f"Exception in get_state_set: {error}")
return ""
return " ".join([s.value_nick for s in state_set.get_states()]) or "(none)"
def get_name(obj: Atspi.Accessible, fallback_on_labelled_by: bool = True) -> str:
"Get the name of an accessible object."
try:
name = Atspi.Accessible.get_name(obj)
except GLib.GError as error:
print(f"Exception in get_name: {error}")
return ""
if name:
return name
result = "(none)"
if not fallback_on_labelled_by:
return result
try:
relations = Atspi.Accessible.get_relation_set(obj)
except GLib.GError as error:
print(f"Exception in get_relation_set: {error}")
return ""
for relation in relations:
if relation.get_relation_type() != Atspi.RelationType.LABELLED_BY:
continue
targets = [relation.get_target(i) for i in range(relation.get_n_targets())]
result = " ".join([get_name(target, False) for target in targets])
if result:
result += " (from labelled-by relation)"
return result
def get_description(obj: Atspi.Accessible, fallback_on_described_by: bool = True) -> str:
"Get the description of an accessible object."
try:
description = Atspi.Accessible.get_description(obj)
except GLib.GError as error:
print(f"Exception in get_description: {error}")
return ""
if description:
return description
result = "(none)"
if not fallback_on_described_by:
return result
try:
relations = Atspi.Accessible.get_relation_set(obj)
except GLib.GError as error:
print(f"Exception in get_relation_set: {error}")
return ""
for relation in relations:
if relation.get_relation_type() != Atspi.RelationType.DESCRIBED_BY:
continue
targets = [relation.get_target(i) for i in range(relation.get_n_targets())]
result = " ".join([get_name(target, False) for target in targets])
if result:
result += " (from described-by relation)"
return result
def get_attributes_as_string(obj: Atspi.Accessible) -> str:
"Get the attributes of an accessible object as a string."
try:
attributes = Atspi.Accessible.get_attributes(obj)
except GLib.GError as error:
print(f"Exception in get_attributes: {error}")
return ""
if not attributes:
return "(none)"
result = []
for key, value in attributes.items():
if value is None:
value = "(null)"
result.append(f"{key}={value}")
return ", ".join(result)
def get_role_as_string(obj: Atspi.Accessible) -> str:
"Get the role of an accessible object."
try:
return Atspi.Accessible.get_role(obj).value_name
except GLib.GError as error:
print(f"Exception in get_role: {error}")
return "(unknown role)"
def get_text(obj: Atspi.Accessible) -> str:
"Get the text of an accessible object."
try:
length = Atspi.Text.get_character_count(obj)
result = Atspi.Text.get_text(obj, 0, length)
except GLib.GError:
return "(not implemented)"
result = result.replace("\n", "\\n")
if len(result) > 80:
result = result[:80] + "[...]"
return f"'{result}'"
def get_parent(obj: Atspi.Accessible) -> Atspi.Accessible:
"Get the parent of an accessible object."
try:
if Atspi.Accessible.get_role(obj) == Atspi.Role.APPLICATION:
return None
except GLib.GError as error:
print(f"Exception in get_role: {error}")
return None
try:
return Atspi.Accessible.get_parent(obj)
except GLib.GError as error:
print(f"Exception in get_parent: {error}")
return None
def get_parent_as_string(obj: Atspi.Accessible) -> str:
"Get the parent of an accessible object."
parent = get_parent(obj)
if parent:
return f"{get_role_as_string(parent)} ({hex(id(parent))})"
return ""
def get_index_in_parent(obj: Atspi.Accessible) -> int:
"Get the index of an accessible object in its parent's children."
parent = get_parent(obj)
if not parent:
return -1
try:
index = Atspi.Accessible.get_index_in_parent(obj)
except GLib.GError as error:
print(f"Exception in get_index_in_parent: {error}")
return -1
if index < 0:
print(f"WARNING: Object {hex(id(obj))} has invalid index in parent {hex(id(parent))}")
return -1
child = Atspi.Accessible.get_child_at_index(parent, index)
if child != obj:
print(f"WARNING: Object {hex(id(obj))} at index {index} in parent {hex(id(parent))} "
f"is not the expected child {hex(id(child))}")
return index
def get_children(obj: Atspi.Accessible) -> list[Atspi.Accessible]:
"Get the children of an accessible object."
children = []
try:
count = Atspi.Accessible.get_child_count(obj)
except GLib.GError as error:
print(f"Exception in get_child_count: {error}")
return []
for i in range(count):
try:
children.append(Atspi.Accessible.get_child_at_index(obj, i))
except GLib.GError as error:
print(f"Exception in get_child_at_index: {error}")
return children
return children
def clear_cache_single(obj: Atspi.Accessible) -> None:
"Clear the cache for a single accessible object."
try:
Atspi.Accessible.clear_cache_single(obj)
except GLib.GError as error:
print(f"Exception in clear_cache_single: {error}")
def print_tree(root: Atspi.Accessible, indent: int = 0) -> None:
"Print the tree structure of accessible objects."
clear_cache_single(root)
prefix = f"{' ' * indent}-->"
print(f"{as_string(root, prefix)}")
for child in get_children(root):
print_tree(child, indent + 1)
def find_application_with_name(app_name: str) -> Atspi.Accessible:
"Find the accessible application with the specified name."
try:
desktop = Atspi.get_desktop(0)
except GLib.GError as error:
print(f"Exception getting desktop from Atspi: {error}")
return None
apps = []
for child in get_children(desktop):
if get_name(child, False) == app_name:
return child
apps.append(child)
apps_as_string = "\n".join([f"{i + 1}. {get_name(app, False)}" for i, app in enumerate(apps)])
print(
f"Application '{app_name}' not found.\nDesktop has {len(apps)} accessible applications:\n"
f"{apps_as_string}"
)
return None
def main():
"""Starts the tree dumper and waits for events."""
if len(sys.argv) != 2:
print("Usage: dump-tree.py <APP_NAME>")
sys.exit(1)
app = find_application_with_name(sys.argv[1])
if not app:
sys.exit(1)
print_tree(app)
print("Tree dump complete.")
if __name__ == "__main__":
main()
|