-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.py
162 lines (124 loc) · 3.31 KB
/
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
from pprint import pprint
import json
raw_lines = open('sample.sa').readlines()
comments = []
tags = {}
trackplan = {
'count': None,
'data': [],
}
timer = {
'unit': None,
'data': [],
}
trace = {
'pt': None,
'data': [],
}
def comment(raw):
comments.append(raw.lstrip(';').strip())
def info(raw):
line = raw.lstrip('#')
tag, data = line.split('=', 1)
tags.update({tag: data.strip().rstrip('=').strip()})
def timer_units(raw):
units = raw.split('#unit=')[1].strip().rstrip('>')
return units
def trackplan_sectors(raw):
count = raw.split('#sectors=')[1].strip().rstrip('>')
return count
def trace_pt(raw):
count = raw.split('#pt=')[1].strip().rstrip('>')
return count
def parse_timer(raw):
data = []
raw_iter = iter(raw['data'])
prev_sector = 0
lap = 1
for head, foot in zip(raw_iter, raw_iter):
sector, time = head.lstrip('#').split(',')
if int(sector) < int(prev_sector):
lap = lap + 1
prev_sector = sector
data.append({ 'lap': int(lap), 'sector': int(sector), 'time': time, 'raw': foot })
return {
'unit': raw['unit'],
'sectors': data
}
def parse_trackplan(raw):
data = []
for line in raw['data']:
sector, start_lat, start_lon, end_lat, end_lon = line.split(',')
data.append({
'sector': int(sector),
'start': [float(start_lat), float(start_lon)],
'end': [float(end_lat), float(end_lon)]
})
return {
'count': int(raw['count']),
'data': data
}
def parse_trace(raw):
return {
'points': raw['pt'],
'data': raw['data']
}
def parse(raw):
header = []
data = []
for line in raw_lines:
if len(data) > 0 or line[0] == '<':
data.append(line)
else:
header.append(line)
return header, data
header, data = parse(raw_lines)
for line in header:
if line[0] == ';':
comment(line)
if line[0] == '#':
info(line)
section = None
for line in data:
if line[0] == '<':
if 'timer' in line:
if section is not None:
timer = parse_timer(timer)
section = None
continue
# get units
timer['unit'] = timer_units(line)
section = 'timer'
continue
if 'trackplan' in line:
if section is not None:
trackplan = parse_trackplan(trackplan)
section = None
continue
# get units
trackplan['count'] = trackplan_sectors(line)
section = 'trackplan'
continue
if 'trace' in line:
if section is not None:
trace = parse_trace(trace)
section = None
continue
# get units
trace['pt'] = trace_pt(line)
section = 'trace'
continue
if section == 'timer':
timer['data'].append(line.strip())
if section == 'trackplan':
trackplan['data'].append(line.strip())
if section == 'trace':
trace['data'].append(line.strip())
out = {
'comments': comments,
'tags': tags,
'trace': trace,
'trackplan': trackplan,
'timer': timer
}
print(json.dumps(out))