55"""
66
77import logging
8+ from typing import Generator , TextIO , Union
89
910from can .message import Message
10- from can .listener import Listener
11- from .generic import BaseIOHandler , FileIOMessageWriter
12- from ..typechecking import AcceptedIOType
11+ from .generic import FileIOMessageWriter , MessageReader
12+ from ..typechecking import AcceptedIOType , StringPathLike
1313
1414log = logging .getLogger ("can.io.canutils" )
1515
2222CANFD_ESI = 0x02
2323
2424
25- class CanutilsLogReader (BaseIOHandler ):
25+ class CanutilsLogReader (MessageReader ):
2626 """
2727 Iterator over CAN messages from a .log Logging File (candump -L).
2828
@@ -32,30 +32,37 @@ class CanutilsLogReader(BaseIOHandler):
3232 ``(0.0) vcan0 001#8d00100100820100``
3333 """
3434
35- def __init__ (self , file : AcceptedIOType ) -> None :
35+ file : TextIO
36+
37+ def __init__ (self , file : Union [StringPathLike , TextIO ]) -> None :
3638 """
3739 :param file: a path-like object or as file-like object to read from
3840 If this is a file-like object, is has to opened in text
3941 read mode, not binary read mode.
4042 """
4143 super ().__init__ (file , mode = "r" )
4244
43- def __iter__ (self ):
45+ def __iter__ (self ) -> Generator [ Message , None , None ] :
4446 for line in self .file :
4547
4648 # skip empty lines
4749 temp = line .strip ()
4850 if not temp :
4951 continue
5052
51- timestamp , channel , frame = temp .split ()
52- timestamp = float (timestamp [1 :- 1 ])
53- canId , data = frame .split ("#" , maxsplit = 1 )
54- if channel .isdigit ():
55- channel = int (channel )
53+ channel_string : str
54+ timestamp_string , channel_string , frame = temp .split ()
55+ timestamp = float (timestamp_string [1 :- 1 ])
56+ can_id_string , data = frame .split ("#" , maxsplit = 1 )
57+
58+ channel : Union [int , str ]
59+ if channel_string .isdigit ():
60+ channel = int (channel_string )
61+ else :
62+ channel = channel_string
5663
57- isExtended = len (canId ) > 3
58- canId = int (canId , 16 )
64+ is_extended = len (can_id_string ) > 3
65+ can_id = int (can_id_string , 16 )
5966
6067 is_fd = False
6168 brs = False
@@ -69,43 +76,43 @@ def __iter__(self):
6976 data = data [2 :]
7077
7178 if data and data [0 ].lower () == "r" :
72- isRemoteFrame = True
79+ is_remote_frame = True
7380
7481 if len (data ) > 1 :
7582 dlc = int (data [1 :])
7683 else :
7784 dlc = 0
7885
79- dataBin = None
86+ data_bin = None
8087 else :
81- isRemoteFrame = False
88+ is_remote_frame = False
8289
8390 dlc = len (data ) // 2
84- dataBin = bytearray ()
91+ data_bin = bytearray ()
8592 for i in range (0 , len (data ), 2 ):
86- dataBin .append (int (data [i : (i + 2 )], 16 ))
93+ data_bin .append (int (data [i : (i + 2 )], 16 ))
8794
88- if canId & CAN_ERR_FLAG and canId & CAN_ERR_BUSERROR :
95+ if can_id & CAN_ERR_FLAG and can_id & CAN_ERR_BUSERROR :
8996 msg = Message (timestamp = timestamp , is_error_frame = True )
9097 else :
9198 msg = Message (
9299 timestamp = timestamp ,
93- arbitration_id = canId & 0x1FFFFFFF ,
94- is_extended_id = isExtended ,
95- is_remote_frame = isRemoteFrame ,
100+ arbitration_id = can_id & 0x1FFFFFFF ,
101+ is_extended_id = is_extended ,
102+ is_remote_frame = is_remote_frame ,
96103 is_fd = is_fd ,
97104 bitrate_switch = brs ,
98105 error_state_indicator = esi ,
99106 dlc = dlc ,
100- data = dataBin ,
107+ data = data_bin ,
101108 channel = channel ,
102109 )
103110 yield msg
104111
105112 self .stop ()
106113
107114
108- class CanutilsLogWriter (FileIOMessageWriter , Listener ):
115+ class CanutilsLogWriter (FileIOMessageWriter ):
109116 """Logs CAN data to an ASCII log file (.log).
110117 This class is is compatible with "candump -L".
111118
@@ -115,7 +122,10 @@ class CanutilsLogWriter(FileIOMessageWriter, Listener):
115122 """
116123
117124 def __init__ (
118- self , file : AcceptedIOType , channel : str = "vcan0" , append : bool = False
125+ self ,
126+ file : Union [StringPathLike , TextIO ],
127+ channel : str = "vcan0" ,
128+ append : bool = False ,
119129 ):
120130 """
121131 :param file: a path-like object or as file-like object to write to
0 commit comments