1 | n/a | """Interface to the libbzip2 compression library. |
---|
2 | n/a | |
---|
3 | n/a | This module provides a file interface, classes for incremental |
---|
4 | n/a | (de)compression, and functions for one-shot (de)compression. |
---|
5 | n/a | """ |
---|
6 | n/a | |
---|
7 | n/a | __all__ = ["BZ2File", "BZ2Compressor", "BZ2Decompressor", |
---|
8 | n/a | "open", "compress", "decompress"] |
---|
9 | n/a | |
---|
10 | n/a | __author__ = "Nadeem Vawda <nadeem.vawda@gmail.com>" |
---|
11 | n/a | |
---|
12 | n/a | from builtins import open as _builtin_open |
---|
13 | n/a | import io |
---|
14 | n/a | import os |
---|
15 | n/a | import warnings |
---|
16 | n/a | import _compression |
---|
17 | n/a | |
---|
18 | n/a | try: |
---|
19 | n/a | from threading import RLock |
---|
20 | n/a | except ImportError: |
---|
21 | n/a | from dummy_threading import RLock |
---|
22 | n/a | |
---|
23 | n/a | from _bz2 import BZ2Compressor, BZ2Decompressor |
---|
24 | n/a | |
---|
25 | n/a | |
---|
26 | n/a | _MODE_CLOSED = 0 |
---|
27 | n/a | _MODE_READ = 1 |
---|
28 | n/a | # Value 2 no longer used |
---|
29 | n/a | _MODE_WRITE = 3 |
---|
30 | n/a | |
---|
31 | n/a | |
---|
32 | n/a | class BZ2File(_compression.BaseStream): |
---|
33 | n/a | |
---|
34 | n/a | """A file object providing transparent bzip2 (de)compression. |
---|
35 | n/a | |
---|
36 | n/a | A BZ2File can act as a wrapper for an existing file object, or refer |
---|
37 | n/a | directly to a named file on disk. |
---|
38 | n/a | |
---|
39 | n/a | Note that BZ2File provides a *binary* file interface - data read is |
---|
40 | n/a | returned as bytes, and data to be written should be given as bytes. |
---|
41 | n/a | """ |
---|
42 | n/a | |
---|
43 | n/a | def __init__(self, filename, mode="r", buffering=None, compresslevel=9): |
---|
44 | n/a | """Open a bzip2-compressed file. |
---|
45 | n/a | |
---|
46 | n/a | If filename is a str, bytes, or PathLike object, it gives the |
---|
47 | n/a | name of the file to be opened. Otherwise, it should be a file |
---|
48 | n/a | object, which will be used to read or write the compressed data. |
---|
49 | n/a | |
---|
50 | n/a | mode can be 'r' for reading (default), 'w' for (over)writing, |
---|
51 | n/a | 'x' for creating exclusively, or 'a' for appending. These can |
---|
52 | n/a | equivalently be given as 'rb', 'wb', 'xb', and 'ab'. |
---|
53 | n/a | |
---|
54 | n/a | buffering is ignored. Its use is deprecated. |
---|
55 | n/a | |
---|
56 | n/a | If mode is 'w', 'x' or 'a', compresslevel can be a number between 1 |
---|
57 | n/a | and 9 specifying the level of compression: 1 produces the least |
---|
58 | n/a | compression, and 9 (default) produces the most compression. |
---|
59 | n/a | |
---|
60 | n/a | If mode is 'r', the input file may be the concatenation of |
---|
61 | n/a | multiple compressed streams. |
---|
62 | n/a | """ |
---|
63 | n/a | # This lock must be recursive, so that BufferedIOBase's |
---|
64 | n/a | # writelines() does not deadlock. |
---|
65 | n/a | self._lock = RLock() |
---|
66 | n/a | self._fp = None |
---|
67 | n/a | self._closefp = False |
---|
68 | n/a | self._mode = _MODE_CLOSED |
---|
69 | n/a | |
---|
70 | n/a | if buffering is not None: |
---|
71 | n/a | warnings.warn("Use of 'buffering' argument is deprecated", |
---|
72 | n/a | DeprecationWarning) |
---|
73 | n/a | |
---|
74 | n/a | if not (1 <= compresslevel <= 9): |
---|
75 | n/a | raise ValueError("compresslevel must be between 1 and 9") |
---|
76 | n/a | |
---|
77 | n/a | if mode in ("", "r", "rb"): |
---|
78 | n/a | mode = "rb" |
---|
79 | n/a | mode_code = _MODE_READ |
---|
80 | n/a | elif mode in ("w", "wb"): |
---|
81 | n/a | mode = "wb" |
---|
82 | n/a | mode_code = _MODE_WRITE |
---|
83 | n/a | self._compressor = BZ2Compressor(compresslevel) |
---|
84 | n/a | elif mode in ("x", "xb"): |
---|
85 | n/a | mode = "xb" |
---|
86 | n/a | mode_code = _MODE_WRITE |
---|
87 | n/a | self._compressor = BZ2Compressor(compresslevel) |
---|
88 | n/a | elif mode in ("a", "ab"): |
---|
89 | n/a | mode = "ab" |
---|
90 | n/a | mode_code = _MODE_WRITE |
---|
91 | n/a | self._compressor = BZ2Compressor(compresslevel) |
---|
92 | n/a | else: |
---|
93 | n/a | raise ValueError("Invalid mode: %r" % (mode,)) |
---|
94 | n/a | |
---|
95 | n/a | if isinstance(filename, (str, bytes, os.PathLike)): |
---|
96 | n/a | self._fp = _builtin_open(filename, mode) |
---|
97 | n/a | self._closefp = True |
---|
98 | n/a | self._mode = mode_code |
---|
99 | n/a | elif hasattr(filename, "read") or hasattr(filename, "write"): |
---|
100 | n/a | self._fp = filename |
---|
101 | n/a | self._mode = mode_code |
---|
102 | n/a | else: |
---|
103 | n/a | raise TypeError("filename must be a str, bytes, file or PathLike object") |
---|
104 | n/a | |
---|
105 | n/a | if self._mode == _MODE_READ: |
---|
106 | n/a | raw = _compression.DecompressReader(self._fp, |
---|
107 | n/a | BZ2Decompressor, trailing_error=OSError) |
---|
108 | n/a | self._buffer = io.BufferedReader(raw) |
---|
109 | n/a | else: |
---|
110 | n/a | self._pos = 0 |
---|
111 | n/a | |
---|
112 | n/a | def close(self): |
---|
113 | n/a | """Flush and close the file. |
---|
114 | n/a | |
---|
115 | n/a | May be called more than once without error. Once the file is |
---|
116 | n/a | closed, any other operation on it will raise a ValueError. |
---|
117 | n/a | """ |
---|
118 | n/a | with self._lock: |
---|
119 | n/a | if self._mode == _MODE_CLOSED: |
---|
120 | n/a | return |
---|
121 | n/a | try: |
---|
122 | n/a | if self._mode == _MODE_READ: |
---|
123 | n/a | self._buffer.close() |
---|
124 | n/a | elif self._mode == _MODE_WRITE: |
---|
125 | n/a | self._fp.write(self._compressor.flush()) |
---|
126 | n/a | self._compressor = None |
---|
127 | n/a | finally: |
---|
128 | n/a | try: |
---|
129 | n/a | if self._closefp: |
---|
130 | n/a | self._fp.close() |
---|
131 | n/a | finally: |
---|
132 | n/a | self._fp = None |
---|
133 | n/a | self._closefp = False |
---|
134 | n/a | self._mode = _MODE_CLOSED |
---|
135 | n/a | self._buffer = None |
---|
136 | n/a | |
---|
137 | n/a | @property |
---|
138 | n/a | def closed(self): |
---|
139 | n/a | """True if this file is closed.""" |
---|
140 | n/a | return self._mode == _MODE_CLOSED |
---|
141 | n/a | |
---|
142 | n/a | def fileno(self): |
---|
143 | n/a | """Return the file descriptor for the underlying file.""" |
---|
144 | n/a | self._check_not_closed() |
---|
145 | n/a | return self._fp.fileno() |
---|
146 | n/a | |
---|
147 | n/a | def seekable(self): |
---|
148 | n/a | """Return whether the file supports seeking.""" |
---|
149 | n/a | return self.readable() and self._buffer.seekable() |
---|
150 | n/a | |
---|
151 | n/a | def readable(self): |
---|
152 | n/a | """Return whether the file was opened for reading.""" |
---|
153 | n/a | self._check_not_closed() |
---|
154 | n/a | return self._mode == _MODE_READ |
---|
155 | n/a | |
---|
156 | n/a | def writable(self): |
---|
157 | n/a | """Return whether the file was opened for writing.""" |
---|
158 | n/a | self._check_not_closed() |
---|
159 | n/a | return self._mode == _MODE_WRITE |
---|
160 | n/a | |
---|
161 | n/a | def peek(self, n=0): |
---|
162 | n/a | """Return buffered data without advancing the file position. |
---|
163 | n/a | |
---|
164 | n/a | Always returns at least one byte of data, unless at EOF. |
---|
165 | n/a | The exact number of bytes returned is unspecified. |
---|
166 | n/a | """ |
---|
167 | n/a | with self._lock: |
---|
168 | n/a | self._check_can_read() |
---|
169 | n/a | # Relies on the undocumented fact that BufferedReader.peek() |
---|
170 | n/a | # always returns at least one byte (except at EOF), independent |
---|
171 | n/a | # of the value of n |
---|
172 | n/a | return self._buffer.peek(n) |
---|
173 | n/a | |
---|
174 | n/a | def read(self, size=-1): |
---|
175 | n/a | """Read up to size uncompressed bytes from the file. |
---|
176 | n/a | |
---|
177 | n/a | If size is negative or omitted, read until EOF is reached. |
---|
178 | n/a | Returns b'' if the file is already at EOF. |
---|
179 | n/a | """ |
---|
180 | n/a | with self._lock: |
---|
181 | n/a | self._check_can_read() |
---|
182 | n/a | return self._buffer.read(size) |
---|
183 | n/a | |
---|
184 | n/a | def read1(self, size=-1): |
---|
185 | n/a | """Read up to size uncompressed bytes, while trying to avoid |
---|
186 | n/a | making multiple reads from the underlying stream. Reads up to a |
---|
187 | n/a | buffer's worth of data if size is negative. |
---|
188 | n/a | |
---|
189 | n/a | Returns b'' if the file is at EOF. |
---|
190 | n/a | """ |
---|
191 | n/a | with self._lock: |
---|
192 | n/a | self._check_can_read() |
---|
193 | n/a | if size < 0: |
---|
194 | n/a | size = io.DEFAULT_BUFFER_SIZE |
---|
195 | n/a | return self._buffer.read1(size) |
---|
196 | n/a | |
---|
197 | n/a | def readinto(self, b): |
---|
198 | n/a | """Read bytes into b. |
---|
199 | n/a | |
---|
200 | n/a | Returns the number of bytes read (0 for EOF). |
---|
201 | n/a | """ |
---|
202 | n/a | with self._lock: |
---|
203 | n/a | self._check_can_read() |
---|
204 | n/a | return self._buffer.readinto(b) |
---|
205 | n/a | |
---|
206 | n/a | def readline(self, size=-1): |
---|
207 | n/a | """Read a line of uncompressed bytes from the file. |
---|
208 | n/a | |
---|
209 | n/a | The terminating newline (if present) is retained. If size is |
---|
210 | n/a | non-negative, no more than size bytes will be read (in which |
---|
211 | n/a | case the line may be incomplete). Returns b'' if already at EOF. |
---|
212 | n/a | """ |
---|
213 | n/a | if not isinstance(size, int): |
---|
214 | n/a | if not hasattr(size, "__index__"): |
---|
215 | n/a | raise TypeError("Integer argument expected") |
---|
216 | n/a | size = size.__index__() |
---|
217 | n/a | with self._lock: |
---|
218 | n/a | self._check_can_read() |
---|
219 | n/a | return self._buffer.readline(size) |
---|
220 | n/a | |
---|
221 | n/a | def readlines(self, size=-1): |
---|
222 | n/a | """Read a list of lines of uncompressed bytes from the file. |
---|
223 | n/a | |
---|
224 | n/a | size can be specified to control the number of lines read: no |
---|
225 | n/a | further lines will be read once the total size of the lines read |
---|
226 | n/a | so far equals or exceeds size. |
---|
227 | n/a | """ |
---|
228 | n/a | if not isinstance(size, int): |
---|
229 | n/a | if not hasattr(size, "__index__"): |
---|
230 | n/a | raise TypeError("Integer argument expected") |
---|
231 | n/a | size = size.__index__() |
---|
232 | n/a | with self._lock: |
---|
233 | n/a | self._check_can_read() |
---|
234 | n/a | return self._buffer.readlines(size) |
---|
235 | n/a | |
---|
236 | n/a | def write(self, data): |
---|
237 | n/a | """Write a byte string to the file. |
---|
238 | n/a | |
---|
239 | n/a | Returns the number of uncompressed bytes written, which is |
---|
240 | n/a | always len(data). Note that due to buffering, the file on disk |
---|
241 | n/a | may not reflect the data written until close() is called. |
---|
242 | n/a | """ |
---|
243 | n/a | with self._lock: |
---|
244 | n/a | self._check_can_write() |
---|
245 | n/a | compressed = self._compressor.compress(data) |
---|
246 | n/a | self._fp.write(compressed) |
---|
247 | n/a | self._pos += len(data) |
---|
248 | n/a | return len(data) |
---|
249 | n/a | |
---|
250 | n/a | def writelines(self, seq): |
---|
251 | n/a | """Write a sequence of byte strings to the file. |
---|
252 | n/a | |
---|
253 | n/a | Returns the number of uncompressed bytes written. |
---|
254 | n/a | seq can be any iterable yielding byte strings. |
---|
255 | n/a | |
---|
256 | n/a | Line separators are not added between the written byte strings. |
---|
257 | n/a | """ |
---|
258 | n/a | with self._lock: |
---|
259 | n/a | return _compression.BaseStream.writelines(self, seq) |
---|
260 | n/a | |
---|
261 | n/a | def seek(self, offset, whence=io.SEEK_SET): |
---|
262 | n/a | """Change the file position. |
---|
263 | n/a | |
---|
264 | n/a | The new position is specified by offset, relative to the |
---|
265 | n/a | position indicated by whence. Values for whence are: |
---|
266 | n/a | |
---|
267 | n/a | 0: start of stream (default); offset must not be negative |
---|
268 | n/a | 1: current stream position |
---|
269 | n/a | 2: end of stream; offset must not be positive |
---|
270 | n/a | |
---|
271 | n/a | Returns the new file position. |
---|
272 | n/a | |
---|
273 | n/a | Note that seeking is emulated, so depending on the parameters, |
---|
274 | n/a | this operation may be extremely slow. |
---|
275 | n/a | """ |
---|
276 | n/a | with self._lock: |
---|
277 | n/a | self._check_can_seek() |
---|
278 | n/a | return self._buffer.seek(offset, whence) |
---|
279 | n/a | |
---|
280 | n/a | def tell(self): |
---|
281 | n/a | """Return the current file position.""" |
---|
282 | n/a | with self._lock: |
---|
283 | n/a | self._check_not_closed() |
---|
284 | n/a | if self._mode == _MODE_READ: |
---|
285 | n/a | return self._buffer.tell() |
---|
286 | n/a | return self._pos |
---|
287 | n/a | |
---|
288 | n/a | |
---|
289 | n/a | def open(filename, mode="rb", compresslevel=9, |
---|
290 | n/a | encoding=None, errors=None, newline=None): |
---|
291 | n/a | """Open a bzip2-compressed file in binary or text mode. |
---|
292 | n/a | |
---|
293 | n/a | The filename argument can be an actual filename (a str, bytes, or |
---|
294 | n/a | PathLike object), or an existing file object to read from or write |
---|
295 | n/a | to. |
---|
296 | n/a | |
---|
297 | n/a | The mode argument can be "r", "rb", "w", "wb", "x", "xb", "a" or |
---|
298 | n/a | "ab" for binary mode, or "rt", "wt", "xt" or "at" for text mode. |
---|
299 | n/a | The default mode is "rb", and the default compresslevel is 9. |
---|
300 | n/a | |
---|
301 | n/a | For binary mode, this function is equivalent to the BZ2File |
---|
302 | n/a | constructor: BZ2File(filename, mode, compresslevel). In this case, |
---|
303 | n/a | the encoding, errors and newline arguments must not be provided. |
---|
304 | n/a | |
---|
305 | n/a | For text mode, a BZ2File object is created, and wrapped in an |
---|
306 | n/a | io.TextIOWrapper instance with the specified encoding, error |
---|
307 | n/a | handling behavior, and line ending(s). |
---|
308 | n/a | |
---|
309 | n/a | """ |
---|
310 | n/a | if "t" in mode: |
---|
311 | n/a | if "b" in mode: |
---|
312 | n/a | raise ValueError("Invalid mode: %r" % (mode,)) |
---|
313 | n/a | else: |
---|
314 | n/a | if encoding is not None: |
---|
315 | n/a | raise ValueError("Argument 'encoding' not supported in binary mode") |
---|
316 | n/a | if errors is not None: |
---|
317 | n/a | raise ValueError("Argument 'errors' not supported in binary mode") |
---|
318 | n/a | if newline is not None: |
---|
319 | n/a | raise ValueError("Argument 'newline' not supported in binary mode") |
---|
320 | n/a | |
---|
321 | n/a | bz_mode = mode.replace("t", "") |
---|
322 | n/a | binary_file = BZ2File(filename, bz_mode, compresslevel=compresslevel) |
---|
323 | n/a | |
---|
324 | n/a | if "t" in mode: |
---|
325 | n/a | return io.TextIOWrapper(binary_file, encoding, errors, newline) |
---|
326 | n/a | else: |
---|
327 | n/a | return binary_file |
---|
328 | n/a | |
---|
329 | n/a | |
---|
330 | n/a | def compress(data, compresslevel=9): |
---|
331 | n/a | """Compress a block of data. |
---|
332 | n/a | |
---|
333 | n/a | compresslevel, if given, must be a number between 1 and 9. |
---|
334 | n/a | |
---|
335 | n/a | For incremental compression, use a BZ2Compressor object instead. |
---|
336 | n/a | """ |
---|
337 | n/a | comp = BZ2Compressor(compresslevel) |
---|
338 | n/a | return comp.compress(data) + comp.flush() |
---|
339 | n/a | |
---|
340 | n/a | |
---|
341 | n/a | def decompress(data): |
---|
342 | n/a | """Decompress a block of data. |
---|
343 | n/a | |
---|
344 | n/a | For incremental decompression, use a BZ2Decompressor object instead. |
---|
345 | n/a | """ |
---|
346 | n/a | results = [] |
---|
347 | n/a | while data: |
---|
348 | n/a | decomp = BZ2Decompressor() |
---|
349 | n/a | try: |
---|
350 | n/a | res = decomp.decompress(data) |
---|
351 | n/a | except OSError: |
---|
352 | n/a | if results: |
---|
353 | n/a | break # Leftover data is not a valid bzip2 stream; ignore it. |
---|
354 | n/a | else: |
---|
355 | n/a | raise # Error on the first iteration; bail out. |
---|
356 | n/a | results.append(res) |
---|
357 | n/a | if not decomp.eof: |
---|
358 | n/a | raise ValueError("Compressed data ended before the " |
---|
359 | n/a | "end-of-stream marker was reached") |
---|
360 | n/a | data = decomp.unused_data |
---|
361 | n/a | return b"".join(results) |
---|