def de_serialize(msg, datatypes): """ De serializes data of the message, represented as bytes into an object of az type defined using the mimetype/datatype of the metadata Args: msg (xMsgMessage): message to be deserialized datatypes (set(<EngineDataType>): datatype set of permitted serializations Returns: EngineData: """ assert isinstance(msg, xMsgMessage) for dt in datatypes: if dt.mimetype == msg.metadata.dataType: try: engine_data = EngineData() engine_data.metadata = msg.metadata engine_data.set_data(msg.metadata.dataType, dt.serializer.read(msg.data)) return engine_data except Exception as e: raise ClaraException( "Clara-Error: Could not serialize. %s" % e.message) if msg.metadata.dataType == Mimetype.STRING: engine_data = EngineData() engine_data.metadata = msg.metadata engine_data.set_data( Mimetype.STRING, EngineDataType.STRING().serializer.read(msg.data)) return engine_data raise ClaraException("Clara-Error: Unsopported mimetype = %s" % msg.metadata.dataType)
def serialize(topic, engine_data, datatypes): """Builds a message by serializing passed data object using serialization routine defined in one of the data types objects Args: topic (xMsgTopic): the topic where data will be published engine_data (EngineData): the data to be serialized datatypes (set(<EngineDataType>)): the set of registered dataTypes Returns: xMsgMessage: Message with serialized data """ assert isinstance(engine_data, EngineData) for dt in datatypes: if dt.mimetype == engine_data.mimetype: msg = xMsgMessage() msg.topic = str(topic) msg.metadata = engine_data.metadata msg.data = dt.serializer.write(engine_data.get_data()) return msg if engine_data.mimetype == Mimetype.STRING: msg = xMsgMessage() msg.topic = str(topic) msg.metadata = engine_data.metadata msg.data = EngineDataType.STRING().serializer.write( engine_data.get_data()) return msg
def test_proper_construction_of_types(self): SINT32 = EngineDataType.SINT32() self.assertEqual(SINT32.mimetype, Mimetype.SINT32) self.assertIsInstance(SINT32.serializer, PrimitiveSerializer) SINT64 = EngineDataType.SINT64() self.assertEqual(SINT64.mimetype, Mimetype.SINT64) self.assertIsInstance(SINT64.serializer, PrimitiveSerializer) SFIXED32 = EngineDataType.SFIXED32() self.assertEqual(SFIXED32.mimetype, Mimetype.SFIXED32) self.assertIsInstance(SFIXED32.serializer, PrimitiveSerializer) SFIXED64 = EngineDataType.SFIXED64() self.assertEqual(SFIXED64.mimetype, Mimetype.SFIXED64) self.assertIsInstance(SFIXED64.serializer, PrimitiveSerializer) FLOAT = EngineDataType.FLOAT() self.assertEqual(FLOAT.mimetype, Mimetype.FLOAT) self.assertIsInstance(FLOAT.serializer, PrimitiveSerializer) DOUBLE = EngineDataType.DOUBLE() self.assertEqual(DOUBLE.mimetype, Mimetype.DOUBLE) self.assertIsInstance(DOUBLE.serializer, PrimitiveSerializer) STRING = EngineDataType.STRING() self.assertEqual(STRING.mimetype, Mimetype.STRING) self.assertIsInstance(STRING.serializer, PrimitiveSerializer) ASINT32 = EngineDataType.ARRAY_SINT32() self.assertEqual(ASINT32.mimetype, Mimetype.ARRAY_SINT32) self.assertIsInstance(ASINT32.serializer, PrimitiveSerializer) ASINT64 = EngineDataType.ARRAY_SFIXED64() self.assertEqual(ASINT64.mimetype, Mimetype.ARRAY_SFIXED64) self.assertIsInstance(ASINT64.serializer, PrimitiveSerializer) ASFIXED32 = EngineDataType.ARRAY_SFIXED32() self.assertEqual(ASFIXED32.mimetype, Mimetype.ARRAY_SFIXED32) self.assertIsInstance(SFIXED32.serializer, PrimitiveSerializer) ASFIXED64 = EngineDataType.ARRAY_SFIXED64() self.assertEqual(ASFIXED64.mimetype, Mimetype.ARRAY_SFIXED64) self.assertIsInstance(ASFIXED64.serializer, PrimitiveSerializer) AFLOAT = EngineDataType.ARRAY_FLOAT() self.assertEqual(AFLOAT.mimetype, Mimetype.ARRAY_FLOAT) self.assertIsInstance(AFLOAT.serializer, PrimitiveSerializer) ADOUBLE = EngineDataType.ARRAY_DOUBLE() self.assertEqual(ADOUBLE.mimetype, Mimetype.ARRAY_DOUBLE) self.assertIsInstance(ADOUBLE.serializer, PrimitiveSerializer) ASTRING = EngineDataType.ARRAY_STRING() self.assertEqual(ASTRING.mimetype, Mimetype.ARRAY_STRING) self.assertIsInstance(ASTRING.serializer, PrimitiveSerializer)
def get_output_data_types(self): return ClaraUtils.build_data_types(EngineDataType.STRING())
def get_input_data_types(self): return [EngineDataType.ARRAY_STRING(), EngineDataType.STRING()]