python-can-csscan-txt 2026.11.20__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- python_can_csscan_txt-2026.11.20/.gitignore +7 -0
- python_can_csscan_txt-2026.11.20/LICENSE +21 -0
- python_can_csscan_txt-2026.11.20/PKG-INFO +44 -0
- python_can_csscan_txt-2026.11.20/README.md +26 -0
- python_can_csscan_txt-2026.11.20/pyproject.toml +42 -0
- python_can_csscan_txt-2026.11.20/src/python_can_csscan_txt/__init__.py +149 -0
- python_can_csscan_txt-2026.11.20/tests/clx000.py +78 -0
- python_can_csscan_txt-2026.11.20/tests/test_txt.py +158 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 CSS-Electronics
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: python-can-csscan-txt
|
|
3
|
+
Version: 2026.11.20
|
|
4
|
+
Summary: Read TXT log files from the CSS Electronics CLX000 CAN loggers through python-can
|
|
5
|
+
Project-URL: Homepage, https://csselectronics.com/
|
|
6
|
+
Project-URL: Source, https://github.com/CSS-Electronics/python-can-csscan-txt
|
|
7
|
+
Author: CSS Electronics
|
|
8
|
+
Author-email: contact@csselectronics.com
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Requires-Python: >=3.12
|
|
14
|
+
Requires-Dist: python-can>=4.6.0
|
|
15
|
+
Provides-Extra: test
|
|
16
|
+
Requires-Dist: pytest>=8.0; extra == 'test'
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
|
|
19
|
+
# python-can-csscan-txt
|
|
20
|
+
|
|
21
|
+
Read TXT log files from the [CSS Electronics](https://www.csselectronics.com/) CLX000 CAN loggers through [python-can](https://python-can.readthedocs.io/).
|
|
22
|
+
|
|
23
|
+
```
|
|
24
|
+
pip install python-can-csscan-txt
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
```python
|
|
28
|
+
import can
|
|
29
|
+
|
|
30
|
+
with can.LogReader("00000001.TXT") as reader:
|
|
31
|
+
for msg in reader:
|
|
32
|
+
print(msg)
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
The logger information from the file header is available through `LogFile`:
|
|
36
|
+
|
|
37
|
+
```python
|
|
38
|
+
from python_can_csscan_txt import LogFile
|
|
39
|
+
|
|
40
|
+
with LogFile("00000001.TXT") as reader:
|
|
41
|
+
print(reader.get_metadata()) # id, hardware/firmware revision, session, split, bitrate
|
|
42
|
+
for msg in reader:
|
|
43
|
+
print(msg)
|
|
44
|
+
```
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# python-can-csscan-txt
|
|
2
|
+
|
|
3
|
+
Read TXT log files from the [CSS Electronics](https://www.csselectronics.com/) CLX000 CAN loggers through [python-can](https://python-can.readthedocs.io/).
|
|
4
|
+
|
|
5
|
+
```
|
|
6
|
+
pip install python-can-csscan-txt
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
```python
|
|
10
|
+
import can
|
|
11
|
+
|
|
12
|
+
with can.LogReader("00000001.TXT") as reader:
|
|
13
|
+
for msg in reader:
|
|
14
|
+
print(msg)
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
The logger information from the file header is available through `LogFile`:
|
|
18
|
+
|
|
19
|
+
```python
|
|
20
|
+
from python_can_csscan_txt import LogFile
|
|
21
|
+
|
|
22
|
+
with LogFile("00000001.TXT") as reader:
|
|
23
|
+
print(reader.get_metadata()) # id, hardware/firmware revision, session, split, bitrate
|
|
24
|
+
for msg in reader:
|
|
25
|
+
print(msg)
|
|
26
|
+
```
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling >= 1.27"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "python-can-csscan-txt"
|
|
7
|
+
version = "2026.11.20"
|
|
8
|
+
authors = [
|
|
9
|
+
{ name="CSS Electronics" },
|
|
10
|
+
{ email="contact@csselectronics.com" },
|
|
11
|
+
]
|
|
12
|
+
description = "Read TXT log files from the CSS Electronics CLX000 CAN loggers through python-can"
|
|
13
|
+
license = "MIT"
|
|
14
|
+
readme = "README.md"
|
|
15
|
+
requires-python = ">=3.12"
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Programming Language :: Python :: 3",
|
|
18
|
+
"Operating System :: OS Independent",
|
|
19
|
+
]
|
|
20
|
+
dependencies = [
|
|
21
|
+
'python-can >= 4.6.0',
|
|
22
|
+
]
|
|
23
|
+
|
|
24
|
+
[project.optional-dependencies]
|
|
25
|
+
test = [
|
|
26
|
+
'pytest >= 8.0',
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
[project.entry-points."can.io.message_reader"]
|
|
30
|
+
".txt" = "python_can_csscan_txt:LogFile"
|
|
31
|
+
|
|
32
|
+
[project.urls]
|
|
33
|
+
"Homepage" = "https://csselectronics.com/"
|
|
34
|
+
"Source" = "https://github.com/CSS-Electronics/python-can-csscan-txt"
|
|
35
|
+
|
|
36
|
+
[tool.pytest.ini_options]
|
|
37
|
+
testpaths = [
|
|
38
|
+
"tests",
|
|
39
|
+
]
|
|
40
|
+
filterwarnings = [
|
|
41
|
+
"error",
|
|
42
|
+
]
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
"""Read TXT log files from the CSS Electronics CLX000 (CL1000/CL2000/CL3000) through python-can."""
|
|
2
|
+
|
|
3
|
+
import calendar
|
|
4
|
+
import io
|
|
5
|
+
import logging
|
|
6
|
+
import os
|
|
7
|
+
from collections.abc import Generator
|
|
8
|
+
from typing import Any, TextIO
|
|
9
|
+
|
|
10
|
+
from can import Message
|
|
11
|
+
from can.io.generic import TextIOMessageReader
|
|
12
|
+
from can.typechecking import StringPathLike
|
|
13
|
+
|
|
14
|
+
__all__ = ["LogFile"]
|
|
15
|
+
|
|
16
|
+
logger = logging.getLogger(__name__)
|
|
17
|
+
|
|
18
|
+
_COLUMNS = {"Timestamp", "Lost", "Type", "ID", "Length", "Data"}
|
|
19
|
+
|
|
20
|
+
# Seconds after which the timestamp wraps around, for time formats 0 (kkk), 1 (sskkk), 2 (mmsskkk) and 3 (hhmmsskkk).
|
|
21
|
+
# Format 4 (DDhhmmsskkk) wraps every month and format 5 (MMDDhhmmsskkk) every year. Format 6 holds the full date.
|
|
22
|
+
_WRAP_SECONDS = {0: 1, 1: 60, 2: 60 * 60, 3: 24 * 60 * 60}
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class LogFile(TextIOMessageReader):
|
|
26
|
+
"""Iterate over the CAN messages of a CLX000 TXT log file.
|
|
27
|
+
|
|
28
|
+
:param file: Path to a log file, or an open file.
|
|
29
|
+
:param kwargs: Ignored. Accepted for compatibility with ``can.LogReader``.
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
def __init__(self, file: StringPathLike | TextIO | io.BufferedIOBase, **kwargs: Any) -> None:
|
|
33
|
+
if isinstance(file, (io.RawIOBase, io.BufferedIOBase)):
|
|
34
|
+
file = io.TextIOWrapper(file, encoding="latin-1")
|
|
35
|
+
super().__init__(file, mode="r", encoding="latin-1")
|
|
36
|
+
|
|
37
|
+
try:
|
|
38
|
+
self._read_header()
|
|
39
|
+
except Exception:
|
|
40
|
+
if isinstance(file, (str, os.PathLike)): # Only close the file if opened here
|
|
41
|
+
self.file.close()
|
|
42
|
+
raise
|
|
43
|
+
|
|
44
|
+
def _read_header(self) -> None:
|
|
45
|
+
# Header: lines starting with "#", formatted as "# Key: value"
|
|
46
|
+
self._header: dict[str, str] = {}
|
|
47
|
+
line = self.file.readline()
|
|
48
|
+
while line.startswith("#"):
|
|
49
|
+
key, _, value = line.lstrip("#").strip().partition(": ")
|
|
50
|
+
self._header[key] = value
|
|
51
|
+
line = self.file.readline()
|
|
52
|
+
|
|
53
|
+
# Column header follows the header, e.g. "Timestamp;Type;ID;Data"
|
|
54
|
+
self._separator = self._quoted("Value separator")
|
|
55
|
+
self._columns = self._split(line.rstrip("\r\n"))
|
|
56
|
+
if "Logger type" not in self._header or not self._separator or not set(self._columns) <= _COLUMNS:
|
|
57
|
+
raise ValueError("Not a CLX000 TXT log file")
|
|
58
|
+
|
|
59
|
+
self._time_format = int(_digits(self._header.get("Time format", "")) or 0)
|
|
60
|
+
|
|
61
|
+
# Start time from the header ("YYYYMMDDThhmmss"). Timestamps in the log only hold the least significant part of
|
|
62
|
+
# the time (depending on the time format), the remaining digits are taken from the start time.
|
|
63
|
+
start = _digits(self._header.get("Time", "")) # "YYYYMMDDhhmmss"
|
|
64
|
+
self._time_template = start + "19700101000000000"[len(start) :] # "YYYYMMDDhhmmsskkk"
|
|
65
|
+
|
|
66
|
+
def get_metadata(self) -> dict[str, Any]:
|
|
67
|
+
"""Return the logger information stored in the file header."""
|
|
68
|
+
return {
|
|
69
|
+
"id": self._header.get("Logger ID", ""),
|
|
70
|
+
"hardware revision": self._header.get("HW rev", ""),
|
|
71
|
+
"firmware revision": self._header.get("FW rev", ""),
|
|
72
|
+
"session": int(self._header.get("Session No.", "0") or 0),
|
|
73
|
+
"split": int(self._header.get("Split No.", "0") or 0),
|
|
74
|
+
"bitrate": int(self._header.get("Bit-rate", "0") or 0),
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
def __iter__(self) -> Generator[Message, None, None]:
|
|
78
|
+
wraps = 0
|
|
79
|
+
parsed_previous = self._parse_timestamp("", 0) # The header start time
|
|
80
|
+
emitted_previous = 0.0
|
|
81
|
+
|
|
82
|
+
for line in self.file:
|
|
83
|
+
line = line.rstrip("\r\n")
|
|
84
|
+
fields = self._split(line)
|
|
85
|
+
if len(fields) != len(self._columns):
|
|
86
|
+
logger.warning("Skipping invalid line: %r", line) # E.g. cut short by a power loss
|
|
87
|
+
continue
|
|
88
|
+
row = dict(zip(self._columns, fields))
|
|
89
|
+
|
|
90
|
+
try:
|
|
91
|
+
timestamp = 0.0
|
|
92
|
+
if "Timestamp" in row:
|
|
93
|
+
timestamp = self._parse_timestamp(row["Timestamp"], wraps)
|
|
94
|
+
# A step back in time means the timestamp wrapped around. The CLX000 occasionally logs a timestamp
|
|
95
|
+
# up to one second too late, so a step back of up to one second is not a wrap. Format 0 does not
|
|
96
|
+
# show seconds, so there any step back is a wrap.
|
|
97
|
+
tolerance = 0 if self._time_format == 0 else 1
|
|
98
|
+
if timestamp < parsed_previous - tolerance and self._time_format in range(6):
|
|
99
|
+
wraps += 1
|
|
100
|
+
timestamp = self._parse_timestamp(row["Timestamp"], wraps)
|
|
101
|
+
parsed_previous = timestamp
|
|
102
|
+
|
|
103
|
+
msg_type = int(row.get("Type") or 0)
|
|
104
|
+
message = Message(
|
|
105
|
+
timestamp=timestamp,
|
|
106
|
+
arbitration_id=int(row.get("ID") or "0", 16),
|
|
107
|
+
is_extended_id=bool(msg_type & 0x1),
|
|
108
|
+
is_rx=not msg_type & 0x8,
|
|
109
|
+
data=bytes.fromhex(row.get("Data", "")),
|
|
110
|
+
)
|
|
111
|
+
except ValueError:
|
|
112
|
+
logger.warning("Skipping invalid line: %r", line)
|
|
113
|
+
continue
|
|
114
|
+
|
|
115
|
+
# After a timestamp that was logged too late, skip the following messages until time has caught up
|
|
116
|
+
if timestamp < emitted_previous:
|
|
117
|
+
logger.warning("Skipping non-increasing timestamp: %f", timestamp)
|
|
118
|
+
continue
|
|
119
|
+
emitted_previous = timestamp
|
|
120
|
+
|
|
121
|
+
yield message
|
|
122
|
+
|
|
123
|
+
def _quoted(self, key: str) -> str:
|
|
124
|
+
"""Header separator values are quoted, e.g. '";"'. Empty when not set."""
|
|
125
|
+
value = self._header.get(key, "")
|
|
126
|
+
return value[1] if len(value) == 3 else ""
|
|
127
|
+
|
|
128
|
+
def _split(self, line: str) -> list[str]:
|
|
129
|
+
return line.split(self._separator)
|
|
130
|
+
|
|
131
|
+
def _parse_timestamp(self, text: str, wraps: int) -> float:
|
|
132
|
+
# The log only holds the least significant digits of the time, the rest are taken from the header start time
|
|
133
|
+
digits = _digits(text)
|
|
134
|
+
full = self._time_template[: len(self._time_template) - len(digits)] + digits
|
|
135
|
+
year = int(full[0:4])
|
|
136
|
+
month, day, hour, minute, second = (int(full[i : i + 2]) for i in (4, 6, 8, 10, 12))
|
|
137
|
+
|
|
138
|
+
if self._time_format == 4:
|
|
139
|
+
year, month = year + (month - 1 + wraps) // 12, (month - 1 + wraps) % 12 + 1
|
|
140
|
+
elif self._time_format == 5:
|
|
141
|
+
year += wraps
|
|
142
|
+
|
|
143
|
+
# timegm accepts days beyond the end of the month (e.g. 31 February), rolling over into the next month
|
|
144
|
+
seconds = calendar.timegm((year, month, day, hour, minute, second))
|
|
145
|
+
return seconds + _WRAP_SECONDS.get(self._time_format, 0) * wraps + int(full[14:17]) / 1000
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def _digits(text: str) -> str:
|
|
149
|
+
return "".join(c for c in text if c.isdigit())
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"""Write CLX000 TXT log files the way the device firmware does (port of cCsvLog.c / cTime.c), for use in tests."""
|
|
2
|
+
|
|
3
|
+
from collections.abc import Iterable
|
|
4
|
+
from datetime import datetime, timezone
|
|
5
|
+
|
|
6
|
+
import can
|
|
7
|
+
|
|
8
|
+
ALL_COLUMNS = ("Timestamp", "Lost", "Type", "ID", "Length", "Data")
|
|
9
|
+
DEFAULT_COLUMNS = ("Timestamp", "Type", "ID", "Data") # Default of LOG_dataFields_config (LOG_logger.c)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def write_log(
|
|
13
|
+
messages: Iterable[can.Message],
|
|
14
|
+
*,
|
|
15
|
+
start: float,
|
|
16
|
+
time_format: int = 4,
|
|
17
|
+
value_sep: str = ";",
|
|
18
|
+
time_sep: str = "",
|
|
19
|
+
ms_sep: str = "",
|
|
20
|
+
date_sep: str = "",
|
|
21
|
+
date_time_sep: str = "T",
|
|
22
|
+
columns: Iterable[str] = DEFAULT_COLUMNS,
|
|
23
|
+
logger_id: str = "00AABBCC",
|
|
24
|
+
session: int = 1,
|
|
25
|
+
split: int = 1,
|
|
26
|
+
bitrate: int = 500000,
|
|
27
|
+
) -> str:
|
|
28
|
+
"""Return the content of a log file with the given messages. Defaults match the firmware defaults."""
|
|
29
|
+
columns = [c for c in ALL_COLUMNS if c in columns] # Fixed order, as in the firmware
|
|
30
|
+
|
|
31
|
+
lines = [
|
|
32
|
+
"# Logger type: CANLogger2000",
|
|
33
|
+
"# HW rev: 00.04",
|
|
34
|
+
"# FW rev: 05.41",
|
|
35
|
+
f"# Logger ID: {logger_id}",
|
|
36
|
+
f"# Session No.: {session}",
|
|
37
|
+
f"# Split No.: {split}",
|
|
38
|
+
f"# Time: {datetime.fromtimestamp(int(start), timezone.utc):%Y%m%dT%H%M%S}",
|
|
39
|
+
f'# Value separator: "{value_sep}"',
|
|
40
|
+
f"# Time format: {time_format}",
|
|
41
|
+
f'# Time separator: "{time_sep}"',
|
|
42
|
+
f'# Time separator ms: "{ms_sep}"',
|
|
43
|
+
f'# Date separator: "{date_sep}"',
|
|
44
|
+
f'# Time and date separator: "{date_time_sep}"',
|
|
45
|
+
f"# Bit-rate: {bitrate}",
|
|
46
|
+
"# Silent mode: false",
|
|
47
|
+
"# Cyclic mode: false",
|
|
48
|
+
value_sep.join(columns),
|
|
49
|
+
]
|
|
50
|
+
|
|
51
|
+
for msg in messages:
|
|
52
|
+
seconds, ms = divmod(round(msg.timestamp * 1000), 1000)
|
|
53
|
+
t = datetime.fromtimestamp(seconds, timezone.utc)
|
|
54
|
+
|
|
55
|
+
# Time fields with the separator following each field, most significant first. Time format N includes the
|
|
56
|
+
# N + 1 least significant fields.
|
|
57
|
+
fields = [
|
|
58
|
+
f"{t.year:04}{date_sep}",
|
|
59
|
+
f"{t.month:02}{date_sep}",
|
|
60
|
+
f"{t.day:02}{date_time_sep}",
|
|
61
|
+
f"{t.hour:02}{time_sep}",
|
|
62
|
+
f"{t.minute:02}{time_sep}",
|
|
63
|
+
f"{t.second:02}{ms_sep}",
|
|
64
|
+
f"{ms:03}",
|
|
65
|
+
]
|
|
66
|
+
msg_type = (1 if msg.is_extended_id else 0) | (0 if msg.is_rx else 8)
|
|
67
|
+
|
|
68
|
+
values = {
|
|
69
|
+
"Timestamp": "".join(fields[6 - time_format :]),
|
|
70
|
+
"Lost": "0",
|
|
71
|
+
"Type": str(msg_type),
|
|
72
|
+
"ID": f"{msg.arbitration_id:x}",
|
|
73
|
+
"Length": f"{len(msg.data):x}",
|
|
74
|
+
"Data": msg.data.hex(),
|
|
75
|
+
}
|
|
76
|
+
lines.append(value_sep.join(values[c] for c in columns))
|
|
77
|
+
|
|
78
|
+
return "\n".join(lines) + "\n"
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import io
|
|
2
|
+
import random
|
|
3
|
+
from datetime import datetime, timezone
|
|
4
|
+
|
|
5
|
+
import can
|
|
6
|
+
import pytest
|
|
7
|
+
from clx000 import ALL_COLUMNS, write_log
|
|
8
|
+
|
|
9
|
+
from python_can_csscan_txt import LogFile
|
|
10
|
+
|
|
11
|
+
START = datetime(2023, 12, 30, 23, 59, 50, tzinfo=timezone.utc).timestamp()
|
|
12
|
+
|
|
13
|
+
# Step between messages per time format. Steps must be less than the wrap period minus one second, to tell a wrap apart
|
|
14
|
+
# from a timestamp logged too late (format 0 does not show seconds, so there steps must be less than one second).
|
|
15
|
+
MAX_STEP = {0: 0.9, 1: 50, 2: 3000, 3: 80000, 4: 20 * 86400, 5: 200 * 86400, 6: 1000 * 86400}
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def random_messages(count: int, max_step: float, seed: int = 0) -> list[can.Message]:
|
|
19
|
+
rng = random.Random(seed)
|
|
20
|
+
t_ms = int(START * 1000)
|
|
21
|
+
messages = []
|
|
22
|
+
for _ in range(count):
|
|
23
|
+
t_ms += rng.randint(0, int(max_step * 1000)) # The first message also comes some time after the header time
|
|
24
|
+
extended = rng.random() < 0.5
|
|
25
|
+
messages.append(
|
|
26
|
+
can.Message(
|
|
27
|
+
timestamp=t_ms / 1000,
|
|
28
|
+
arbitration_id=rng.randint(0, 0x1FFFFFFF if extended else 0x7FF),
|
|
29
|
+
is_extended_id=extended,
|
|
30
|
+
is_rx=rng.random() < 0.5,
|
|
31
|
+
data=rng.randbytes(rng.randint(0, 8)),
|
|
32
|
+
)
|
|
33
|
+
)
|
|
34
|
+
return messages
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def read(text: str) -> list[can.Message]:
|
|
38
|
+
return list(LogFile(io.StringIO(text)))
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def assert_equal(actual: list[can.Message], expected: list[can.Message]) -> None:
|
|
42
|
+
assert len(actual) == len(expected)
|
|
43
|
+
for a, e in zip(actual, expected):
|
|
44
|
+
assert a.equals(e, timestamp_delta=1e-6), f"{a} != {e}"
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
@pytest.mark.parametrize("time_format", [0, 1, 2, 3, 4, 5, 6])
|
|
48
|
+
def test_time_formats(time_format):
|
|
49
|
+
# Timestamps only hold the least significant part of the time and wrap around (e.g. every minute for format 1)
|
|
50
|
+
messages = random_messages(500, MAX_STEP[time_format], seed=time_format)
|
|
51
|
+
assert_equal(read(write_log(messages, start=START, time_format=time_format)), messages)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
@pytest.mark.parametrize("time_format", [1, 2, 3])
|
|
55
|
+
def test_wrap_before_first_message(time_format):
|
|
56
|
+
# The header holds the time the file was opened. The timestamp can wrap before the first message. Not for format 0:
|
|
57
|
+
# the header time has no milliseconds, so a wrap within the first second after it cannot be detected.
|
|
58
|
+
start = datetime(2023, 12, 31, 23, 59, 59, 500000, tzinfo=timezone.utc).timestamp()
|
|
59
|
+
messages = [can.Message(timestamp=start + 0.7, arbitration_id=1), can.Message(timestamp=start + 0.8, arbitration_id=2)]
|
|
60
|
+
assert_equal(read(write_log(messages, start=start, time_format=time_format)), messages)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
@pytest.mark.parametrize("time_format", [4, 5])
|
|
64
|
+
def test_wrap_leap_year(time_format):
|
|
65
|
+
# Wraps of a month (format 4) or a year (format 5) do not have a fixed length
|
|
66
|
+
start = datetime(2023, 12, 1, tzinfo=timezone.utc).timestamp()
|
|
67
|
+
if time_format == 4: # Steps of less than a month
|
|
68
|
+
times = [(2023, 12, 1), (2023, 12, 20), (2024, 1, 10), (2024, 2, 1), (2024, 2, 29), (2024, 3, 1), (2024, 3, 31)]
|
|
69
|
+
else: # Steps of less than a year
|
|
70
|
+
times = [(2023, 12, 1), (2024, 2, 1), (2024, 2, 29), (2024, 3, 1), (2024, 12, 31), (2025, 3, 1)]
|
|
71
|
+
messages = [
|
|
72
|
+
can.Message(timestamp=datetime(*t, 12, tzinfo=timezone.utc).timestamp(), arbitration_id=i)
|
|
73
|
+
for i, t in enumerate(times)
|
|
74
|
+
]
|
|
75
|
+
assert_equal(read(write_log(messages, start=start, time_format=time_format)), messages)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
@pytest.mark.parametrize(
|
|
79
|
+
"separators",
|
|
80
|
+
[
|
|
81
|
+
dict(), # Firmware default
|
|
82
|
+
dict(value_sep=",", time_sep=":", ms_sep=".", date_sep="-", date_time_sep=" "),
|
|
83
|
+
dict(value_sep=";", time_sep=".", ms_sep=",", date_sep="/", date_time_sep=" "),
|
|
84
|
+
dict(value_sep=" ", time_sep=":", ms_sep=".", date_sep="/", date_time_sep="T"),
|
|
85
|
+
dict(value_sep="|", date_time_sep=""),
|
|
86
|
+
],
|
|
87
|
+
)
|
|
88
|
+
def test_separators(separators):
|
|
89
|
+
messages = random_messages(100, 10)
|
|
90
|
+
assert_equal(read(write_log(messages, start=START, time_format=6, **separators)), messages)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def test_all_columns():
|
|
94
|
+
messages = random_messages(100, 10)
|
|
95
|
+
assert_equal(read(write_log(messages, start=START, columns=ALL_COLUMNS)), messages)
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def test_without_timestamp():
|
|
99
|
+
messages = random_messages(10, 10)
|
|
100
|
+
for msg in messages:
|
|
101
|
+
msg.timestamp = 0.0
|
|
102
|
+
assert_equal(read(write_log(messages, start=START, columns=("Type", "ID", "Data"))), messages)
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def test_timestamp_glitch():
|
|
106
|
+
# The CLX000 occasionally logs a timestamp up to one second too late. It is kept, following earlier messages are
|
|
107
|
+
# skipped and not mistaken for a wrap.
|
|
108
|
+
messages = random_messages(5, 0.1)
|
|
109
|
+
messages[1].timestamp += 1
|
|
110
|
+
expected = [messages[0], messages[1]]
|
|
111
|
+
assert_equal(read(write_log(messages, start=START, time_format=1)), expected)
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def test_truncated_last_line():
|
|
115
|
+
# Power loss while writing can cut the last line anywhere. Complete messages are read, the cut one is either
|
|
116
|
+
# skipped or read with partial data (undetectable when cut between two data bytes).
|
|
117
|
+
messages = [can.Message(timestamp=START + i, arbitration_id=i, data=bytes(range(1, 9))) for i in range(5)]
|
|
118
|
+
text = write_log(messages, start=START, columns=ALL_COLUMNS)
|
|
119
|
+
last_line = text.splitlines()[-1]
|
|
120
|
+
for cut in range(1, len(last_line)):
|
|
121
|
+
actual = read(text[: len(text) - len(last_line) - 1 + cut])
|
|
122
|
+
assert_equal(actual[:4], messages[:4])
|
|
123
|
+
assert len(actual) == 4 or actual[4].arbitration_id == messages[4].arbitration_id
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def test_metadata():
|
|
127
|
+
reader = LogFile(io.StringIO(write_log([], start=START, logger_id="ABCD1234", session=12, split=3, bitrate=250000)))
|
|
128
|
+
assert reader.get_metadata() == {
|
|
129
|
+
"id": "ABCD1234",
|
|
130
|
+
"hardware revision": "00.04",
|
|
131
|
+
"firmware revision": "05.41",
|
|
132
|
+
"session": 12,
|
|
133
|
+
"split": 3,
|
|
134
|
+
"bitrate": 250000,
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def test_log_reader(tmp_path):
|
|
139
|
+
messages = random_messages(10, 10)
|
|
140
|
+
path = tmp_path / "00000001.TXT"
|
|
141
|
+
path.write_text(write_log(messages, start=START), newline="\r\n") # The firmware writes "\n", also accept "\r\n"
|
|
142
|
+
with can.LogReader(path) as reader:
|
|
143
|
+
assert isinstance(reader, LogFile)
|
|
144
|
+
assert_equal(list(reader), messages)
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def test_binary_file():
|
|
148
|
+
messages = random_messages(10, 10)
|
|
149
|
+
with LogFile(io.BytesIO(write_log(messages, start=START).encode())) as reader:
|
|
150
|
+
assert_equal(list(reader), messages)
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
@pytest.mark.parametrize("text", ["not a log file\n", "# Some: comment\nData\n0102\n"])
|
|
154
|
+
def test_invalid_file(tmp_path, text):
|
|
155
|
+
path = tmp_path / "notes.txt"
|
|
156
|
+
path.write_text(text)
|
|
157
|
+
with pytest.raises(ValueError):
|
|
158
|
+
LogFile(path) # Must not leak the open file (warnings are errors, see pyproject.toml)
|