def bufferize(worker: AbstractWorker, protocol: "Protocol") -> ProtocolPB: """ This function takes the attributes of a Protocol and saves them in a Protobuf message Args: worker (AbstractWorker): the worker doing the serialization protocol (Protocol): a Protocol object Returns: ProtocolPB: a Protobuf message holding the unique attributes of the Protocol object """ if not protocol.is_built: raise RuntimeError("A Protocol needs to be built before being serialized.") protobuf_protocol = ProtocolPB() sy.serde.protobuf.proto.set_protobuf_id(protobuf_protocol.id, protocol.id) protobuf_protocol.name = protocol.name for role_id, role in protocol.roles.items(): protobuf_protocol.roles.get_or_create(role_id).CopyFrom( sy.serde.protobuf.serde._bufferize(worker, role) ) protobuf_protocol.tags.extend(protocol.tags) if protocol.description: protobuf_protocol.description = protocol.description return protobuf_protocol
def bufferize(worker: BaseWorker, protocol: "Protocol") -> ProtocolPB: """ This function takes the attributes of a Protocol and saves them in protobuf ProtocolPB Args: worker (BaseWorker) : the worker doing the serialization protocol (Protocol): a Protocol object Returns: ProtocolPB: a protobuf object holding the unique attributes of the Protocol object Raises: TypeError: if a plan is not sy.Plan or sy.PointerPlan """ pb_protocol = ProtocolPB() for worker, plan in protocol.plans: if isinstance(plan, sy.Plan): plan_id = plan.id elif isinstance(plan, sy.PointerPlan): plan_id = plan.id_at_location else: raise TypeError("This is not a valid Plan") if isinstance(worker, str): worker_id = worker else: worker_id = worker.id plan_assignment = pb_protocol.plan_assignments.add() sy.serde.protobuf.proto.set_protobuf_id(plan_assignment.worker_id, worker_id) sy.serde.protobuf.proto.set_protobuf_id(plan_assignment.plan_id, plan_id) sy.serde.protobuf.proto.set_protobuf_id(pb_protocol.id, protocol.id) if protocol.tags: pb_protocol.tags.extend(protocol.tags) if protocol.description: pb_protocol.description = protocol.description pb_protocol.workers_resolved = protocol.workers_resolved return pb_protocol