-
Notifications
You must be signed in to change notification settings - Fork 605
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
can.io: Add preferred mode for opening files to Reader/Writer classes. #1585
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_MODE Previously, 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.GzipFile Then 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
@@ -105,5 +108,21 @@ def file_size(self) -> int: | |||
return self.file.tell() | |||
|
|||
|
|||
class TextIOMessageWriter(FileIOMessageWriter, metaclass=ABCMeta): | |||
file: TextIO | gzip.GzipFile |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GzipFile returns a TextIOWrapper when opened in text mode, so this should be TextIO
only
eea1e8d
to
e083462
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀 Thank you for your contribution
Hi :)
In order to be able to enhance the
Logger
facility 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 byLogger
andLogReader
when 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!