4
4
ZSTD_DStreamOutSize )
5
5
from compression ._common import _streams
6
6
7
- __all__ = (" ZstdFile" , " open" )
7
+ __all__ = (' ZstdFile' , ' open' )
8
8
9
9
_MODE_CLOSED = 0
10
10
_MODE_READ = 1
@@ -31,15 +31,15 @@ class ZstdFile(_streams.BaseStream):
31
31
FLUSH_BLOCK = ZstdCompressor .FLUSH_BLOCK
32
32
FLUSH_FRAME = ZstdCompressor .FLUSH_FRAME
33
33
34
- def __init__ (self , file , / , mode = "r" , * ,
34
+ def __init__ (self , file , / , mode = 'r' , * ,
35
35
level = None , options = None , zstd_dict = None ):
36
36
"""Open a Zstandard compressed file in binary mode.
37
37
38
38
*file* can be either an file-like object, or a file name to open.
39
39
40
- *mode* can be "r" for reading (default), "w" for (over)writing, "x" for
41
- creating exclusively, or "a" for appending. These can equivalently be
42
- given as "rb", "wb", "xb" and "ab" respectively.
40
+ *mode* can be 'r' for reading (default), 'w' for (over)writing, 'x' for
41
+ creating exclusively, or 'a' for appending. These can equivalently be
42
+ given as 'rb', 'wb', 'xb' and 'ab' respectively.
43
43
44
44
*level* is an optional int specifying the compression level to use,
45
45
or COMPRESSION_LEVEL_DEFAULT if not given.
@@ -57,33 +57,33 @@ def __init__(self, file, /, mode="r", *,
57
57
self ._buffer = None
58
58
59
59
if not isinstance (mode , str ):
60
- raise ValueError (" mode must be a str" )
60
+ raise ValueError (' mode must be a str' )
61
61
if options is not None and not isinstance (options , dict ):
62
- raise TypeError (" options must be a dict or None" )
63
- mode = mode .removesuffix ("b" ) # handle rb, wb, xb, ab
64
- if mode == "r" :
62
+ raise TypeError (' options must be a dict or None' )
63
+ mode = mode .removesuffix ('b' ) # handle rb, wb, xb, ab
64
+ if mode == 'r' :
65
65
if level is not None :
66
- raise TypeError (" level is illegal in read mode" )
66
+ raise TypeError (' level is illegal in read mode' )
67
67
self ._mode = _MODE_READ
68
- elif mode in {"w" , "a" , "x" }:
68
+ elif mode in {'w' , 'a' , 'x' }:
69
69
if level is not None and not isinstance (level , int ):
70
- raise TypeError (" level must be int or None" )
70
+ raise TypeError (' level must be int or None' )
71
71
self ._mode = _MODE_WRITE
72
72
self ._compressor = ZstdCompressor (level = level , options = options ,
73
73
zstd_dict = zstd_dict )
74
74
self ._pos = 0
75
75
else :
76
- raise ValueError (f" Invalid mode: { mode !r} " )
76
+ raise ValueError (f' Invalid mode: { mode !r} ' )
77
77
78
78
if isinstance (file , (str , bytes , PathLike )):
79
79
self ._fp = io .open (file , f'{ mode } b' )
80
80
self ._close_fp = True
81
- elif ((mode == 'r' and hasattr (file , " read" ))
82
- or (mode != 'r' and hasattr (file , " write" ))):
81
+ elif ((mode == 'r' and hasattr (file , ' read' ))
82
+ or (mode != 'r' and hasattr (file , ' write' ))):
83
83
self ._fp = file
84
84
else :
85
- raise TypeError (" file must be a file-like object "
86
- " or a str, bytes, or PathLike object" )
85
+ raise TypeError (' file must be a file-like object '
86
+ ' or a str, bytes, or PathLike object' )
87
87
88
88
if self ._mode == _MODE_READ :
89
89
raw = _streams .DecompressReader (
@@ -151,22 +151,22 @@ def flush(self, mode=FLUSH_BLOCK):
151
151
return
152
152
self ._check_not_closed ()
153
153
if mode not in {self .FLUSH_BLOCK , self .FLUSH_FRAME }:
154
- raise ValueError (" Invalid mode argument, expected either "
155
- " ZstdFile.FLUSH_FRAME or "
156
- " ZstdFile.FLUSH_BLOCK" )
154
+ raise ValueError (' Invalid mode argument, expected either '
155
+ ' ZstdFile.FLUSH_FRAME or '
156
+ ' ZstdFile.FLUSH_BLOCK' )
157
157
if self ._compressor .last_mode == mode :
158
158
return
159
159
# Flush zstd block/frame, and write.
160
160
data = self ._compressor .flush (mode )
161
161
self ._fp .write (data )
162
- if hasattr (self ._fp , " flush" ):
162
+ if hasattr (self ._fp , ' flush' ):
163
163
self ._fp .flush ()
164
164
165
165
def read (self , size = - 1 ):
166
166
"""Read up to size uncompressed bytes from the file.
167
167
168
168
If size is negative or omitted, read until EOF is reached.
169
- Returns b"" if the file is already at EOF.
169
+ Returns b'' if the file is already at EOF.
170
170
"""
171
171
if size is None :
172
172
size = - 1
@@ -178,7 +178,7 @@ def read1(self, size=-1):
178
178
making multiple reads from the underlying stream. Reads up to a
179
179
buffer's worth of data if size is negative.
180
180
181
- Returns b"" if the file is at EOF.
181
+ Returns b'' if the file is at EOF.
182
182
"""
183
183
self ._check_can_read ()
184
184
if size < 0 :
@@ -293,16 +293,16 @@ def writable(self):
293
293
return self ._mode == _MODE_WRITE
294
294
295
295
296
- def open (file , / , mode = "rb" , * , level = None , options = None , zstd_dict = None ,
296
+ def open (file , / , mode = 'rb' , * , level = None , options = None , zstd_dict = None ,
297
297
encoding = None , errors = None , newline = None ):
298
298
"""Open a Zstandard compressed file in binary or text mode.
299
299
300
300
file can be either a file name (given as a str, bytes, or PathLike object),
301
301
in which case the named file is opened, or it can be an existing file object
302
302
to read from or write to.
303
303
304
- The mode parameter can be "r", "rb" (default), "w", "wb", "x", "xb", "a" ,
305
- "ab" for binary mode, or "rt", "wt", "xt", "at" for text mode.
304
+ The mode parameter can be 'r', 'rb' (default), 'w', 'wb', 'x', 'xb', 'a' ,
305
+ 'ab' for binary mode, or 'rt', 'wt', 'xt', 'at' for text mode.
306
306
307
307
The level, options, and zstd_dict parameters specify the settings the same
308
308
as ZstdFile.
@@ -323,19 +323,19 @@ def open(file, /, mode="rb", *, level=None, options=None, zstd_dict=None,
323
323
behavior, and line ending(s).
324
324
"""
325
325
326
- text_mode = "t" in mode
327
- mode = mode .replace ("t" , "" )
326
+ text_mode = 't' in mode
327
+ mode = mode .replace ('t' , '' )
328
328
329
329
if text_mode :
330
- if "b" in mode :
331
- raise ValueError (f" Invalid mode: { mode !r} " )
330
+ if 'b' in mode :
331
+ raise ValueError (f' Invalid mode: { mode !r} ' )
332
332
else :
333
333
if encoding is not None :
334
- raise ValueError (" Argument ' encoding' not supported in binary mode" )
334
+ raise ValueError (' Argument " encoding" not supported in binary mode' )
335
335
if errors is not None :
336
- raise ValueError (" Argument ' errors' not supported in binary mode" )
336
+ raise ValueError (' Argument " errors" not supported in binary mode' )
337
337
if newline is not None :
338
- raise ValueError (" Argument ' newline' not supported in binary mode" )
338
+ raise ValueError (' Argument " newline" not supported in binary mode' )
339
339
340
340
binary_file = ZstdFile (file , mode , level = level , options = options ,
341
341
zstd_dict = zstd_dict )
0 commit comments