Ejemplo n.º 1
0
def message_from_dict(data: Dict[str, Any]) -> Message:
    if "_type" not in data:
        raise InvalidProtocolMessage(
            "Invalid message data. Can not find the data type") from None
    if data["_type"] not in VALID_MESSAGE_TYPES:
        raise InvalidProtocolMessage(
            'Invalid message type (data["type"] = {})'.format(
                data["_type"])) from None

    return DictSerializer.deserialize(data)
Ejemplo n.º 2
0
def message_from_dict(data: dict) -> Message:
    try:
        klass: Message = CLASSNAME_TO_CLASS[data["type"]]
    except KeyError:
        if "type" in data:
            raise InvalidProtocolMessage(
                'Invalid message type (data["type"] = {})'.format(data["type"])
            ) from None
        raise InvalidProtocolMessage("Invalid message data. Can not find the data type") from None

    return klass.from_dict(data)
Ejemplo n.º 3
0
def from_dict(data: dict) -> 'Message':
    try:
        klass = CLASSNAME_TO_CLASS[data['type']]
    except KeyError:
        if 'type' in data:
            raise InvalidProtocolMessage(
                'Invalid message type (data["type"] = {})'.format(
                    data['type']), ) from None
        else:
            raise InvalidProtocolMessage(
                'Invalid message data. Can not find the data type', ) from None
    return klass.from_dict(data)
Ejemplo n.º 4
0
def decode(data):
    try:
        klass = CMDID_TO_CLASS[data[0]]
    except KeyError:
        raise InvalidProtocolMessage(
            'Invalid message type (data[0] = {})'.format(hex(data[0])))
    return klass.decode(data)
Ejemplo n.º 5
0
    def __init__(self, data):
        if len(data) < size:
            raise InvalidProtocolMessage(
                'data buffer has less than the expected size {}'.format(size),
            )

        object.__setattr__(self, 'data', data)
Ejemplo n.º 6
0
def from_dict(data):
    try:
        klass = CLASSNAME_TO_CLASS[data['type']]
    except KeyError:
        raise InvalidProtocolMessage(
            'Invalid message type (data["type"] = {})'.format(data['type']),
        ) from None
    return klass.from_dict(data)