-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_ui.py
More file actions
executable file
·92 lines (78 loc) · 3.14 KB
/
Copy pathparse_ui.py
File metadata and controls
executable file
·92 lines (78 loc) · 3.14 KB
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
#!/usr/bin/env python3
import xml.etree.ElementTree as ET
import json
import re
def parse_bounds(bounds_str):
"""Parse bounds string like '[0,0][1840,2944]' into coordinates"""
match = re.match(r'\[(\d+),(\d+)\]\[(\d+),(\d+)\]', bounds_str)
if match:
left, top, right, bottom = map(int, match.groups())
return {
'left': left,
'top': top,
'right': right,
'bottom': bottom,
'width': right - left,
'height': bottom - top
}
return None
def parse_node_hierarchical(node, flat_list, parent_index=None, depth=0):
"""Recursively parse XML node preserving hierarchy"""
if node.tag == 'node':
bounds = parse_bounds(node.get('bounds', ''))
element = {
'class': node.get('class', ''),
'package': node.get('package', ''),
'id': node.get('resource-id', ''),
'text': node.get('text', ''),
'desc': node.get('content-desc', ''),
'clickable': node.get('clickable', 'false') == 'true',
'enabled': node.get('enabled', 'false') == 'true',
'focusable': node.get('focusable', 'false') == 'true',
'focused': node.get('focused', 'false') == 'true',
'scrollable': node.get('scrollable', 'false') == 'true',
'selected': node.get('selected', 'false') == 'true',
'checkable': node.get('checkable', 'false') == 'true',
'checked': node.get('checked', 'false') == 'true',
'bounds': bounds,
'depth': depth,
'parentIndex': parent_index,
'childIndices': []
}
# Add to flat list and get index
current_index = len(flat_list)
flat_list.append(element)
# Update parent's childIndices
if parent_index is not None:
flat_list[parent_index]['childIndices'].append(current_index)
# Recursively process children
for child in node:
parse_node_hierarchical(child, flat_list, current_index, depth + 1)
else:
# For non-node elements (like hierarchy root), just process children
for child in node:
parse_node_hierarchical(child, flat_list, parent_index, depth)
def main():
import os
script_dir = os.path.dirname(os.path.abspath(__file__))
# Parse XML
tree = ET.parse(os.path.join(script_dir, 'window_dump.xml'))
root = tree.getroot()
# Extract screen dimensions from root node
root_bounds = parse_bounds(root.get('bounds', '[0,0][1840,2944]'))
# Parse all elements with hierarchy
elements = []
parse_node_hierarchical(root, elements)
# Create output data
output = {
'screenWidth': root_bounds['width'] if root_bounds else 1840,
'screenHeight': root_bounds['height'] if root_bounds else 2944,
'elements': elements
}
# Write JSON
with open(os.path.join(script_dir, 'ui_data.json'), 'w') as f:
json.dump(output, f, indent=2)
print(f"✅ Parsed {len(elements)} UI elements")
print(f"📱 Screen size: {output['screenWidth']}×{output['screenHeight']}")
if __name__ == '__main__':
main()