-
Notifications
You must be signed in to change notification settings - Fork 12
/
inspect.py
159 lines (126 loc) · 4.68 KB
/
inspect.py
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
from reprlib import Repr
from talon import Context, Module, actions, ctrl, ui
mod = Module()
ctx = Context()
ctx.matches = """
os: mac
"""
REPR = Repr()
REPR.indent = 4
@mod.action_class
class Actions:
def element_print_hierarchy_at_mouse_pos(
all_attributes: bool, complex_attributes: bool
):
"""Print information about the element at the mouse cursor position and its parents"""
def element_print_hierarchy(
element: ui.Element, all_attributes: bool, complex_attributes: bool
):
"""Print information about the element and its parents"""
def element_print_tree_at_mouse_pos(all_attributes: bool, complex_attributes: bool):
"""Print information about the element at the mouse cursor position, its parents and parents' siblings"""
def element_print_tree(
element: ui.Element, all_attributes: bool, complex_attributes: bool
):
"""Print information about the element, its parents and parents' siblings"""
def element_print(
element: ui.Element, all_attributes: bool, complex_attributes: bool
):
"""Print information about the element"""
@ctx.action_class("user")
class UserActions:
def element_print_hierarchy_at_mouse_pos(all_attributes, complex_attributes):
pos = ctrl.mouse_pos()
element = ui.element_at(*pos)
print(element_context(element, pos, "hierarchy"))
actions.user.element_print_hierarchy(
element, all_attributes, complex_attributes
)
def element_print_hierarchy(element, all_attributes, complex_attributes):
hierarchy = []
while element is not None:
hierarchy.append(element_dict(element, all_attributes, complex_attributes))
try:
element = element.parent
except ui.UIErr:
break
print("\n".join(map(format_attributes, hierarchy)))
def element_print_tree_at_mouse_pos(all_attributes, complex_attributes):
pos = ctrl.mouse_pos()
element = ui.element_at(*pos)
print(element_context(element, pos, "tree"))
actions.user.element_print_tree(element, all_attributes, complex_attributes)
def element_print_tree(element, all_attributes, complex_attributes):
tree = []
while element is not None:
try:
parent = element.parent
except ui.UIErr:
tree.append(
[("*0", element_dict(element, all_attributes, complex_attributes))]
)
break
children = [
(
f"*{i}" if c == element else f" {i}",
element_dict(c, all_attributes, complex_attributes),
)
for i, c in enumerate(parent.children)
]
tree.append(children)
element = parent
print(
"\n".join(
"\n".join(
format_attributes(e, (" " * level) + prefix)
for prefix, e in children
)
for level, children in enumerate(reversed(tree))
)
)
def element_print(element, all_attributes, complex_attributes):
print(
element_context(element),
format_attributes(
element_dict(element, all_attributes, complex_attributes)
),
)
def element_context(element, pos=None, display=None):
return f"""Element{
f' {display}' if display else ""
}{
f' at {tuple(map(round, pos))}' if pos else ""
} in app bundle={element.window.app.bundle!r}:"""
def format_attributes(d, prefix=""):
return f"{prefix}({', '.join(f'{k}={REPR.repr(v)}' for k, v in d.items())})"
def is_simple(val):
return type(val) in (int, bool, str)
def element_dict(element, all_attributes, complex_attributes):
ordered = {}
attributes = element.dump()
def push(key):
if key in attributes:
val = attributes.pop(key)
if complex_attributes or is_simple(val):
ordered[key] = val
push("AXRole")
push("AXSubrole")
push("AXRoleDescription")
push("AXDescription")
push("AXIdentifier")
push("AXDOMIdentifier")
push("AXTitle")
push("AXValue")
push("AXFocused")
if all_attributes:
if complex_attributes:
ordered.update(attributes)
if attributes := element.parameterized_attrs:
ordered["parameterized_attrs"] = attributes
else:
ordered.update(
{key: val for key, val in attributes.items() if is_simple(val)}
)
if actions := element.actions:
ordered["actions"] = actions
return ordered