-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaction_records.py
More file actions
263 lines (202 loc) · 9.26 KB
/
Copy pathaction_records.py
File metadata and controls
263 lines (202 loc) · 9.26 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
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
import json
class BasicAction:
def __init__(self, name, arguments):
self.name = name
self.arguments = arguments
def compute_talon_script(self):
code = self.name + '(' + ', '.join(self.compute_arguments_converted_to_talon_script_string()) + ')'
return code
def compute_arguments_converted_to_talon_script_string(self):
result = []
for argument in self.arguments:
if type(argument) == str:
converted_argument = self.compute_string_argument(argument)
elif type(argument) == bool:
converted_argument = str(compute_talon_script_boolean_value(argument))
else:
converted_argument = str(argument)
result.append(converted_argument)
return result
def compute_string_argument(self, argument: str):
string_argument = "'" + argument.replace("'", "\\'") + "'"
return string_argument
def get_name(self):
return self.name
def get_arguments(self):
return self.arguments
def to_json(self) -> str:
return json.dumps({'name': self.name, 'arguments': self.arguments}, cls = BasicActionEncoder)
@staticmethod
def from_json(text: str):
representation = json.loads(text)
return BasicAction(representation['name'], representation['arguments'])
def __eq__(self, other) -> bool:
return other is not None and self.name == other.name and self.arguments == other.arguments
def __repr__(self):
return self.__str__()
def __str__(self):
return self.to_json()
class BasicActionEncoder(json.JSONEncoder):
def default(self, object):
if isinstance(object, TalonCapture):
return object.to_json()
return json.JSONEncoder.default(self, object)
def compute_talon_script_boolean_value(value: bool):
if value:
return 1
return 0
class TalonCapture:
def __init__(self, name: str, instance: int, postfix: str = ''):
self.name = name
self.instance = instance
self.postfix = postfix
def __repr__(self):
return self.__str__()
def __str__(self):
return self.name + '_' + str(self.instance) + self.postfix
def compute_command_component(self):
return f'<{self.name}>'
def to_json(self):
return json.dumps({'name': self.name, 'instance': self.instance})
@staticmethod
def from_json(json):
attributes = json.loads(json)
return TalonCapture(attributes['name'], attributes['instance'])
def __eq__(self, other) -> bool:
return self.name == other.name and self.instance == other.instance and self.postfix == other.postfix
class Command:
def __init__(self, name: str, actions, seconds_since_action: int = None):
self.name = name
self.actions = actions
self.seconds_since_action = seconds_since_action
def get_name(self) -> str:
return self.name
def get_actions(self):
return self.actions
def copy(self):
return Command(self.name, self.actions[:])
def has_same_actions_as(self, other) -> bool:
return self.actions == other.actions
def set_name(self, name: str) -> None:
self.name = name
def is_time_information_available(self) -> bool:
return self.seconds_since_action is not None
def get_seconds_since_action(self) -> int:
return self.seconds_since_action
def is_command_record(self):
return True
def __repr__(self):
return self.__str__()
def __str__(self):
representation = f'Command({self.name}{", " + str(self.seconds_since_action) if self.is_time_information_available() else ""},\n'
for action in self.actions: representation += str(action) + '\n'
representation += ')'
return representation
class CommandChain(Command):
def __init__(self, name: str, actions, chain_number: int = 0, chain_size: int = 0):
super().__init__(name, actions)
self.chain_number: int = chain_number
self.chain_size: int = chain_size
def append_command(self, command):
if self.name is None:
self.name = command.get_name()
else:
self.name += f' {command.get_name()}'
self.actions.extend(command.get_actions())
self.chain_size += 1
def get_chain_number(self):
return self.chain_number
def get_chain_ending_index(self):
return self.get_next_chain_index() - 1
def get_next_chain_index(self):
return self.chain_number + self.chain_size
def get_size(self):
return self.chain_size
class RecordingStart:
def is_command_record(self):
return False
COMMAND_NAME_PREFIX = 'Command: '
RECORDING_START_MESSAGE = 'START'
TIME_DIFFERENCE_PREFIX = 'T'
class RecordParser:
def __init__(self, path: str):
self.commands = []
self.current_command_name = ''
self.current_command_actions = []
self.seconds_since_last_action = None
self.seconds_since_last_action_for_next_command = None
self.time_information_found_after_command = False
self.parse_path(path)
def parse_path(self, path: str):
self.process_file_lines(path)
if self.is_command_found():
self.add_current_command()
def process_file_lines(self, path: str):
with open(path, 'r') as file:
line = file.readline()
while line:
line_without_trailing_newline = line.strip()
self.process_line(line_without_trailing_newline)
line = file.readline()
def process_line(self, line: str):
if is_action(line):
self.add_action_based_on_line(line)
elif is_line_command_start(line):
self.process_command_start(line)
elif is_line_time_deference(line):
self.process_time_difference(line)
elif is_line_recording_start(line):
self.process_recording_start()
if is_line_command_ending(line):
self.reset_command_information_except_name()
def add_action_based_on_line(self, line_without_trailing_newline: str):
self.current_command_actions.append(BasicAction.from_json(line_without_trailing_newline))
def process_command_start(self, line_without_trailing_newline: str):
self.add_current_command_if_available()
self.current_command_name = compute_command_name_without_prefix(line_without_trailing_newline)
def add_current_command_if_available(self):
if self.is_command_found():
self.add_current_command()
def is_command_found(self):
return len(self.current_command_actions) > 0
def add_current_command(self):
seconds_since_last_action = self.seconds_since_last_action
if not self.time_information_found_after_command:
seconds_since_last_action = self.seconds_since_last_action_for_next_command
self.commands.append(Command(self.current_command_name, self.current_command_actions[:], seconds_since_last_action))
def process_time_difference(self, line_without_trailing_newline):
self.seconds_since_last_action = self.seconds_since_last_action_for_next_command
self.seconds_since_last_action_for_next_command = compute_seconds_since_last_action(line_without_trailing_newline)
self.time_information_found_after_command = True
def process_recording_start(self):
self.add_current_command_if_available()
self.commands.append(RecordingStart())
self.current_command_name = ''
def reset_command_information_except_name(self):
self.current_command_actions = []
self.seconds_since_last_action = None
if not self.time_information_found_after_command:
self.seconds_since_last_action_for_next_command = None
self.time_information_found_after_command = False
def get_record(self):
return self.commands
def read_file_record(path: str):
'''Obtains a list of the basic actions performed by the commands in the specified record file'''
parser = RecordParser(path)
return parser.get_record()
def compute_command_name_without_prefix(command_name: str):
return command_name[len(COMMAND_NAME_PREFIX):]
def compute_seconds_since_last_action(time_record: str) -> int:
return int(time_record[1:])
def is_action(text: str):
return text.startswith('{')
def compute_time_difference_text(difference: int) -> str:
return TIME_DIFFERENCE_PREFIX + str(difference)
def is_line_command_ending(line_without_trailing_newline: str):
return is_line_command_start(line_without_trailing_newline) or is_line_recording_start(line_without_trailing_newline)
def is_line_command_start(line: str):
return line.startswith(COMMAND_NAME_PREFIX)
def is_line_time_deference(line: str):
return line.startswith(TIME_DIFFERENCE_PREFIX)
def is_line_recording_start(line_without_trailing_newline: str):
return line_without_trailing_newline == RECORDING_START_MESSAGE