|
| 1 | +import typing |
| 2 | +from collections.abc import ByteString |
| 3 | +from io import BytesIO, IOBase |
| 4 | +from mmap import mmap |
| 5 | +from warnings import warn |
| 6 | + |
| 7 | +from .BlastError import BlastError |
| 8 | +from .ctypes import _decompressBytes, _decompressStream |
| 9 | + |
| 10 | +__all__ = ("decompressStreamToStream", "decompressStreamToBytes", "decompressBytesWholeToStream", "decompressBytesWholeToBytes", "decompressBytesChunkedToStream", "decompressBytesChunkedToBytes", "decompress", "DEFAULT_CHUNK_SIZE") |
| 11 | + |
| 12 | +DEFAULT_CHUNK_SIZE = 16384 |
| 13 | + |
| 14 | + |
| 15 | +def decompressStreamToStream(inputStream: IOBase, outputStream: IOBase, chunkSize: int = DEFAULT_CHUNK_SIZE) -> (int, IOBase): |
| 16 | + """Used to do streaming decompression. The first arg is the stream to read from, the second ard is the stream to write to. |
| 17 | + May be a memory map. `chunkSize` is the hint""" |
| 18 | + |
| 19 | + errorCode, left = _decompressStream(inputStream, outputStream, chunkSize=chunkSize) |
| 20 | + |
| 21 | + if errorCode: |
| 22 | + raise Exception(BlastError(errorCode)) |
| 23 | + |
| 24 | + return left.value, outputStream |
| 25 | + |
| 26 | + |
| 27 | +def decompressStreamToBytes(inputStream: IOBase, chunkSize: int = DEFAULT_CHUNK_SIZE) -> (int, ByteString): |
| 28 | + """Decompresses `inputStream` into `outputStream`. Processes the whole data. You should use it instead of `decompressBytesChunkedToStream`, if it is possible.""" |
| 29 | + with BytesIO() as outputStream: |
| 30 | + left, _ = decompressStreamToStream(inputStream, outputStream, chunkSize) |
| 31 | + return left, outputStream.getvalue() |
| 32 | + |
| 33 | + |
| 34 | +def decompressBytesWholeToStream(compressed: ByteString, outputStream: IOBase) -> (int, IOBase): |
| 35 | + """Decompresses `compressed` into `outputStream`. Processes the whole data. You should use it instead of `decompressBytesChunkedToStream`, if it is possible.""" |
| 36 | + errorCode, left = _decompressBytes(compressed, outputStream) |
| 37 | + |
| 38 | + if errorCode: |
| 39 | + raise Exception(BlastError(errorCode)) |
| 40 | + |
| 41 | + return left.value, outputStream |
| 42 | + |
| 43 | + |
| 44 | +def decompressBytesWholeToBytes(compressed: ByteString) -> (int, ByteString): |
| 45 | + """Decompresses `compressed` and returns tuple (remaining, decompressed)`. Processes the whole data. You should use it instead of `decompressBytesChunkedToStream`, if it is possible.""" |
| 46 | + with BytesIO() as outputStream: |
| 47 | + left, _ = decompressBytesWholeToStream(compressed, outputStream) |
| 48 | + return left, outputStream.getvalue() |
| 49 | + |
| 50 | + |
| 51 | +def decompressBytesChunkedToStream(compressed: ByteString, outputStream: IOBase, chunkSize: int = DEFAULT_CHUNK_SIZE) -> (int, IOBase): |
| 52 | + """Decompresses `compressed` into `outputStream`. Processes the `compressed` the same way `decompressStreamToStream` does. In fact it is just a wrapper around it and `BytesIO`. Has bigger overhead than `decompressBytesWholeToStream` for the data that is already in memory: first it copies the data into `BytesIO`, then it allocates space for chunks, then it copies data from there into chunks, each copying has overhead of calling from C into python.""" |
| 53 | + _efficiencyDeprecationMessage(decompressBytesChunkedToStream, decompressBytesWholeToStream) |
| 54 | + chunkSize = min(chunkSize, len(compressed)) |
| 55 | + with BytesIO(compressed) as inputStream: |
| 56 | + return decompressStreamToStream(inputStream, outputStream, chunkSize=chunkSize) |
| 57 | + |
| 58 | + |
| 59 | +def decompressBytesChunkedToBytes(compressed: ByteString, chunkSize: int = DEFAULT_CHUNK_SIZE) -> (int, ByteString): |
| 60 | + """Decompresses `compressed` into `bytes`. Processes the `compressed` the same way `decompressStreamToStream` does. In fact it is just a wrapper around it and `BytesIO`. Has bigger overhead than `decompressBytesWholeToStream` for the data that is already in memory: first it copies the data into `BytesIO`, then it allocates space for chunks, then it copies data from there into chunks, each copying has overhead of calling from C into python.""" |
| 61 | + _efficiencyDeprecationMessage(decompressBytesChunkedToBytes, decompressBytesWholeToBytes) |
| 62 | + with BytesIO() as outputStream: |
| 63 | + left, _ = decompressBytesChunkedToStream(compressed, outputStream, chunkSize) |
| 64 | + return left, outputStream.getvalue() |
| 65 | + |
| 66 | + |
| 67 | +def _efficiencyDeprecationMessage(calledFunc, func) -> None: |
| 68 | + warn("It is inefficient to use `" + calledFunc.__name__ + "`. Use `" + func.__name__ + "` for this use case") |
| 69 | + |
| 70 | + |
| 71 | +_functionsUseCaseMapping = ( |
| 72 | + decompressStreamToStream, |
| 73 | + decompressBytesWholeToStream, |
| 74 | + decompressStreamToBytes, |
| 75 | + decompressBytesWholeToBytes, |
| 76 | +) |
| 77 | + |
| 78 | + |
| 79 | +def decompress(compressed: typing.Union[ByteString, IOBase], outputStream: typing.Optional[IOBase] = None, chunkSize: int = DEFAULT_CHUNK_SIZE) -> (int, typing.Union[ByteString, IOBase]): |
| 80 | + """A convenience function. It is better to use the more specialized ones since they have less overhead. It decompresses `compressed` into `outputStream` and returns a tuple `(left, output)`. |
| 81 | + `compressed` can be either a stream, or `bytes`-like stuff. |
| 82 | + If `outputStream` is None, then it returns `bytes`. If `outputStream` is a stream, it writes into it. |
| 83 | + `left` returned is the count of bytes in the array/stream that weren't processed.""" |
| 84 | + |
| 85 | + isOutputBytes = outputStream is None |
| 86 | + isInputBytes = isinstance(compressed, (ByteString, mmap)) |
| 87 | + selector = isOutputBytes << 1 | int(isInputBytes) |
| 88 | + func = _functionsUseCaseMapping[selector] |
| 89 | + argz = [compressed] |
| 90 | + if not isOutputBytes: |
| 91 | + argz.append(outputStream) |
| 92 | + if not isInputBytes: |
| 93 | + argz.append(chunkSize) |
| 94 | + _efficiencyDeprecationMessage(decompress, func) |
| 95 | + return func(*argz) |
0 commit comments