Compressor Serializers

Data compressor serializer module.

class easynetwork.serializers.wrapper.compressor.BZ2CompressorSerializer

Bases: AbstractCompressorSerializer[_T_SentDTOPacket, _T_ReceivedDTOPacket]

A serializer wrapper to handle bzip2 compressed data, built on top of bz2 module.

__init__(serializer: AbstractPacketSerializer[_T_SentDTOPacket, _T_ReceivedDTOPacket], *, compress_level: int | None = None, debug: bool = False) None
Parameters:
  • serializer (AbstractPacketSerializer[_T_SentDTOPacket, _T_ReceivedDTOPacket]) – The serializer to wrap.

  • compress_level (int | None) – bzip2 compression level. Defaults to 9.

  • debug (bool) – If True, add information to DeserializeError via the error_info attribute.

final new_compressor_stream() bz2.BZ2Compressor

See AbstractCompressorSerializer.new_compressor_stream() documentation for details.

Return type:

bz2.BZ2Compressor

final new_decompressor_stream() bz2.BZ2Decompressor

See AbstractCompressorSerializer.new_decompressor_stream() documentation for details.

Return type:

bz2.BZ2Decompressor

class easynetwork.serializers.wrapper.compressor.ZlibCompressorSerializer

Bases: AbstractCompressorSerializer[_T_SentDTOPacket, _T_ReceivedDTOPacket]

A serializer wrapper to handle zlib compressed data, built on top of zlib module.

__init__(serializer: AbstractPacketSerializer[_T_SentDTOPacket, _T_ReceivedDTOPacket], *, compress_level: int | None = None, debug: bool = False) None
Parameters:
final new_compressor_stream() zlib.Compress

See AbstractCompressorSerializer.new_compressor_stream() documentation for details.

Return type:

zlib.Compress

final new_decompressor_stream() zlib.Decompress

See AbstractCompressorSerializer.new_decompressor_stream() documentation for details.

Return type:

zlib.Decompress

Abstract Compressor Interface

class easynetwork.serializers.wrapper.compressor.AbstractCompressorSerializer

Bases: BufferedIncrementalPacketSerializer[_T_SentDTOPacket, _T_ReceivedDTOPacket, memoryview]

A serializer wrapper base class for compressors.

__init__(serializer: AbstractPacketSerializer[_T_SentDTOPacket, _T_ReceivedDTOPacket], expected_decompress_error: type[Exception] | tuple[type[Exception], ...], *, debug: bool = False) None
Parameters:
abstractmethod new_compressor_stream() CompressorInterface
Returns:

an object suitable for data compression.

Return type:

CompressorInterface

abstractmethod new_decompressor_stream() DecompressorInterface
Returns:

an object suitable for data decompression.

Return type:

DecompressorInterface

final serialize(packet: _T_SentDTOPacket) bytes

Serializes packet and returns the compressed data parts.

See AbstractPacketSerializer.serialize() documentation for details.

Return type:

bytes

final incremental_serialize(packet: _T_SentDTOPacket) Generator[bytes]

Serializes packet and yields the compressed data parts.

See AbstractIncrementalPacketSerializer.incremental_serialize() documentation for details.

Return type:

Generator[bytes]

final deserialize(data: bytes) _T_ReceivedDTOPacket

Decompresses data and returns the deserialized packet.

See AbstractPacketSerializer.deserialize() documentation for details.

Raises:
Return type:

_T_ReceivedDTOPacket

final incremental_deserialize() Generator[None, bytes, tuple[_T_ReceivedDTOPacket, bytes]]

Yields until data decompression is finished and deserializes the decompressed data using the underlying serializer.

See AbstractIncrementalPacketSerializer.incremental_deserialize() documentation for details.

Raises:
Return type:

Generator[None, bytes, tuple[_T_ReceivedDTOPacket, bytes]]

final create_deserializer_buffer(sizehint: int) memoryview

See BufferedIncrementalPacketSerializer.create_deserializer_buffer() documentation for details.

Return type:

memoryview

final buffered_incremental_deserialize(buffer: memoryview) Generator[None, int, tuple[_T_ReceivedDTOPacket, TypeAliasForwardRef('bytes | bytearray | memoryview | collections.abc.Buffer')]]

Yields until data decompression is finished and deserializes the decompressed data using the underlying serializer.

See BufferedIncrementalPacketSerializer.buffered_incremental_deserialize() documentation for details.

Raises:
Return type:

Generator[None, int, tuple[_T_ReceivedDTOPacket, TypeAliasForwardRef(‘bytes | bytearray | memoryview | collections.abc.Buffer’)]]

property debug: bool

The debug mode flag. Read-only attribute.

protocol easynetwork.serializers.wrapper.compressor.CompressorInterface

Bases: Protocol

A compressor object.

Classes that implement this protocol must have the following methods / attributes:

abstractmethod compress(data: bytes | bytearray | memoryview | collections.abc.Buffer, /) bytes

Provide data to the compressor object.

Returns:

a chunk of compressed data if possible, or an empty byte string otherwise.

Return type:

bytes

abstractmethod flush() bytes

Finish the compression process.

The compressor object may not be used after this method has been called.

Returns:

the compressed data left in internal buffers.

Return type:

bytes

protocol easynetwork.serializers.wrapper.compressor.DecompressorInterface

Bases: Protocol

A decompressor object.

Classes that implement this protocol must have the following methods / attributes:

abstractmethod decompress(data: bytes | bytearray | memoryview | collections.abc.Buffer, /) bytes

Decompress data (a bytes-like object).

Some of data may be buffered internally, for use in later calls to decompress(). The returned data should be concatenated with the output of any previous calls to decompress().

Returns:

uncompressed data as bytes.

Return type:

bytes

abstract property eof: bool

True if the end-of-stream marker has been reached.

abstract property unused_data: bytes

Data found after the end of the compressed stream.

If this attribute is accessed before the end of the stream has been reached, its value will be b''.