can.io: Add preferred mode for opening files to Reader/Writer classes.#1585
can.io: Add preferred mode for opening files to Reader/Writer classes.#1585zariiii9003 merged 1 commit intohardbyte:developfrom
Conversation
|
Could you explain, which problem you are trying to solve with the |
|
Sure! I wanted to create own subclasses of if kwargs.get("append", False):
- mode = "ab" if real_suffix == ".blf" else "at"
+ mode = LoggerType.PREFERRED_MODE_APPEND
else:
- mode = "wb" if real_suffix == ".blf" else "wt"
+ mode = LoggerType.PREFERRED_MODEPreviously, it was hardcoded that only files for the |
|
I understand now. But we just need to know, whether the log format is binary or not, right? What do you think about adding these new types to class TextIOMessageWriter(FileIOMessageWriter, metaclass=ABCMeta):
file: typing.TextIO
class BinaryIOMessageWriter(FileIOMessageWriter, metaclass=ABCMeta):
file: typing.BinaryIO | gzip.GzipFileThen the gzip-mode could just be dependent on a subclass check: if issubclass(logger_type, BinaryIOMessageWriter):
mode = "ab" if append else "wb"
else:
mode = "at" if append else "wt" |
|
Thanks for the feedback! I adapted the code (and also considered the *Reader class). Did you have something like that in mind? |
can/io/generic.py
Outdated
|
|
||
|
|
||
| class TextIOMessageWriter(FileIOMessageWriter, metaclass=ABCMeta): | ||
| file: TextIO | gzip.GzipFile |
There was a problem hiding this comment.
GzipFile returns a TextIOWrapper when opened in text mode, so this should be TextIO only
eea1e8d to
e083462
Compare
zariiii9003
left a comment
There was a problem hiding this comment.
🚀 Thank you for your contribution
Hi :)
In order to be able to enhance the
Loggerfacility with custom classes, we need a way to handle the preferred file opening modes of those classes. I enhanced the current log writer and reader classes so that each class can specify an own preferred file opening mode which is respected byLoggerandLogReaderwhen handling gzip-compressed files.Additionally, I fixed a small typo leading to several test failures.
Please review and thank you very much for your work!