-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxls_parse.py
executable file
·514 lines (414 loc) · 14.4 KB
/
xls_parse.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
#!/usr/bin/env python
import argparse
import sys
from pathlib import Path
from typing import Optional
import openpyxl
from openpyxl.cell.cell import Cell, MergedCell
from openpyxl.formula import Tokenizer
from openpyxl.formula.tokenizer import Token
def dbgp(msg):
return
print(msg)
WORKBOOK = None
WORKBOOK_DATA = None
LINES = []
PROCESSED_CELLS = set()
WORKSHEET_STACK = []
FAPP_XML_OBJ = "fapp_xml"
SPECIAL_FAPP_XML_CALL = {
"C4": f"{FAPP_XML_OBJ}.get_counter_timer_freq()",
"G4": f"{FAPP_XML_OBJ}.get_measured_time()",
"G5": f"{FAPP_XML_OBJ}.get_node_name()",
"G10": f"{FAPP_XML_OBJ}.get_process_no()",
"G11": f"{FAPP_XML_OBJ}.get_cmg_no()",
"G12": f"{FAPP_XML_OBJ}.get_measured_region()",
"C10": f"{FAPP_XML_OBJ}.get_vector_length()",
}
# These cells are read by formulas and are "hardcoded" in the excel
# sheet (i.e. they are not read from anywhere else, e.g. from xml).
DATA_VALUES = {
"C6",
"C7",
"C8",
"C9",
"C14",
"C15",
"C16",
"C17",
"Y4",
}
HEADER_ROW = 29
TOP_ROW = 30
BOTOM_ROW = 41
LEFT_COLUMN = 29
RIGHT_COLUMN = 334
INFIX_OP_MAP = {
"=": "==",
"^": "**",
}
def is_event_cell(cell_id):
cell = cell_id_to_obj(cell_id)
col = cell.col_idx
row = cell.row
result = (
LEFT_COLUMN <= col
and col <= RIGHT_COLUMN
and TOP_ROW <= row
and row <= BOTOM_ROW
)
return result
def full_cell_id(cell_id):
dbgp(f"> full_cell_id({cell_id})")
prefix = WORKSHEET_STACK[-1] if WORKSHEET_STACK else "report"
if cell_id in WORKBOOK.defined_names:
cell_id = WORKBOOK.defined_names[cell_id].value
elif "!" not in cell_id:
cell_id = f"{prefix}!{cell_id}"
dbgp(f"< full_cell_id -> {cell_id}")
return cell_id
def cell_obj_to_id(cell):
return cell.parent.title + "!" + cell.coordinate
def cell_id_to_varname(cell_id):
dbgp(f"> cell_id_to_varname({cell_id})")
if ":" in cell_id:
cells = cell_id_to_obj(cell_id)
cell_ids = [cell_obj_to_id(cell[0]) for cell in cells]
cell_vars = [cell_id_to_varname(cell_id) for cell_id in cell_ids]
result = f"{', '.join(cell_vars)}"
else:
cell_id = full_cell_id(cell_id)
result = cell_id.replace("!", "_").replace("$", "")
dbgp(f"< cell_id_to_varname -> {result}")
return result
def cell_id_to_obj(cell_id):
dbgp(f"> cell_id_to_obj({cell_id})")
cell_id = full_cell_id(cell_id)
ws, cell = cell_id.split("!")
cell = WORKBOOK[ws][cell]
if isinstance(cell, MergedCell):
print("WARNING: MergedCell!")
dbgp(f"< cell_id_to_obj -> {cell}")
return cell
def unknown_type_exception(token):
msg = f"ERROR: Unknown type {token.type}"
raise Exception(msg)
def unknown_subtype_exception(token):
msg = f"ERROR: Unknown subtype {token.subtype} "
msg += f"(of token type {token.type})"
raise Exception(msg)
def unknown_func_exception(token):
msg = f"ERROR: Unknown FUNC {token.value}"
raise Exception(msg)
def assert_sep_comma(token):
assert token.type == Token.SEP and token.value == ","
def assert_func_close(token):
assert token.type == Token.FUNC and token.subtype == Token.CLOSE
def python_cmd_to_read_xml(cell_id):
dbgp(f"> python_cmd_to_read_xml({cell_id})")
result = None
cell_id = cell_id.replace("$", "")
if ":" in cell_id:
cells = cell_id_to_obj(cell_id)
cell_ids = [cell_obj_to_id(cell[0]) for cell in cells]
results = [python_cmd_to_read_xml(cell_id) for cell_id in cell_ids]
if any(results):
result = f"{', '.join(results)}"
else:
cell_id = full_cell_id(cell_id)
ws, cell = cell_id.split("!")
if ws == "label":
value = WORKBOOK[ws][cell].value
result = f"'{value}'"
elif ws == "data":
if cell in SPECIAL_FAPP_XML_CALL:
result = SPECIAL_FAPP_XML_CALL[cell]
elif cell in DATA_VALUES:
result = str(WORKBOOK["data"][cell].value)
elif is_event_cell(cell_id):
cell_obj = WORKBOOK[ws][cell]
col = cell_obj.col_idx - 1
event_name = WORKBOOK[ws][HEADER_ROW][col].value
thread_id = cell_obj.row - HEADER_ROW - 1
result = f"{FAPP_XML_OBJ}.get_event('{event_name}', {thread_id})"
dbgp(f"< python_cmd_to_read_xml -> {result}")
return result
def parse_operand(token):
dbgp(f"> parse_operand({token})")
if token.subtype == Token.RANGE:
cell_id = full_cell_id(token.value)
raw = python_cmd_to_read_xml(cell_id)
if raw:
result = raw
else:
cell_to_inst(cell_id)
result = cell_id_to_varname(cell_id)
elif token.subtype == Token.TEXT:
result = token.value
elif token.subtype == Token.NUMBER:
result = token.value
else:
unknown_subtype_exception(token)
dbgp(f"< parse_operand -> {result}")
return result
def parse_if(tokens, cur):
cond, cur = parse_tokens(tokens, cur + 1)
assert_sep_comma(tokens[cur])
true_val, cur = parse_tokens(tokens, cur + 1)
assert_sep_comma(tokens[cur])
false_val, cur = parse_tokens(tokens, cur + 1)
assert_func_close(tokens[cur])
result = f"({true_val}) if ({cond}) else ({false_val})"
return result, cur
def parse_or(tokens, cur):
ops, cur = parse_tokens(tokens, cur + 1)
while tokens[cur].type == Token.SEP and tokens[cur].value == ",":
tmp, cur = parse_tokens(tokens, cur + 1)
if tmp != "":
ops += f", {tmp}"
assert_func_close(tokens[cur])
result = f"any([{ops}])"
return result, cur
def parse_count(tokens, cur):
cells, cur = parse_tokens(tokens, cur + 1)
assert_func_close(tokens[cur])
result = f"sum(1 for e in [{cells}] if e !='')"
return result, cur
def parse_sum(tokens, cur):
terms, cur = parse_tokens(tokens, cur + 1)
assert_func_close(tokens[cur])
result = f"(xls_sum([{terms}]))"
return result, cur
def parse_average(tokens, cur):
terms, cur = parse_tokens(tokens, cur + 1)
while tokens[cur].type == Token.SEP and tokens[cur].value == ",":
tmp, cur = parse_tokens(tokens, cur + 1)
if tmp != "":
terms += f", {tmp}"
assert_func_close(tokens[cur])
result = f"(xls_sum([{terms}]) / len(xls_nonempty([{terms}])))"
return result, cur
def parse_gl_lower(tokens, cur):
args, cur = parse_tokens(tokens, cur + 1)
while tokens[cur].type == Token.SEP and tokens[cur].value == ",":
tmp, cur = parse_tokens(tokens, cur + 1)
if tmp != "":
args += f", {tmp}"
assert_func_close(tokens[cur])
result = f"vba_guard_limit_lower({args})"
return result, cur
def parse_gl_upper(tokens, cur):
args, cur = parse_tokens(tokens, cur + 1)
while tokens[cur].type == Token.SEP and tokens[cur].value == ",":
tmp, cur = parse_tokens(tokens, cur + 1)
if tmp != "":
args += f", {tmp}"
assert_func_close(tokens[cur])
result = f"vba_guard_limit_upper({args})"
return result, cur
def parse_func(tokens, cur):
dbgp(f"> parse_func({tokens} {cur} = {tokens[cur]})")
token = tokens[cur]
open_func_dict = {
"IF(": parse_if,
"OR(": parse_or,
"COUNT(": parse_count,
"SUM(": parse_sum,
"AVERAGE(": parse_average,
"GuardLimitLower(": parse_gl_lower,
"GuardLimitUpper(": parse_gl_upper,
}
if token.subtype == Token.OPEN:
parse_fn = open_func_dict[token.value]
result, cur = parse_fn(tokens, cur)
elif token.subtype == Token.CLOSE:
result = None
else:
unknown_subtype_exception(token)
dbgp(f"< parse_func -> {result, cur}")
return result, cur
def parse_infix_op(token):
dbgp(f"> parse_infix_op({token})")
op_name = token.value
if token.value in INFIX_OP_MAP:
op_name = INFIX_OP_MAP[token.value]
dbgp(f"< parse_infix_op -> {op_name}")
return op_name
def parse_tokens(tokens, cur):
dbgp(f"> parse_tokens({tokens}, {cur} = {tokens[cur]})")
result = ""
while cur < len(tokens):
token = tokens[cur]
if token.type == Token.OPERAND:
result += parse_operand(token)
elif token.type == Token.FUNC:
tmp, cur = parse_func(tokens, cur)
if tmp:
result += tmp
else:
break
elif token.type == Token.OP_IN:
result += parse_infix_op(token)
elif token.type == Token.PAREN:
result += token.value
elif token.type == Token.WSPACE:
pass
elif token.type == Token.SEP:
break
else:
unknown_type_exception(token)
cur += 1
dbgp(f"< parse_tokens -> {result, cur}")
return result, cur
def cell_to_inst(cell_id):
dbgp(f"> cell_to_inst({cell_id})")
cell_id = full_cell_id(cell_id)
cell = cell_id_to_obj(cell_id)
if isinstance(cell, tuple):
for c in cell:
cell_to_inst(cell_obj_to_id(c[0]))
else:
cell_var = cell_id_to_varname(cell_id)
if cell_var in PROCESSED_CELLS:
return
WORKSHEET_STACK.append(cell.parent.title)
tokens = Tokenizer(cell.value).items
cell_val, _ = parse_tokens(tokens, 0)
WORKSHEET_STACK.pop()
line = f"{cell_var} = {cell_val}"
PROCESSED_CELLS.add(cell_var)
LINES.append(line)
dbgp(f"< cell_to_inst -> APPEND: {line}")
def get_label(cell_id):
cell = WORKBOOK_DATA["report"][cell_id]
return cell.value
def create_program(output):
result = []
with open("fapp_loader.py") as loader:
result += loader.readlines()
with open("fapp_top.py.in") as top:
result += top.readlines()
result += [line + "\n" for line in LINES]
with open("fapp_bottom.py.in") as top:
result += top.readlines()
result = "".join(result)
with open(output, "w") as out:
out.write(result)
return result
def record_entry(prefix, key, value):
LINES.append(
f"add_path({[get_label(e) for e in prefix]}, '{key}', {value}, results)"
)
def add_key_single_value_pair(prefix, key, value):
cell_to_inst(value)
record_entry(prefix, get_label(key), cell_id_to_varname(value))
def add_column_of_12_1(prefix, key, first, num_rows=12):
first_cell = cell_id_to_obj(first)
row = first_cell.row
col = first_cell.col_idx - 1
if isinstance(WORKBOOK["report"][row + 1][col], MergedCell):
add_key_single_value_pair(prefix, key, first)
else:
cell_varnames = []
for offset in range(num_rows):
cell_id = cell_obj_to_id(WORKBOOK["report"][row + offset][col])
cell_to_inst(cell_id)
cell_varnames.append(cell_id_to_varname(cell_id))
total_id = cell_obj_to_id(WORKBOOK["report"][row + num_rows][col])
cell_to_inst(total_id)
value = f"[e for e in [{', '.join(cell_varnames)}] if e != '']"
label = get_label(key)
record_entry(prefix, label, value)
# total_var = cell_id_to_varname(total_id)
# record_entry(prefix, f"{label}_total", total_var)
pass
def _col2num(col, base=26):
ords = list(map(lambda t: ord(t) - ord("A"), col))
result = ords[0]
if len(col) == 2:
result = (ords[0] + 1) * base + ords[1]
return result
def _num2col(num, base=26):
q, r = divmod(num, base)
result = chr(r + ord("A"))
if q:
result = chr(q - 1 + ord("A")) + result
return result
def col_range(begin_col, end_col):
assert len(begin_col) <= 2
assert len(end_col) <= 2
begin_num = _col2num(begin_col)
end_num = _col2num(end_col)
return [_num2col(num) for num in range(begin_num, end_num + 1)]
def add_table(prefix, begin_col, end_col, head_row, first_row):
for col in col_range(begin_col, end_col):
head_cell = col + str(head_row)
first_cell = col + str(first_row)
add_column_of_12_1(prefix, head_cell, first_cell)
def add_tables():
# TOP
add_key_single_value_pair([], "A3", "C3")
add_key_single_value_pair([], "A4", "C4")
add_key_single_value_pair([], "H3", "J3")
# add_key_single_value_pair([], "H4", "J4")
add_key_single_value_pair([], "H5", "J5")
add_key_single_value_pair([], "O3", "Q3")
add_key_single_value_pair([], "O4", "Q4")
# STATISTICS
add_table(["A8"], "C", "N", 8, 14)
# CYCLE ACCOUNTING
add_table(["P8", "R8"], "R", "S", 9, 14)
add_table(["P8", "T8"], "T", "Y", 9, 14)
add_table(["P8", "Z8"], "Z", "AA", 9, 14)
add_table(["P8", "AB8"], "AB", "AC", 9, 14)
add_table(["P8"], "AD", "AG", 8, 14)
add_table(["P8", "AH8"], "AH", "AK", 9, 14)
add_table(["P8"], "AL", "AL", 8, 14)
# BUSY
add_table(["A28"], "C", "P", 28, 34)
# CACHE
add_table(["A48"], "C", "P", 48, 55)
# INSTRUCTIONS
add_table(["A69", "C69", "C70", "C71"], "C", "I", 72, 77)
add_table(["A69", "C69", "C70", "J71"], "J", "J", 72, 77)
add_table(["A69", "C69", "K70", "K71"], "K", "O", 72, 77)
add_table(["A69", "C69", "K70", "P71"], "P", "P", 72, 77)
add_table(["A69", "Q69"], "Q", "S", 70, 77)
add_table(["A69"], "T", "T", 69, 77)
add_table(["A69", "U69"], "U", "W", 70, 77)
add_table(["A69", "X69"], "X", "Y", 71, 77)
add_table(["A69"], "Z", "AE", 69, 77)
# POWER
add_table(["AG69"], "AI", "AK", 69, 77)
# PREFETCH
add_table(["A92", "C92"], "C", "E", 93, 98)
add_table(["A92", "F92"], "F", "H", 93, 98)
add_table(["A92", "I92"], "I", "I", 93, 98)
# FLOP
add_table(["K92"], "M", "P", 92, 98)
# EXTRA
add_table(["R92", "T92"], "T", "V", 93, 98)
add_table(["R92", "W92"], "W", "AB", 93, 98)
add_table(["R92"], "AC", "AC", 92, 98)
# # DATA TRANSFER
# for col in colnames("C", "F"):
# add_key_single_value_pair(f"{col}113", f"{col}115")
# add_key_single_value_pair(f"{col}113", f"{col}116")
def main():
global WORKBOOK
global WORKBOOK_DATA
parser = argparse.ArgumentParser()
parser.add_argument(
"input_xls",
type=str,
help="Path to the input xls file",
)
args = parser.parse_args()
filename = Path(args.input_xls).expanduser()
WORKBOOK = openpyxl.load_workbook(filename)
WORKBOOK_DATA = openpyxl.load_workbook(filename, data_only=True)
add_tables()
out_file = sys.argv[0].replace(".py", ".out.py")
create_program(Path(out_file).expanduser())
print(f"Created {out_file}")
main()