Skip to content

Commit 5088ec2

Browse files
committed
Add unzip method to logreader
1 parent 8b6b592 commit 5088ec2

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

can/io/player.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
well as :class:`MessageSync` which plays back messages
44
in the recorded order an time intervals.
55
"""
6-
6+
import gzip
77
import pathlib
88
from time import time, sleep
99
import typing
@@ -14,7 +14,7 @@
1414
import can
1515

1616
from .generic import BaseIOHandler, MessageReader
17-
from .asc import ASCReader, GzipASCReader
17+
from .asc import ASCReader
1818
from .blf import BLFReader
1919
from .canutils import CanutilsLogReader
2020
from .csv import CSVReader
@@ -27,12 +27,13 @@ class LogReader(BaseIOHandler):
2727
2828
The format is determined from the file format which can be one of:
2929
* .asc
30-
* .asc.gz
3130
* .blf
3231
* .csv
3332
* .db
3433
* .log
3534
35+
Or any of the above compressed using gzip (.gz)
36+
3637
Exposes a simple iterator interface, to use simply:
3738
3839
>>> for msg in LogReader("some/path/to/my_file.log"):
@@ -50,7 +51,6 @@ class LogReader(BaseIOHandler):
5051
fetched_plugins = False
5152
message_readers = {
5253
".asc": ASCReader,
53-
".asc.gz": GzipASCReader,
5454
".blf": BLFReader,
5555
".csv": CSVReader,
5656
".db": SqliteReader,
@@ -77,7 +77,11 @@ def __new__( # type: ignore
7777
)
7878
LogReader.fetched_plugins = True
7979

80-
suffix = "".join(s.lower() for s in pathlib.PurePath(filename).suffixes)
80+
suffix = pathlib.PurePath(filename).suffix.lower()
81+
82+
if suffix == ".gz":
83+
suffix, filename = LogReader.unzip(filename)
84+
8185
try:
8286
return typing.cast(
8387
MessageReader,
@@ -88,6 +92,23 @@ def __new__( # type: ignore
8892
f'No read support for this unknown log format "{suffix}"'
8993
) from None
9094

95+
@staticmethod
96+
def unzip(zipfile: "can.typechecking.StringPathLike"):
97+
"""
98+
Return the suffix and io object of the decompressed file.
99+
"""
100+
real_suffix = pathlib.Path(zipfile).suffixes[-2].lower()
101+
file = gzip.open(zipfile, "rt")
102+
103+
# Re-open in binary mode if file not readable.
104+
try:
105+
file.read()
106+
file.seek(0)
107+
except UnicodeDecodeError:
108+
file = gzip.open(zipfile, "rb")
109+
110+
return real_suffix, file
111+
91112

92113
class MessageSync: # pylint: disable=too-few-public-methods
93114
"""

0 commit comments

Comments
 (0)