Skip to content

Commit 2dda623

Browse files
committed
address review comments, cleanup tests
1 parent 2ae5100 commit 2dda623

File tree

5 files changed

+31
-24
lines changed

5 files changed

+31
-24
lines changed

can/io/generic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def __init__(self, file: can.typechecking.AcceptedIOType, mode: str = "rt") -> N
3333
self.file = cast(Optional[can.typechecking.FileLike], file)
3434
else:
3535
# file is some path-like object
36-
self.file = open(cast(can.typechecking.PathLike, file), mode)
36+
self.file = open(cast(can.typechecking.StringPathLike, file), mode)
3737

3838
# for multiple inheritance
3939
super().__init__()

can/io/logger.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,13 @@ class Logger(BaseIOHandler, Listener): # pylint: disable=abstract-method
4242

4343
@staticmethod
4444
def __new__(
45-
cls, filename: typing.Optional[can.typechecking.PathLike], *args, **kwargs
45+
cls, filename: typing.Optional[can.typechecking.StringPathLike], *args, **kwargs
4646
):
4747
"""
4848
:param filename: the filename/path of the file to write to,
4949
may be a path-like object or None to
5050
instantiate a :class:`~can.Printer`
51+
:raises ValueError: if the filename's suffix is of an unknown file type
5152
"""
5253
if filename is None:
5354
return Printer(*args, **kwargs)
@@ -64,4 +65,4 @@ def __new__(
6465
try:
6566
return lookup[suffix](filename, *args, **kwargs)
6667
except KeyError:
67-
raise ValueError(f'unknown file type "{suffix}"')
68+
raise ValueError(f'No write support for this unknown log format "{suffix}"')

can/io/player.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
from time import time, sleep
1111
import typing
1212

13-
import can # pylint: disable=unused-import
13+
if typing.TYPE_CHECKING:
14+
import can
1415

1516
from .generic import BaseIOHandler
1617
from .asc import ASCReader
@@ -49,21 +50,22 @@ class LogReader(BaseIOHandler):
4950
def __new__(cls, filename: "can.typechecking.PathLike", *args, **kwargs):
5051
"""
5152
:param filename: the filename/path of the file to read from
53+
:raises ValueError: if the filename's suffix is of an unknown file type
5254
"""
5355
suffix = pathlib.PurePath(filename).suffix
5456

55-
if suffix == ".asc":
56-
return ASCReader(filename)
57-
if suffix == ".blf":
58-
return BLFReader(filename)
59-
if suffix == ".csv":
60-
return CSVReader(filename)
61-
if suffix == ".db":
62-
return SqliteReader(filename, *args, **kwargs)
63-
if suffix == ".log":
64-
return CanutilsLogReader(filename)
65-
66-
raise NotImplementedError(f"No read support for this log format: {filename}")
57+
lookup = {
58+
".asc": ASCReader,
59+
".blf": BLFReader,
60+
".csv": CSVReader,
61+
".db": SqliteReader,
62+
".log": CanutilsLogReader,
63+
}
64+
suffix = pathlib.PurePath(filename).suffix
65+
try:
66+
return lookup[suffix](filename, *args, **kwargs)
67+
except KeyError:
68+
raise ValueError(f'No read support for this unknown log format "{suffix}"')
6769

6870

6971
class MessageSync: # pylint: disable=too-few-public-methods
@@ -81,7 +83,8 @@ def __init__(
8183
"""Creates an new **MessageSync** instance.
8284
8385
:param messages: An iterable of :class:`can.Message` instances.
84-
:param timestamps: Use the messages' timestamps. If False, uses the *gap* parameter as the time between messages.
86+
:param timestamps: Use the messages' timestamps. If False, uses the *gap* parameter
87+
as the time between messages.
8588
:param gap: Minimum time between sent messages in seconds
8689
:param skip: Skip periods of inactivity greater than this (in seconds).
8790
"""

can/typechecking.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
import typing
55

6-
import os # pylint: disable=unused-import
6+
if typing.TYPE_CHECKING:
7+
import os
78

89
import mypy_extensions
910

test/listener_test.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,11 @@ def test_filetype_to_instance(extension, klass):
111111
test_filetype_to_instance(".db", can.SqliteReader)
112112
test_filetype_to_instance(".log", can.CanutilsLogReader)
113113

114-
# test file extensions that are not supported
115-
with self.assertRaisesRegex(NotImplementedError, ".xyz_42"):
116-
test_filetype_to_instance(".xyz_42", can.Printer)
114+
def testPlayerTypeResolutionUnsupportedFileTypes(self):
115+
for should_fail_with in ["", ".", ".some_unknown_extention_42"]:
116+
with self.assertRaises(ValueError):
117+
with can.LogReader(should_fail_with): # make sure we close it anyways
118+
pass
117119

118120
def testLoggerTypeResolution(self):
119121
def test_filetype_to_instance(extension, klass):
@@ -138,10 +140,10 @@ def test_filetype_to_instance(extension, klass):
138140
with can.Logger(None) as logger:
139141
self.assertIsInstance(logger, can.Printer)
140142

141-
should_fail_with = ["", ".", ".some_unknown_extention_42"]
142-
for supposed_fail in should_fail_with:
143+
def testLoggerTypeResolutionUnsupportedFileTypes(self):
144+
for should_fail_with in ["", ".", ".some_unknown_extention_42"]:
143145
with self.assertRaises(ValueError):
144-
with can.Logger(supposed_fail): # make sure we close it anyways
146+
with can.Logger(should_fail_with): # make sure we close it anyways
145147
pass
146148

147149
def testBufferedListenerReceives(self):

0 commit comments

Comments
 (0)