Skip to content

Interfaces

blake2signer.interfaces

Interfaces: abstract classes to define serializers, encoders and compressors.

CompressorInterface

Compressor interface.

Implement your own compressor inheriting from this class.

default_compression_level: int property readonly

Get the default compression level.

compress(self, data, *, level)

Compress given data.

Parameters:

Name Type Description Default
data bytes

Data to compress.

required
level int

Desired compression level.

required

Returns:

Type Description
bytes

Raw compressed data.

Source code in blake2signer/interfaces.py
@abstractmethod
def compress(self, data: bytes, *, level: int) -> bytes:
    """Compress given data.

    Args:
        data: Data to compress.
        level: Desired compression level.

    Returns:
        Raw compressed data.
    """

decompress(self, data)

Decompress given compressed data.

Parameters:

Name Type Description Default
data bytes

Compressed data to decompress.

required

Returns:

Type Description
bytes

Original data.

Source code in blake2signer/interfaces.py
@abstractmethod
def decompress(self, data: bytes) -> bytes:
    """Decompress given compressed data.

    Args:
        data: Compressed data to decompress.

    Returns:
        Original data.
    """

get_compression_level(self, level)

Return compression level for the compressor.

It correctly converts the scale (if necessary), and the default value for None.

Parameters:

Name Type Description Default
level Optional[int]

Desired compression level from 1 (least compressed) to 9 (most compressed) or None for the default.

required

Returns:

Type Description
int

Correct compression level for the compressor.

Source code in blake2signer/interfaces.py
def get_compression_level(self, level: typing.Optional[int]) -> int:
    """Return compression level for the compressor.

    It correctly converts the scale (if necessary), and the default value
    for None.

    Args:
        level: Desired compression level from 1 (least compressed) to 9 (most
            compressed) or None for the default.

    Returns:
        Correct compression level for the compressor.
    """
    if level is None:
        return self.default_compression_level

    if level < 1 or level > 9:
        raise errors.CompressionError('compression level must be between 1 and 9')

    return self.scale_compression_level(level)

scale_compression_level(self, level)

Scale the compression level to from 1 to 9 to a value for the compressor.

Override this method if the compressor requires scaling the level.

Parameters:

Name Type Description Default
level int

Desired compression level from 1 to 9.

required

Returns:

Type Description
int

Scaled compression level for the compressor.

Source code in blake2signer/interfaces.py
def scale_compression_level(self, level: int) -> int:
    """Scale the compression level to from 1 to 9 to a value for the compressor.

    Override this method if the compressor requires scaling the level.

    Args:
        level: Desired compression level from 1 to 9.

    Returns:
        Scaled compression level for the compressor.
    """
    return level

EncoderInterface

Encoder interface.

Implement your own encoder inheriting from this class.

Note

Verify that the encoder alphabet is ASCII (a check is enforced nevertheless).

alphabet: bytes property readonly

Return the encoder alphabet characters.

This is used to validate that separator characters and flags don't belong to this alphabet to prevent malfunctions.

decode(self, data)

Decode given encoded data.

Parameters:

Name Type Description Default
data bytes

Encoded data to decode.

required

Returns:

Type Description
bytes

Original data.

Source code in blake2signer/interfaces.py
@abstractmethod
def decode(self, data: bytes) -> bytes:
    """Decode given encoded data.

    Args:
        data: Encoded data to decode.

    Returns:
        Original data.
    """

encode(self, data)

Encode given data.

Parameters:

Name Type Description Default
data bytes

Data to encode.

required

Returns:

Type Description
bytes

Encoded data.

Source code in blake2signer/interfaces.py
@abstractmethod
def encode(self, data: bytes) -> bytes:
    """Encode given data.

    Args:
        data: Data to encode.

    Returns:
        Encoded data.
    """

SerializerInterface

Serializer interface.

Implement your own serializer inheriting from this class.

serialize(self, data, **kwargs)

Serialize given data.

Parameters:

Name Type Description Default
data Any

Data to serialize.

required
**kwargs Any

Additional arguments for the serializer.

{}

Returns:

Type Description
bytes

Serialized data

Source code in blake2signer/interfaces.py
@abstractmethod
def serialize(self, data: typing.Any, **kwargs: typing.Any) -> bytes:
    """Serialize given data.

    Args:
        data: Data to serialize.
        **kwargs: Additional arguments for the serializer.

    Returns:
        Serialized data
    """

unserialize(self, data, **kwargs)

Unserialize given serialized data.

Parameters:

Name Type Description Default
data bytes

Serialized data to unserialize.

required
**kwargs Any

Additional arguments for the serializer.

{}

Returns:

Type Description
Any

Original data.

Source code in blake2signer/interfaces.py
@abstractmethod
def unserialize(self, data: bytes, **kwargs: typing.Any) -> typing.Any:
    """Unserialize given serialized data.

    Args:
        data: Serialized data to unserialize.
        **kwargs: Additional arguments for the serializer.

    Returns:
        Original data.
    """