Skip to content

Commit 73663b6

Browse files
Raise appropriate error message when append is not possible (#1361)
* Raise appropriate error messages when SqliteWriter is requested to roll - Handle "append" by adding **options to SqlWriter - Check logger instance for SqliteWriter in rotating logger setup and raise appropriate error. * Change **options to **kwargs in can/io/sqlite.py Co-authored-by: zariiii9003 <[email protected]> * Better error handling for Sqlite rollover and append option * Add type to **kwargs in asc * Raise type error rather than exception so tests pass * Changes to kwargs lookup and rolling log unavailability * Updates based on black and mypy feedback * Reformat exception by black * Handle multiple suffixes in exception * Replace ValueError with TypeError * Update append test to handle ValueError Co-authored-by: zariiii9003 <[email protected]>
1 parent 616b00e commit 73663b6

File tree

4 files changed

+19
-5
lines changed

4 files changed

+19
-5
lines changed

can/io/asc.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ def __init__(
352352
self,
353353
file: Union[StringPathLike, TextIO],
354354
channel: int = 1,
355+
**kwargs: Any,
355356
) -> None:
356357
"""
357358
:param file: a path-like object or as file-like object to write to
@@ -360,6 +361,11 @@ def __init__(
360361
:param channel: a default channel to use when the message does not
361362
have a channel set
362363
"""
364+
if kwargs.get("append", False):
365+
raise ValueError(
366+
f"{self.__class__.__name__} is currently not equipped to "
367+
f"append messages to an existing file."
368+
)
363369
super().__init__(file, mode="w")
364370

365371
self.channel = channel

can/io/logger.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from typing_extensions import Literal
1515
from pkg_resources import iter_entry_points
1616

17+
import can.io
1718
from ..message import Message
1819
from ..listener import Listener
1920
from .generic import BaseIOHandler, FileIOMessageWriter, MessageWriter
@@ -227,8 +228,8 @@ def _get_new_writer(self, filename: StringPathLike) -> FileIOMessageWriter:
227228
return cast(FileIOMessageWriter, logger)
228229
else:
229230
raise Exception(
230-
"The Logger corresponding to the arguments is not a FileIOMessageWriter or "
231-
"can.Printer"
231+
f"The log format \"{''.join(pathlib.Path(filename).suffixes[-2:])}"
232+
f'" is not supported by {self.__class__.__name__}'
232233
)
233234

234235
def stop(self) -> None:

can/io/sqlite.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import threading
99
import logging
1010
import sqlite3
11-
from typing import Generator
11+
from typing import Generator, Any
1212

1313
from can.listener import BufferedReader
1414
from can.message import Message
@@ -128,7 +128,9 @@ class SqliteWriter(MessageWriter, BufferedReader):
128128
MAX_BUFFER_SIZE_BEFORE_WRITES = 500
129129
"""Maximum number of messages to buffer before writing to the database"""
130130

131-
def __init__(self, file: StringPathLike, table_name: str = "messages") -> None:
131+
def __init__(
132+
self, file: StringPathLike, table_name: str = "messages", **kwargs: Any
133+
) -> None:
132134
"""
133135
:param file: a `str` or path like object that points
134136
to the database file to use
@@ -137,6 +139,11 @@ def __init__(self, file: StringPathLike, table_name: str = "messages") -> None:
137139
.. warning:: In contrary to all other readers/writers the Sqlite handlers
138140
do not accept file-like objects as the `file` parameter.
139141
"""
142+
if kwargs.get("append", False):
143+
raise ValueError(
144+
f"The append argument should not be used in "
145+
f"conjunction with the {self.__class__.__name__}."
146+
)
140147
super().__init__(file=None)
141148
self.table_name = table_name
142149
self._db_filename = file

test/logformats_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ def test_append_mode(self):
311311
# use append mode for second half
312312
try:
313313
writer = self.writer_constructor(self.test_file_name, append=True)
314-
except TypeError as e:
314+
except ValueError as e:
315315
# maybe "append" is not a formal parameter (this is the case for SqliteWriter)
316316
try:
317317
writer = self.writer_constructor(self.test_file_name)

0 commit comments

Comments
 (0)