Skip to content

Commit 6929b9f

Browse files
robertlong13tridge
authored andcommitted
DFReader: init_arrays_fast: fix key error
Some logs, like ones created by other tools (not autopilots), frequently don't have some of the metadata messages like 'UNIT'. Don't crash when that happens.
1 parent b6616bd commit 6929b9f

File tree

1 file changed

+42
-39
lines changed

1 file changed

+42
-39
lines changed

DFReader.py

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,49 +1340,52 @@ def init_arrays_fast(self, progress_callback=None):
13401340
self.id_to_name[mfmt.type] = mfmt.name
13411341

13421342
# Parse the UNIT messages
1343-
mtype = self.name_to_id['UNIT']
1344-
fmt = self.formats[mtype]
1345-
mlen = fmt.len
1346-
for ofs in offsets[mtype]:
1347-
body = data[ofs+3:ofs+mlen]
1348-
if len(body)+3 < mlen:
1349-
break
1350-
elements = list(struct.unpack(fmt.msg_struct, body))
1351-
self.unit_lookup[chr(elements[1])] = null_term(elements[2])
1343+
if 'UNIT' in self.name_to_id:
1344+
mtype = self.name_to_id['UNIT']
1345+
fmt = self.formats[mtype]
1346+
mlen = fmt.len
1347+
for ofs in offsets[mtype]:
1348+
body = data[ofs+3:ofs+mlen]
1349+
if len(body)+3 < mlen:
1350+
break
1351+
elements = list(struct.unpack(fmt.msg_struct, body))
1352+
self.unit_lookup[chr(elements[1])] = null_term(elements[2])
13521353

13531354
# Parse the MULT messages
1354-
mtype = self.name_to_id['MULT']
1355-
fmt = self.formats[mtype]
1356-
mlen = fmt.len
1357-
for ofs in offsets[mtype]:
1358-
body = data[ofs+3:ofs+mlen]
1359-
if len(body)+3 < mlen:
1360-
break
1361-
elements = list(struct.unpack(fmt.msg_struct, body))
1362-
# Even though the multiplier value is logged as a double, the
1363-
# values in log files look to be single-precision values that have
1364-
# been cast to a double.
1365-
# To ensure that the values saved here can be used to index the
1366-
# MULT_TO_PREFIX table, we round them to 7 significant decimal digits
1367-
mult = float("%.7g" % (elements[2]))
1368-
self.mult_lookup[chr(elements[1])] = mult
1355+
if 'MULT' in self.name_to_id:
1356+
mtype = self.name_to_id['MULT']
1357+
fmt = self.formats[mtype]
1358+
mlen = fmt.len
1359+
for ofs in offsets[mtype]:
1360+
body = data[ofs+3:ofs+mlen]
1361+
if len(body)+3 < mlen:
1362+
break
1363+
elements = list(struct.unpack(fmt.msg_struct, body))
1364+
# Even though the multiplier value is logged as a double, the
1365+
# values in log files look to be single-precision values that have
1366+
# been cast to a double.
1367+
# To ensure that the values saved here can be used to index the
1368+
# MULT_TO_PREFIX table, we round them to 7 significant decimal digits
1369+
mult = float("%.7g" % (elements[2]))
1370+
self.mult_lookup[chr(elements[1])] = mult
13691371

13701372
# Parse the FMTU messages
1371-
mtype = self.name_to_id['FMTU']
1372-
fmt = self.formats[mtype]
1373-
mlen = fmt.len
1374-
for ofs in offsets[mtype]:
1375-
body = data[ofs+3:ofs+mlen]
1376-
if len(body)+3 < mlen:
1377-
break
1378-
elements = list(struct.unpack(fmt.msg_struct, body))
1379-
ftype = int(elements[1])
1380-
if ftype in self.formats:
1381-
fmt2 = self.formats[ftype]
1382-
if 'UnitIds' in fmt.colhash:
1383-
fmt2.set_unit_ids(null_term(elements[fmt.colhash['UnitIds']]), self.unit_lookup)
1384-
if 'MultIds' in fmt.colhash:
1385-
fmt2.set_mult_ids(null_term(elements[fmt.colhash['MultIds']]), self.mult_lookup)
1373+
if 'FMTU' in self.name_to_id:
1374+
mtype = self.name_to_id['FMTU']
1375+
fmt = self.formats[mtype]
1376+
mlen = fmt.len
1377+
for ofs in offsets[mtype]:
1378+
body = data[ofs+3:ofs+mlen]
1379+
if len(body)+3 < mlen:
1380+
break
1381+
elements = list(struct.unpack(fmt.msg_struct, body))
1382+
ftype = int(elements[1])
1383+
if ftype in self.formats:
1384+
fmt2 = self.formats[ftype]
1385+
if 'UnitIds' in fmt.colhash:
1386+
fmt2.set_unit_ids(null_term(elements[fmt.colhash['UnitIds']]), self.unit_lookup)
1387+
if 'MultIds' in fmt.colhash:
1388+
fmt2.set_mult_ids(null_term(elements[fmt.colhash['MultIds']]), self.mult_lookup)
13861389

13871390
# Parse the first 100 messages of each type to try to build the
13881391
# messages dictionary. 100 was chosen as a reasonable heuristic to

0 commit comments

Comments
 (0)