MCPcopy
hub / github.com/TencentQQGYLab/AppAgent / traverse_tree

Function traverse_tree

scripts/and_controller.py:56–86  ·  view source on GitHub ↗
(xml_path, elem_list, attrib, add_index=False)

Source from the content-addressed store, hash-verified

54
55
56def traverse_tree(xml_path, elem_list, attrib, add_index=False):
57 path = []
58 for event, elem in ET.iterparse(xml_path, ['start', 'end']):
59 if event == 'start':
60 path.append(elem)
61 if attrib in elem.attrib and elem.attrib[attrib] == "true":
62 parent_prefix = ""
63 if len(path) > 1:
64 parent_prefix = get_id_from_element(path[-2])
65 bounds = elem.attrib["bounds"][1:-1].split("][")
66 x1, y1 = map(int, bounds[0].split(","))
67 x2, y2 = map(int, bounds[1].split(","))
68 center = (x1 + x2) // 2, (y1 + y2) // 2
69 elem_id = get_id_from_element(elem)
70 if parent_prefix:
71 elem_id = parent_prefix + "_" + elem_id
72 if add_index:
73 elem_id += f"_{elem.attrib['index']}"
74 close = False
75 for e in elem_list:
76 bbox = e.bbox
77 center_ = (bbox[0][0] + bbox[1][0]) // 2, (bbox[0][1] + bbox[1][1]) // 2
78 dist = (abs(center[0] - center_[0]) ** 2 + abs(center[1] - center_[1]) ** 2) ** 0.5
79 if dist <= configs["MIN_DIST"]:
80 close = True
81 break
82 if not close:
83 elem_list.append(AndroidElement(elem_id, ((x1, y1), (x2, y2)), attrib))
84
85 if event == 'end':
86 path.pop()
87
88
89class AndroidController:

Callers 3

task_executor.pyFile · 0.90
step_recorder.pyFile · 0.90
self_explorer.pyFile · 0.90

Calls 2

get_id_from_elementFunction · 0.85
AndroidElementClass · 0.85

Tested by

no test coverage detected