Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trc file writer add version param #1924

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
52 changes: 30 additions & 22 deletions can/io/trc.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,17 @@
import os
from datetime import datetime, timedelta, timezone
from enum import Enum
from typing import Any, Callable, Dict, Generator, Optional, TextIO, Tuple, Union
from typing import (
Any,
Callable,
Dict,
Generator,
Mapping,
Optional,
TextIO,
Tuple,
Union,
)

from ..message import Message
from ..typechecking import StringPathLike
Expand Down Expand Up @@ -278,15 +288,18 @@ class TRCWriter(TextIOMessageWriter):
file: TextIO
first_timestamp: Optional[float]

FORMAT_MESSAGE = (
"{msgnr:>7} {time:13.3f} DT {channel:>2} {id:>8} {dir:>2} - {dlc:<4} {data}"
)
FORMAT_MESSAGE_V1_0 = "{msgnr:>6}) {time:7.0f} {id:>8} {dlc:<1} {data}"
MESSAGE_FORMAT_MAP: Mapping[TRCFileVersion, str] = {
TRCFileVersion.V1_0: "{msgnr:>6}) {time:7.0f} {id:>8} {dlc:<1} {data}",
TRCFileVersion.V2_1: (
"{msgnr:>7} {time:13.3f} DT {channel:>2} {id:>8} {dir:>2} - {dlc:<4} {data}"
),
}

def __init__(
self,
file: Union[StringPathLike, TextIO],
channel: int = 1,
file_version: Union[int, TRCFileVersion] = TRCFileVersion.V2_1,
**kwargs: Any,
) -> None:
"""
Expand All @@ -295,6 +308,7 @@ def __init__(
write mode, not binary write mode.
:param channel: a default channel to use when the message does not
have a channel set
:param file_version: the trc file format version. Version 2.1 by default
"""
super().__init__(file, mode="w")
self.channel = channel
Expand All @@ -308,9 +322,15 @@ def __init__(
self.header_written = False
self.msgnr = 0
self.first_timestamp = None
self.file_version = TRCFileVersion.V2_1
self._msg_fmt_string = self.FORMAT_MESSAGE_V1_0
self._format_message = self._format_message_init
self._setup_file_version(file_version)

def _setup_file_version(self, file_version: Union[int, TRCFileVersion]) -> None:
try:
self.file_version = TRCFileVersion(file_version)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instance attributes should be set in __init__. You could have version and format string as return values of a static method or just remove this method and put the try/except block into __init__

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok. The branch where i picked this from handled it with a tuple returned. I think this does not improve readability.

As this method is invoked from the init function it is always ensured that the attributes are initialized. Can you tell my why it should be added explicit in init?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just a good practice, and it helps with code completion since IDEs usually look into __init__ for instance attributes.

I wonder why pylint is silent, there's a rule for this: https://pylint.readthedocs.io/en/latest/user_guide/messages/warning/attribute-defined-outside-init.html 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I googled a bit for this and it seems like the only requirement is that all attributes are already initialized after the init function was executed. It is not enforced that the assignment is inside of the init function. So for me it looks like the approach here is correct.

I prefer to try to make a function implementation fit on one or two screen page. With this implementation we are quite prepared for more additions and changes not to blow up the function.

If we return a tuple we would achieve the same that is correct, but i think it is better readable like this.

So please take another look and tell me your thoughts.

self._msg_fmt_string = self.MESSAGE_FORMAT_MAP[self.file_version]
except (KeyError, ValueError) as exc:
err_msg = f"File version is not supported: {file_version}"
raise NotImplementedError(err_msg) from exc

def _write_header_v1_0(self, start_time: datetime) -> None:
lines = [
Expand Down Expand Up @@ -359,7 +379,7 @@ def _write_header_v2_1(self, start_time: datetime) -> None:
]
self.file.writelines(line + "\n" for line in lines)

def _format_message_by_format(self, msg, channel):
def _format_message(self, msg: Message, channel: int) -> str:
if msg.is_extended_id:
arb_id = f"{msg.arbitration_id:07X}"
else:
Expand All @@ -369,7 +389,7 @@ def _format_message_by_format(self, msg, channel):

serialized = self._msg_fmt_string.format(
msgnr=self.msgnr,
time=(msg.timestamp - self.first_timestamp) * 1000,
time=(msg.timestamp - (self.first_timestamp or 0.0)) * 1000,
channel=channel,
id=arb_id,
dir="Rx" if msg.is_rx else "Tx",
Expand All @@ -378,18 +398,6 @@ def _format_message_by_format(self, msg, channel):
)
return serialized

def _format_message_init(self, msg, channel):
if self.file_version == TRCFileVersion.V1_0:
self._format_message = self._format_message_by_format
self._msg_fmt_string = self.FORMAT_MESSAGE_V1_0
elif self.file_version == TRCFileVersion.V2_1:
self._format_message = self._format_message_by_format
self._msg_fmt_string = self.FORMAT_MESSAGE
else:
raise NotImplementedError("File format is not supported")

return self._format_message_by_format(msg, channel)

def write_header(self, timestamp: float) -> None:
# write start of file header
start_time = datetime.fromtimestamp(timestamp, timezone.utc)
Expand Down
8 changes: 8 additions & 0 deletions doc/file_io.rst
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,14 @@ The following class can be used to read messages from TRC file:
:members:


The following enum can be used during creation of a TRC file with `TRCWriter` to select a specific file version:

.. autoclass:: can.TRCFileVersion
:show-inheritance:
:members:
:undoc-members:


Rotating Loggers
----------------

Expand Down
3 changes: 1 addition & 2 deletions test/logformats_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1058,8 +1058,7 @@ class TestTrcFileFormatV1_0(TestTrcFileFormatBase):

@staticmethod
def Writer(filename):
writer = can.TRCWriter(filename)
writer.file_version = can.TRCFileVersion.V1_0
writer = can.TRCWriter(filename, file_version=can.TRCFileVersion.V1_0)
return writer

def _setup_instance(self):
Expand Down
Loading