示例#1
0
def message_event_schema_deserialization(
        message_event_dict: Dict[str, Any],
        parent: Dict[str, Any]) -> Optional[Schema]:
    # pylint: disable=unused-argument
    message_type = message_event_dict.get("_type")
    if message_type is None:
        return None

    if message_type.endswith("SendLockExpired"):
        from raiden.transfer.mediated_transfer.events import SendLockExpired

        return SchemaCache.get_or_create_schema(SendLockExpired)
    elif message_type.endswith("SendLockedTransfer"):
        from raiden.transfer.mediated_transfer.events import SendLockedTransfer

        return SchemaCache.get_or_create_schema(SendLockedTransfer)
    elif message_type.endswith("SendSecretReveal"):
        from raiden.transfer.mediated_transfer.events import SendSecretReveal

        return SchemaCache.get_or_create_schema(SendSecretReveal)
    elif message_type.endswith("SendBalanceProof"):
        from raiden.transfer.mediated_transfer.events import SendBalanceProof

        return SchemaCache.get_or_create_schema(SendBalanceProof)
    elif message_type.endswith("SendSecretRequest"):
        from raiden.transfer.mediated_transfer.events import SendSecretRequest

        return SchemaCache.get_or_create_schema(SendSecretRequest)
    elif message_type.endswith("SendRefundTransfer"):
        from raiden.transfer.mediated_transfer.events import SendRefundTransfer

        return SchemaCache.get_or_create_schema(SendRefundTransfer)

    elif message_type.endswith("SendWithdrawRequest"):
        from raiden.transfer.events import SendWithdrawRequest

        return SchemaCache.get_or_create_schema(SendWithdrawRequest)

    elif message_type.endswith("SendWithdrawConfirmation"):
        from raiden.transfer.events import SendWithdrawConfirmation

        return SchemaCache.get_or_create_schema(SendWithdrawConfirmation)

    elif message_type.endswith("SendWithdrawExpired"):
        from raiden.transfer.events import SendWithdrawExpired

        return SchemaCache.get_or_create_schema(SendWithdrawExpired)

    elif message_type.endswith("SendProcessed"):
        from raiden.transfer.events import SendProcessed

        return SchemaCache.get_or_create_schema(SendProcessed)

    return None
示例#2
0
def balance_proof_schema_deserialization(
        balance_proof_dict: Dict[str, Any],
        parent: Dict[str, Any]) -> Optional[Schema]:
    # pylint: disable=unused-argument
    bp_type = balance_proof_dict.get("_type")
    if bp_type is None:
        return None

    if bp_type.endswith("UnsignedState"):
        return SchemaCache.get_or_create_schema(BalanceProofUnsignedState)
    elif bp_type.endswith("SignedState"):
        return SchemaCache.get_or_create_schema(BalanceProofSignedState)

    return None
示例#3
0
 def deserialization_schema_selector(self, deserializable_dict: Dict[str,
                                                                     Any],
                                     parent: Dict[str, Any]) -> Schema:
     # pylint: disable=unused-argument
     type_ = deserializable_dict["_type"].split(".")[-1]
     return SchemaCache.get_or_create_schema(
         self._class_of_classname[type_])
示例#4
0
def transfer_task_schema_deserialization(
        task_dict: Dict[str, Any], parent: Dict[str, Any]) -> Optional[Schema]:
    # pylint: disable=unused-argument
    # Avoid cyclic dependencies
    task_type = task_dict.get("_type")
    if task_type is None:
        return None

    if task_type.endswith("InitiatorTask"):
        from raiden.transfer.mediated_transfer.tasks import InitiatorTask

        return SchemaCache.get_or_create_schema(InitiatorTask)
    if task_type.endswith("MediatorTask"):
        from raiden.transfer.mediated_transfer.tasks import MediatorTask

        return SchemaCache.get_or_create_schema(MediatorTask)
    if task_type.endswith("TargetTask"):
        from raiden.transfer.mediated_transfer.tasks import TargetTask

        return SchemaCache.get_or_create_schema(TargetTask)

    return None
示例#5
0
    def serialize(obj: Any) -> Dict:
        # Default, in case this is not a dataclass
        data = obj
        if is_dataclass(obj):
            try:
                schema = SchemaCache.get_or_create_schema(obj.__class__)
                data = schema.dump(obj)
            except (TypeError, ValidationError, ValueError) as ex:
                raise SerializationError(f"Can't serialize: {data}") from ex
        elif not isinstance(obj, Mapping):
            raise SerializationError(
                f"Can only serialize dataclasses or dict-like objects: {obj}")

        return data
示例#6
0
    def deserialize(data: Dict) -> Any:
        """ Deserialize a dict-like object.

        If the key ``_type`` is present, import the target and deserialize via Marshmallow.
        Raises ``SerializationError`` for invalid inputs.
        """
        if not isinstance(data, Mapping):
            raise SerializationError(
                f"Can't deserialize non dict-like objects: {data}")
        if "_type" in data:
            try:
                klass = _import_type(data["_type"])
                schema = SchemaCache.get_or_create_schema(klass)
                return schema.load(deepcopy(data))
            except (ValueError, TypeError, ValidationError) as ex:
                raise SerializationError(f"Can't deserialize: {data}") from ex
        return data
示例#7
0
 def serialization_schema_selector(obj: Any, parent: Any) -> Schema:
     # pylint: disable=unused-argument
     return SchemaCache.get_or_create_schema(obj.__class__)
示例#8
0
def transfer_task_schema_serialization(task: TransferTask,
                                       parent: Any) -> Schema:
    # pylint: disable=unused-argument
    return SchemaCache.get_or_create_schema(task.__class__)
示例#9
0
def message_event_schema_serialization(message_event: SendMessageEvent,
                                       parent: Any) -> Schema:
    # pylint: disable=unused-argument
    return SchemaCache.get_or_create_schema(message_event.__class__)
示例#10
0
def balance_proof_schema_serialization(balance_proof: Union[
    BalanceProofSignedState, BalanceProofUnsignedState],
                                       parent: Any) -> Schema:
    # pylint: disable=unused-argument
    return SchemaCache.get_or_create_schema(balance_proof.__class__)