-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpositioner.py
73 lines (51 loc) · 1.73 KB
/
positioner.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
import json
import argparse
import os
def main():
parser = argparse.ArgumentParser()
parser.add_argument('version', help='The `v#` folder where your generated pdfs are.')
args = parser.parse_args()
base_path = os.path.join('generated', args.version)
log_path = os.path.join(base_path, 'log')
with open(os.path.join(base_path, 'order.json')) as fp:
orders = json.load(fp)
for f in os.listdir(log_path):
if f.startswith('Answer-') and f.endswith('.log'):
test_id = f.strip('Answer-').strip('.log')
with open(os.path.join(log_path, f)) as fp:
positions = parse(fp, orders)
orders[test_id]['positions'] = positions
with open(os.path.join(base_path, 'order.json'), 'w') as fp:
json.dump(orders, fp, indent=4)
def get_description(line):
line = line.split()
return line[1].strip('()')
def get_position(line):
line = line.split()
x, y = line[2].strip('()').split(',')
return (int(x), int(y))
def get_rel_pos(x, min, max):
return (x - min) * 1.0 / (max - min)
def parse(fp, orders):
ticks = []
for line in fp:
line = line.strip()
if line.startswith('[UPPER-LEFT]'):
upper_left = get_position(line)
elif line.startswith('[UPPER-RIGHT]'):
upper_right = get_position(line)
elif line.startswith('[BOTTOM-LEFT]'):
bottom_left = get_position(line)
elif line.startswith('[BOTTOM-RIGHT]'):
bottom_right = get_position(line)
elif line.startswith('[TICK-POSITION]'):
ticks.append(dict(description=get_description(line), position=get_position(line)))
result = dict()
for tick in ticks:
x, y = tick['position']
x = get_rel_pos(x, upper_left[0], upper_right[0])
y = get_rel_pos(y, upper_left[1], bottom_left[1])
result[tick['description']] = (x, y)
return result
if __name__ == '__main__':
main()