forked from microsoft/UFO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.py
200 lines (162 loc) · 6.13 KB
/
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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
import json
import os
import re
from ..utils import encode_image_from_path, print_with_color
class ExperienceLogLoader:
"""
Loading the logs from previous runs.
"""
def __init__(self, log_path: str):
"""
Initialize the LogLoader.
:param log_path: The path of the log file.
"""
self.log_path = log_path
self.response = self.load_response_log()
self.max_stepnum = self.find_max_number_in_filenames(log_path)
self.request_partition = self.get_request_partition()
self.screenshots = {}
self.logs = []
def load_response_log(self):
"""
Load the response log.
:return: The response log.
"""
response = []
response_log_path = os.path.join(self.log_path, "response.log")
with open(response_log_path, 'r', encoding='utf-8') as file:
# Read the lines and split them into a list
response_log = file.readlines()
for response_string in response_log:
try:
response.append(json.loads(response_string))
except json.JSONDecodeError:
print_with_color(f"Error loading response log: {response_string}", "yellow")
return response
@staticmethod
def find_max_number_in_filenames(log_path) -> int:
"""
Find the maximum number in the filenames.
:return: The maximum number in the filenames.
"""
# Get the list of files in the folder
files = os.listdir(log_path)
# Initialize an empty list to store extracted numbers
numbers = []
# Iterate through each file
for file in files:
# Extract the number from the filename
number = ExperienceLogLoader.extract_action_step_count(file)
if number is not None:
# Append the extracted number to the list
numbers.append(number)
if numbers:
# Return the maximum number if numbers list is not empty
return max(numbers)
else:
# Return None if no numbers are found in filenames
return None
def load_screenshot(self, stepnum: int = 0, version: str = "") -> str:
"""
Load the screenshot.
:param stepnum: The step number of the screenshot.
:param version: The version of the screenshot.
:return: The screenshot.
"""
# create version tag
if version:
version_tag = "_" + version
else:
version_tag = ""
# Get the filename of the screenshot
filename = "action_step{stepnum}{version}.png".format(stepnum=stepnum, version=version_tag)
screenshot_path = os.path.join(self.log_path, filename)
# Check if the screenshot exists
if os.path.exists(screenshot_path):
image_url = encode_image_from_path(screenshot_path)
else:
image_url = None
return image_url
def create_logs(self) -> list:
"""
Create the response log.
:return: The response log.
"""
self.logs = []
for partition in self.request_partition:
request = self.response[partition[0]]["Request"]
nround = self.response[partition[0]]["Round"]
partitioned_logs = {
"request": request,
"round": nround,
"step_num": len(partition),
**{
"step_%s" % local_step: {
"response": self.response[step],
"is_first_action": local_step == 1,
"screenshot": {
version: self.load_screenshot(step, "" if version == "raw" else version)
for version in ["raw", "selected_controls"]
}
}
for local_step, step in enumerate(partition)
},
"application": list({self.response[step]["Application"] for step in partition})
}
self.logs.append(partitioned_logs)
return self.logs
def get_request_partition(self) -> list:
"""
Partition the logs.
:return: The partitioned logs.
"""
request_partition = []
current_round = 0
current_partition = []
for step in range(self.max_stepnum):
nround = self.response[step]["Round"]
if nround != current_round:
if current_partition:
request_partition.append(current_partition)
current_partition = [step]
current_round = nround
else:
current_partition.append(step)
if current_partition:
request_partition.append(current_partition)
return request_partition
@staticmethod
def get_user_request(log_partition: dict) -> str:
"""
Get the user request.
:param log_partition: The log partition.
:return: The user request.
"""
return log_partition.get("request")
@staticmethod
def get_app_list(log_partition: dict) -> list:
"""
Get the user request.
:param log_partition: The log partition.
:return: The application list.
"""
return log_partition.get("application")
@staticmethod
def extract_action_step_count(filename : str) -> int:
"""
Extract the action step count from the filename.
:param filename: The filename.
:return: The number extracted from the filename.
"""
# Define a regular expression pattern to extract numbers
pattern = r'action_step(\d+)\.png'
# Use re.search to find the matching pattern in the filename
match = re.search(pattern, filename)
if match:
# Return the extracted number as an integer
return int(match.group(1))
else:
# Return None if no match is found
return None