Skip to content

bareutils.compression

module bareutils.compression

Summary

Compression utilities

Description

Only compression direclty supported by standard library functions are provided here to avoid the need for additional dependencies. Other compression methods should be implemented in a separate module.

async function bareutils.compression.compression_reader

Summary

Reads a compressed stream and returns the decompressed bytes.

async bareutils.compression.compression_reader(
source: AsyncIterable[bytes],
decompressobj: Decompressor
) -> bytes

Parameters

source: AsyncIterable[bytes]

The input stream.

decompressobj: Decompressor

The decompressor.

Returns

bytes: The decompressed bytes.

async generator function bareutils.compression.compression_reader_adapter

Summary

Adapters a reader to decompress the data.

async bareutils.compression.compression_reader_adapter(
reader: AsyncIterable[bytes],
decompressobj: Decompressor
) -> AsyncIterable[bytes]

Parameters

reader: AsyncIterable[bytes]

The reader.

decompressobj: Decompressor

The decompressor.

Yields

AsyncIterable[bytes]: An async iterable of decompressed bytes.

function bareutils.compression.compression_writer

Summary

Create an async iterator for compressed content.

bareutils.compression.compression_writer(
buf: bytes,
compressobj: Compressor,
chunk_size: int
) -> AsyncIterable[bytes]

Parameters

buf: bytes

The bytes to compress.

compressobj: Compressor

The compressor.

chunk_size: int

An optional chunk size where -1 indicates no chunking. Defaults to -1.

Returns

AsyncIterable[bytes]: An async iterator of compressed bytes.

async generator function bareutils.compression.compression_writer_adapter

Summary

Adapts a bytes generator to generated compressed output.

async bareutils.compression.compression_writer_adapter(
writer: AsyncIterable[bytes],
compressobj: Compressor
) -> AsyncIterable[bytes]

Parameters

writer: AsyncIterable[bytes]

The writer to be adapted.

compressobj: Compressor

A compressor

Yields

AsyncIterable[bytes]: The compressed content as bytes

function bareutils.compression.make_compress_compressobj

Summary

Make a compressor for 'compress'

Description

Note: This is not used by browsers anymore and should be avoided.

bareutils.compression.make_compress_compressobj() -> Compressor

Returns

Compressor: A compress compressor.

function bareutils.compression.make_compress_decompressobj

Summary

Make a compressor for 'compress'

Description

Note: This is not used by browsers anymore and should be avoided.

bareutils.compression.make_compress_decompressobj() -> Decompressor

Returns

Decompressor: A compress compressor.

function bareutils.compression.make_deflate_compressobj

Summary

Make a compressor for 'deflate'

bareutils.compression.make_deflate_compressobj() -> Compressor

Returns

Compressor: A deflate compressor.

function bareutils.compression.make_deflate_decompressobj

Summary

Make a compressor for 'deflate'

bareutils.compression.make_deflate_decompressobj() -> Decompressor

Returns

Decompressor: A deflate compressor.

function bareutils.compression.make_gzip_compressobj

Summary

Make a compressor for 'gzip'

bareutils.compression.make_gzip_compressobj() -> Compressor

Returns

Compressor: A gzip compressor.

function bareutils.compression.make_gzip_decompressobj

Summary

Make a compressor for 'gzip'

bareutils.compression.make_gzip_decompressobj() -> Decompressor

Returns

Decompressor: A gzip compressor.

class Compressor

Summary

A class to represent the methods available on a compressor

bareutils.compression.Compressor() -> None

method Compressor.compress

Summary

Compress a buffer

Compressor.compress(
buf: bytes
) -> bytes

Parameters

buf: bytes

The buffer to compress.

Returns

bytes: The compressed buffer.

method Compressor.flush

Summary

Flush the compressor

Compressor.flush() -> bytes

Returns

bytes: The remaining bytes.

class Decompressor

Summary

A class to represent the methods available on a compressor

bareutils.compression.Decompressor() -> None

property Decompressor.eof

Summary

A boolean indicating whether the end of the compressed data stream has been reached.

Description

This makes it possible to distinguish between a properly formed compressed stream, and an incomplete or truncated one.

eof -> bool

property Decompressor.unconsumed_tail

Summary

A bytes object that contains any data that was not consumed by the

Description

last decompress() call because it exceeded the limit for the uncompressed data buffer.

unconsumed_tail -> bytes

property Decompressor.unused_data

Summary

A bytes object which contains any bytes past the end of the

Description

compressed data.

That is, this remains b"" until the last byte that contains compression data is available. If the whole bytestring turned out to contain compressed data, this is b"", an empty bytes object.

unused_data -> bytes

method Decompressor.copy

Summary

Returns a copy of the decompression object.

Description

This can be used to save the state of the decompressor midway through the data stream in order to speed up random seeks into the stream at a future point.

Decompressor.copy() -> Decompressor

Returns

Decompressor: A copy of the decompressor.

method Decompressor.decompress

Summary

Decompress data, returning a bytes object containing the uncompressed

Description

data corresponding to at least part of the data in string.

This data should be concatenated to the output produced by any preceding calls to the decompress() method. Some of the input data may be preserved in internal buffers for later processing.

If the optional parameter max_length is non-zero then the return value will be no longer than max_length. This may mean that not all of the compressed input can be processed; and unconsumed data will be stored in the attribute unconsumed_tail. This bytestring must be passed to a subsequent call to decompress() if decompression is to continue. If max_length is zero then the whole input is decompressed, and unconsumed_tail is empty.

Decompressor.decompress(
buf: bytes,
max_length: int
) -> bytes

Parameters

buf: bytes

The data to decompress.

max_length: int (optional)

Max length of output. Defaults to 0.

Returns

bytes: The decompressed data.

method Decompressor.flush

Summary

All pending input is processed, and a bytes object containing the

Description

remaining uncompressed output is returned. After calling flush(), the decompress() method cannot be called again; the only realistic action is to delete the object.

Decompressor.flush(
length: int | None
) -> bytes

Parameters

length: int | None

The initial size of the output buffer. Defaults to None.

Returns

bytes: The remaining uncompressed output.