Skip to content

Commit d8e9dc5

Browse files
authored
Merge pull request #1223 from zariiii9003/fix_ascii
Improve ASCReader robustness
2 parents b66ea80 + 2bc44be commit d8e9dc5

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

can/io/asc.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
- under `test/data/logfile.asc`
77
"""
88
import gzip
9-
from typing import cast, Any, Generator, IO, List, Optional, Dict, Union, TextIO
9+
import re
10+
from typing import Any, Generator, List, Optional, Dict, Union, TextIO
1011

1112
from datetime import datetime
1213
import time
@@ -85,8 +86,8 @@ def _extract_header(self):
8586
self.timestamps_format = timestamp_format
8687
elif lower_case.endswith("internal events logged"):
8788
self.internal_events_logged = not lower_case.startswith("no")
88-
elif lower_case.startswith("// version"):
89-
# the test files include `// version 9.0.0` - not sure what this is
89+
elif lower_case.startswith("//"):
90+
# ignore comments
9091
continue
9192
# grab absolute timestamp
9293
elif lower_case.startswith("begin triggerblock"):
@@ -210,14 +211,20 @@ def __iter__(self) -> Generator[Message, None, None]:
210211
self._extract_header()
211212

212213
for line in self.file:
213-
temp = line.strip()
214-
if not temp or not temp[0].isdigit():
215-
# Could be a comment
214+
line = line.strip()
215+
216+
if not re.match(
217+
r"\d+\.\d+\s+(\d+\s+(\w+\s+(Tx|Rx)|ErrorFrame)|CANFD)",
218+
line,
219+
re.ASCII | re.IGNORECASE,
220+
):
221+
# line might be a comment, chip status,
222+
# J1939 message or some other unsupported event
216223
continue
217224

218225
msg_kwargs: Dict[str, Union[float, bool, int]] = {}
219226
try:
220-
_timestamp, channel, rest_of_message = temp.split(None, 2)
227+
_timestamp, channel, rest_of_message = line.split(None, 2)
221228
timestamp = float(_timestamp) + self.start_time
222229
msg_kwargs["timestamp"] = timestamp
223230
if channel == "CANFD":

test/data/logfile.asc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ date Sam Sep 30 15:06:13.191 2017
22
base hex timestamps absolute
33
internal events logged
44
// version 9.0.0
5+
//0.000000 previous log file: logfile_errorframes.asc
56
Begin Triggerblock Sam Sep 30 15:06:13.191 2017
67
0.000000 Start of measurement
78
0.015991 CAN 1 Status:chip status error passive - TxErr: 132 RxErr: 0

test/logformats_test.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,9 @@ def test_can_and_canfd_error_frames(self):
554554
actual = self._read_log_file("test_CanErrorFrames.asc")
555555
self.assertMessagesEqual(actual, expected_messages)
556556

557+
def test_ignore_comments(self):
558+
_msg_list = self._read_log_file("logfile.asc")
559+
557560

558561
class TestGzipASCFileFormat(ReaderWriterTest):
559562
"""Tests can.GzipASCWriter and can.GzipASCReader"""

0 commit comments

Comments
 (0)