3
3
well as :class:`MessageSync` which plays back messages
4
4
in the recorded order an time intervals.
5
5
"""
6
-
6
+ import gzip
7
7
import pathlib
8
8
from time import time , sleep
9
9
import typing
14
14
import can
15
15
16
16
from .generic import BaseIOHandler , MessageReader
17
- from .asc import ASCReader , GzipASCReader
17
+ from .asc import ASCReader
18
18
from .blf import BLFReader
19
19
from .canutils import CanutilsLogReader
20
20
from .csv import CSVReader
@@ -27,12 +27,13 @@ class LogReader(BaseIOHandler):
27
27
28
28
The format is determined from the file format which can be one of:
29
29
* .asc
30
- * .asc.gz
31
30
* .blf
32
31
* .csv
33
32
* .db
34
33
* .log
35
34
35
+ Or any of the above compressed using gzip (.gz)
36
+
36
37
Exposes a simple iterator interface, to use simply:
37
38
38
39
>>> for msg in LogReader("some/path/to/my_file.log"):
@@ -50,7 +51,6 @@ class LogReader(BaseIOHandler):
50
51
fetched_plugins = False
51
52
message_readers = {
52
53
".asc" : ASCReader ,
53
- ".asc.gz" : GzipASCReader ,
54
54
".blf" : BLFReader ,
55
55
".csv" : CSVReader ,
56
56
".db" : SqliteReader ,
@@ -77,7 +77,11 @@ def __new__( # type: ignore
77
77
)
78
78
LogReader .fetched_plugins = True
79
79
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
+
81
85
try :
82
86
return typing .cast (
83
87
MessageReader ,
@@ -88,6 +92,23 @@ def __new__( # type: ignore
88
92
f'No read support for this unknown log format "{ suffix } "'
89
93
) from None
90
94
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
+
91
112
92
113
class MessageSync : # pylint: disable=too-few-public-methods
93
114
"""
0 commit comments