Codecs

Codec implementations

This package imports the codecs that can be used for de- and encoding incoming and outgoing messages:

All codecs should implement the base class Codec.

Most of this code is taken and adapted from Stefan Scherfkes aiomas: https://gitlab.com/sscherfke/aiomas/

class mango.messages.codecs.Codec[source]

Bases: object

Base class for all Codecs.

Subclasses must implement encode() and decode().

add_serializer(otype, serialize, deserialize, type_id=None)[source]

Add methods to serialize and deserialize objects typed otype.

This can be used to de-/encode objects that the codec otherwise couldn’t encode.

serialize will receive the unencoded object and needs to return an encodable serialization of it.

deserialize will receive an objects representation and should return an instance of the original object.

decode(data)[source]

Decode data from bytes to the original data structure.

deserialize_obj(obj_repr)[source]

Deserialize the original object from obj_repr.

encode(data)[source]

Encode the given data and return a bytes object.

make_type_id(otype)[source]

Create a type id for otype using: - type name - function names in the class - signature of the class and return a 32 bit integer type id.

serialize_obj(obj)[source]

Serialize obj to something that the codec can encode.

exception mango.messages.codecs.DecodeError[source]

Bases: Exception

Raised when an object representation can not be decoded.

class mango.messages.codecs.JSON[source]

Bases: Codec

A Codec that uses JSON to encode and decode messages.

decode(data)[source]

Decode data from bytes to the original data structure.

encode(data)[source]

Encode the given data and return a bytes object.

class mango.messages.codecs.PROTOBUF[source]

Bases: Codec

decode(data)[source]

Decode data from bytes to the original data structure.

encode(data)[source]

Encode the given data and return a bytes object.

register_proto_type(proto_class)[source]
serialize_obj(obj)[source]

Serialize obj to something that the codec can encode.

exception mango.messages.codecs.SerializationError[source]

Bases: Exception

Raised when an object cannot be serialized.

mango.messages.codecs.json_serializable(cls=None, repr=True)[source]

This is a direct copy from aiomas: https://gitlab.com/sscherfke/aiomas/-/blob/master/src/aiomas/codecs.py

Class decorator that makes the decorated class serializable by the json codec (or any codec that can handle python dictionaries).

The decorator tries to extract all arguments to the class’ __init__(). That means, the arguments must be available as attributes with the same name.

The decorator adds the following methods to the decorated class:

  • __asdict__(): Returns a dict with all __init__ parameters

  • __fromdict__(dict): Creates a new class instance from dict

  • __serializer__(): Returns a tuple with args for Codec.add_serializer()

  • __repr__(): Returns a generic instance representation. Adding this method can be deactivated by passing repr=False to the decorator.