-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGui_Parser.py
263 lines (218 loc) · 11.2 KB
/
Gui_Parser.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
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
# SPDX-FileCopyrightText: 2025 German Aerospace Center (DLR)
# SPDX-FileContributor: Tim Rosenbach <tim.rosenbach@dlr.de>
#
# SPDX-License-Identifier: MIT
import base64
import configparser
import shutil
from pywinauto.controls.hwndwrapper import HwndWrapper
from pywinauto.controls.uia_controls import ListItemWrapper
from pywinauto.controls.uia_controls import HeaderWrapper
from Agent import Agent
from Workflow_Administrator import Workflow_Administrator
config = configparser.ConfigParser()
config.read("config.cfg")
settings = config["settings"]
PATH_TO_ICON_PROMPT = "prompts\\icon_description_promt.txt"
PATH_TO_IMAGES = "temp\\images\\"
class GUI_Parser:
"""Class that can parse GUI elements into textual description.
Attributes:
workflow_administrator (Workflow_Administrator): Workflow administrator to get the workflow editor from.
icon_agent (Agent): Agent to generate descriptions for icons.
generated_Image_descriptions (list[dict]): List of generated image descriptions.
Methods:
create_gui_information(element: HwndWrapper) -> dict: Creates a dictionary with information about the GUI element.
get_icon_description(icon_path: str) -> str: Gets a description for the icon at the given path.
"""
generated_Image_descriptions = []
def __init__(self, workflow_administrator: Workflow_Administrator) -> None:
self.workflow_administrator = workflow_administrator
self.icon_agent = Agent(settings["icon_description_model"])
def _is_control_valid(self, window: HwndWrapper) -> bool:
"""Valid control elments are visible, enabled, have a control id, and have a non-zero size."""
if(not window.is_visible()):
return False
if(not window.is_enabled()):
return False
if(window.control_id() == None):
return False
if window.friendly_class_name() in ("Header", "TitleBar", "TabItem", "ListItem", "HeaderItem"):
return False
rect = window.rectangle()
if(rect.width() == 0 or rect.height() == 0):
return False
return True
def create_gui_information(self, element: HwndWrapper) -> dict:
"""Creates a dictionary with information about the GUI element.
Args:
element (HwndWrapper): GUI element to get information from.
Returns:
dict: Dictionary with information about the GUI element.
"""
if self._is_control_valid(element):
rect = element.rectangle()
element_info = {
"class_name": element.friendly_class_name(),
"control_type": type(element).__name__,
"control_id": element.control_id(),
"rectangle": ['L' + str(rect.left), 'T' + str(rect.top), 'R' + str(rect.right), 'B' + str(rect.bottom)],
}
if element.window_text() != '':
element_info["text"] = element.window_text()
match element.friendly_class_name():
case "RadioButton":
state = element.is_selected()
if state == 0:
element_info["check_state"] = "unchecked"
elif state == 1:
element_info["check_state"] = "checked"
else:
element_info["check_state"] = "indeterminate"
case "ListBox":
element_info["single_selection"] = (element.can_select_multiple() == True)
element_info["columns"] = []
element_info["items"] = []
for index, item in enumerate(element.get_items()):
if type(item) == ListItemWrapper:
text = item.texts()
if len(text) == 1:
text = text[0]
element_info["items"].append({
"index": index,
"text": text,
"selected": item.is_selected() == 1
})
elif type(item) == HeaderWrapper:
for header in item.children():
element_info["columns"].append(header.texts())
if element_info["columns"] == []:
element_info.pop("columns")
case "ComboBox":
if element.class_name() == "ComboBox":
element.expand()
element_info["items"] = []
list = None
for child in element.children():
if child.friendly_class_name() == "ListBox":
list = child
for index, item in enumerate(list.get_items()):
if type(item) == ListItemWrapper:
text = item.texts()
if len(text) == 1:
text = text[0]
element_info["items"].append({
"index": index,
"text": text,
"selected": item.is_selected() == 1
})
element.collapse()
else:
print("Weird ComboBox")
case "CheckBox":
check_state = element.get_toggle_state()
if check_state == 0:
element_info["check_state"] = "unchecked"
elif check_state == 1:
element_info["check_state"] = "checked"
else:
element_info["check_state"] = "indeterminate"
case "Image":
element.capture_as_image().save(PATH_TO_IMAGES + "icon.png")
description = self.get_icon_description(PATH_TO_IMAGES + "icon.png")
element_info["image_description"] = description
case "ListView":
element_info["columns"] = []
header_control = element.get_header_control()
for i in range(header_control.item_count()):
element_info["columns"].append(header_control.get_column_text(i))
element_info["content"] = []
for row in range(element.item_count()):
row_info = []
for column in range(element.column_count()):
row_info.append(element.get_item(row, column).text())
element_info["content"].append(row_info)
case "TabControl":
element_info["tabs"] = []
for i in range(element.tab_count()):
text = element.texts()[i]
if text != "":
element_info["tabs"].append(
{
"index": i,
"text": element.texts()[i],
"selected": i == element.get_selected_tab()
}
)
case "Edit":
element_info["content"] = element.get_value()
element_info["is_editable"] = element.is_editable()
case "TreeView":
items = element.print_items()
lines = items.strip().splitlines()
result = []
current_parents = []
def process_line(line):
# Remove leading spaces to determine level of indentation
level = len(line) - len(line.lstrip())
text = line.strip()
return level, text
for line in lines:
level, text = process_line(line)
current = {"text": text}
if len(current_parents) == level:
current_parents.append(current)
else:
if level == 0:
result.append(current_parents[0])
current_parents[0] = current
else:
if "children" not in current_parents[level-1]:
current_parents[level-1]["children"] = [current_parents[level]]
else:
current_parents[level-1]["children"].append(current_parents[level])
current_parents[level] = current
for i in range(len(current_parents)-1, 0, -1):
if "children" not in current_parents[i-1]:
current_parents[i-1]["children"] = [current_parents[i]]
else:
current_parents[i-1]["children"].append(current_parents[i])
current_parents[i] = current
if len(current_parents) > 0:
result.append(current_parents[0])
element_info["items"] = result
if element.control_id() == self.workflow_administrator.get_workflow_editor().control_id():
element_info["workflow_components"] = self.workflow_administrator.get_components_as_json()
element_info["sub_elements"] = []
for child in element.children():
child_info = self.create_gui_information(child)
if child_info:
element_info["sub_elements"].append(child_info)
if element_info["sub_elements"] == []:
element_info.pop("sub_elements")
return element_info
else:
if element.friendly_class_name() == "Dialog":
for child in element.children():
if child.friendly_class_name() == "Dialog":
return self.create_gui_information(child)
return {}
def get_icon_description(self, icon_path: str) -> str:
"""Gets a description for the icon at the given path.
Args:
icon_path (str): Path to the icon image.
Returns:
str: Description of the icon.
"""
with open(icon_path, "rb") as image_file:
image_base64 = base64.b64encode(image_file.read()).decode('utf-8')
for image in self.generated_Image_descriptions:
if image["image"] == image_base64:
return image["description"]
description = self.icon_agent.make_request(PATH_TO_ICON_PROMPT, jsonFormat=False, pathsToImages=[icon_path])
self.generated_Image_descriptions.append({
"image": image_base64,
"description": description
})
shutil.copyfile(icon_path, PATH_TO_IMAGES + str(len(self.generated_Image_descriptions)) + "_icon.png")
return description