Skip to content

Commit 89f9f1f

Browse files
committed
Revert "explicitly set supported file formats (hardbyte#1406)"
This reverts commit 99fea55.
1 parent 705da47 commit 89f9f1f

File tree

3 files changed

+17
-32
lines changed

3 files changed

+17
-32
lines changed

can/io/logger.py

+14-20
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
from abc import ABC, abstractmethod
88
from datetime import datetime
99
import gzip
10-
from typing import Any, Optional, Callable, Type, Tuple, cast, Dict, Set
10+
from typing import Any, Optional, Callable, Type, Tuple, cast, Dict
1111

1212
from types import TracebackType
1313

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
@@ -130,9 +131,8 @@ class BaseRotatingLogger(Listener, BaseIOHandler, ABC):
130131
:class:`~logging.handlers.BaseRotatingHandler`.
131132
132133
Subclasses must set the `_writer` attribute upon initialization.
133-
"""
134134
135-
_supported_formats: Set[str] = set()
135+
"""
136136

137137
#: If this attribute is set to a callable, the :meth:`~BaseRotatingLogger.rotation_filename`
138138
#: method delegates to this callable. The parameters passed to the callable are
@@ -224,21 +224,17 @@ def _get_new_writer(self, filename: StringPathLike) -> FileIOMessageWriter:
224224
:return:
225225
An instance of a writer class.
226226
"""
227-
suffix = "".join(pathlib.Path(filename).suffixes[-2:]).lower()
228-
229-
if suffix in self._supported_formats:
230-
logger = Logger(filename, *self.writer_args, **self.writer_kwargs)
231-
if isinstance(logger, FileIOMessageWriter):
232-
return logger
233-
elif isinstance(logger, Printer) and logger.file is not None:
234-
return cast(FileIOMessageWriter, logger)
235-
236-
raise Exception(
237-
f'The log format "{suffix}" '
238-
f"is not supported by {self.__class__.__name__}. "
239-
f"{self.__class__.__name__} supports the following formats: "
240-
f"{', '.join(self._supported_formats)}"
241-
)
227+
228+
logger = Logger(filename, *self.writer_args, **self.writer_kwargs)
229+
if isinstance(logger, FileIOMessageWriter):
230+
return logger
231+
elif isinstance(logger, Printer) and logger.file is not None:
232+
return cast(FileIOMessageWriter, logger)
233+
else:
234+
raise Exception(
235+
f"The log format \"{''.join(pathlib.Path(filename).suffixes[-2:])}"
236+
f'" is not supported by {self.__class__.__name__}'
237+
)
242238

243239
def stop(self) -> None:
244240
"""Stop handling new messages.
@@ -310,8 +306,6 @@ class SizedRotatingLogger(BaseRotatingLogger):
310306
:meth:`~can.Listener.stop` is called.
311307
"""
312308

313-
_supported_formats = {".asc", ".blf", ".csv", ".log", ".txt"}
314-
315309
def __init__(
316310
self,
317311
base_filename: StringPathLike,

can/io/printer.py

+3-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import logging
66

7-
from typing import Optional, TextIO, Union, Any, cast
7+
from typing import Optional, TextIO, Union, Any
88

99
from ..message import Message
1010
from .generic import MessageWriter
@@ -40,18 +40,11 @@ def __init__(
4040
:param append: If set to `True` messages, are appended to the file,
4141
else the file is truncated
4242
"""
43-
self.write_to_file = file is not None
4443
mode = "a" if append else "w"
4544
super().__init__(file, mode=mode)
4645

4746
def on_message_received(self, msg: Message) -> None:
48-
if self.write_to_file:
49-
cast(TextIO, self.file).write(str(msg) + "\n")
47+
if self.file is not None:
48+
self.file.write(str(msg) + "\n")
5049
else:
5150
print(msg)
52-
53-
def file_size(self) -> int:
54-
"""Return an estimate of the current file size in bytes."""
55-
if self.file is not None:
56-
return self.file.tell()
57-
return 0

test/test_rotating_loggers.py

-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ def _get_instance(path, *args, **kwargs) -> can.io.BaseRotatingLogger:
1818
class SubClass(can.io.BaseRotatingLogger):
1919
"""Subclass that implements abstract methods for testing."""
2020

21-
_supported_formats = {".asc", ".blf", ".csv", ".log", ".txt"}
22-
2321
def __init__(self, *args, **kwargs) -> None:
2422
super().__init__(*args, **kwargs)
2523
self._writer = can.Printer(file=path / "__unused.txt")

0 commit comments

Comments
 (0)