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 _parsed_linked(self): """Parses composition field of the transient data and returns the list of services output linked to this service, i.e. that are getting output data of this service. Attention: service name CAN NOT appear twice in the composition. """ element_list = [] statement_iterator = iter(self._statement_string.split("+")) while True: try: element = statement_iterator.next() element = remove_first(element, "&") element = remove_first(element, "{") element_list.append(element) except StopIteration: break index = -1 for element in element_list: index += 1 if self._service_name in element: break if index == -1: raise ClaraException("Routing statement parsing exception. " + "Service name can not be found in the " + "statement.") else: p_index = index - 1 if p_index >= 0: element = Statement._get_element(element_list, p_index) if "," in element: element = element.split(",") statement_iterator = iter(element) while True: try: self._input_links.add(statement_iterator.next()) except StopIteration: break else: self._input_links.add(element) n_index = index + 1 if len(element_list) > n_index: element = Statement._get_element(element_list, n_index) if "," in element: element = element.split(",") statement_iterator = iter(element) while True: try: self._output_links.add(statement_iterator.next()) except StopIteration: break else: self._output_links.add(element)
def _pre_process(code_string): if code_string.find(";") == -1 and not code_string.endswith(";"): raise ClaraException("Syntax error in the Clara routing program." + "Missing end of statement operator = \";\"") instruction_set = [] for text in code_string.split(";"): if text != "" and text != "}": instruction_set.append(text) return instruction_set
def __init__(self, statement_string, service_name): if service_name in statement_string: self._service_name = service_name self._statement_string = statement_string self._input_links = set() self._output_links = set() self._log_and_inputs = {} self._process() else: raise ClaraException("Irrelevant statement")
def _parse_condition(self, condition): pattern = re.compile(Regex.CONDITION) match = pattern.match(condition) if match: try: index = condition.find("{") + 1 statement_string = condition[index:] if not (self._service_name in statement_string): return None ts = Statement(statement_string, self._service_name) ti = Instruction(self._service_name) if condition.startswith("if(") or condition.startswith("}if("): par1 = condition.find("(") + 1 par2 = condition.rfind(")") tc = Condition(condition[par1:par2], self._service_name) ti.if_condition = tc ti.if_statements.add(ts) elif condition.startswith("}elseif("): par1 = condition.find("(") + 1 par2 = condition.rfind(")") tc = Condition(condition[par1:par2], self._service_name) ti.elseif_condition = tc ti.elseif_statements.add(ts) elif condition.startswith("}else"): ti.else_statements.add(ts) return ti except Exception as e: print e raise ClaraException(self._SYNTAX_ERROR) else: raise ClaraException(self._SYNTAX_ERROR)
def _parse_statement(self, statement_string): ti = Instruction(self._service_name) statement_string = remove_first(statement_string, "}") pattern = re.compile(Regex.ROUTING_STATEMENT) match = pattern.match(statement_string) if match: if not (self._service_name in statement_string): return False ts = Statement(statement_string, self._service_name) ti.unconditional_statements.add(ts) self._instructions.add(ti) return True else: raise ClaraException(self._SYNTAX_ERROR)
def _parse_conditional_statement(self, statement_string, instruction): pattern = re.compile(Regex.ROUTING_STATEMENT) match = pattern.match(statement_string) if match: if not (self._service_name in statement_string): return False ts = Statement(statement_string, self._service_name) if instruction.if_condition: instruction.if_statements.add(ts) elif instruction.elseif_statements: instruction.elseif_statements.add(ts) else: instruction.else_statements.add(ts) return True else: raise ClaraException(self._SYNTAX_ERROR)
def compile(self, composition): self._instructions.clear() ppi = list(self._pre_process(CCompiler._no_blanks(composition))) i = 0 while i < len(ppi): scs1 = ppi[i] if (scs1.startswith("if(") or scs1.startswith("}if(") or scs1.startswith("}else") or scs1.startswith("}elseif(")): instruction = self._parse_condition(scs1) j = i + 1 while j < len(ppi): scs2 = ppi[j] if (not scs2.startswith("}") and not scs2.startswith("if(") and not scs2.startswith("}if(") and not scs2.startswith("}elseif(") and not scs2.startswith("}else")): if instruction: self._parse_conditional_statement( scs2, instruction) else: i = j - 1 break j += 1 if instruction: self._instructions.add(instruction) else: self._parse_statement(scs1) i += 1 if not bool(self._instructions): raise ClaraException("Composition is irrelevant for a service.")
def _parse_condition(self, condition_string, operator): if operator: if condition_string.find("&&") != -1 and\ condition_string.find("!!") == -1: iterator = iter(condition_string.split(operator)) while True: try: ac = iterator.next() pattern = re.compile(Regex.COMPLEX_CONDITION) match = pattern.match(ac) if match: if ac.find("!=") != -1: condition = ac.next() condition = condition.split("!=") if len(condition) != 2: raise ClaraException(self._CONDITION_ERROR) self.and_not_states.add( ServiceState(condition[0], condition[1])) elif ac.find("==") != -1: condition = ac.next() condition = condition.split("==") if len(condition) != 2: raise ClaraException(self._CONDITION_ERROR) self.and_states.add( ServiceState(condition[0], condition[1])) else: raise ClaraException(self._CONDITION_ERROR) else: raise ClaraException(self._CONDITION_ERROR) except StopIteration: break elif (condition_string.find("!!") != -1 and condition_string.find("&&") == -1): iterator = iter(condition_string.split(operator)) while True: try: ac = iterator.next() pattern = re.compile(Regex.SIMPLE_CONDITION) match = pattern.match(ac) if match: if ac.find("!=") != -1: condition = ac.next() condition = condition.split("!=") if len(condition) != 2: raise ClaraException(self._CONDITION_ERROR) self.or_not_states.add( ServiceState(condition[0], condition[1])) elif ac.find("==") != -1: condition = ac.next() condition = condition.split("==") if len(condition) != 2: raise ClaraException(self._CONDITION_ERROR) self.or_states.add( ServiceState(condition[0], condition[1])) else: raise ClaraException(self._CONDITION_ERROR) else: raise ClaraException(self._CONDITION_ERROR) except StopIteration: break else: raise ClaraException(self._CONDITION_ERROR) else: pattern = re.compile(Regex.SIMPLE_CONDITION) match = pattern.match(condition_string) if match: if condition_string.find("!=") != -1: condition = condition_string.split("!=") if len(condition) != 2: raise ClaraException(self._CONDITION_ERROR) self.or_not_states.add( ServiceState(condition[0], condition[1])) elif condition_string.find("==") != -1: condition = condition_string.split("==") if len(condition) != 2: raise ClaraException(self._CONDITION_ERROR) self.or_states.add(ServiceState(condition[0], condition[1])) else: raise ClaraException(self._CONDITION_ERROR) else: raise ClaraException(self._CONDITION_ERROR)
def sync_run(self, timeout_in_ms): try: response = self.__base.sync_send(self.__frontend, timeout_in_ms) return self.parse_data(response) except Exception as e: raise ClaraException("cannot send message: %s" % e)
def run(self): try: self.__base.send(self.__frontend, self.msg()) except Exception as e: raise ClaraException("cannot send message: %s" % e)
def __create_message(topic, data): try: return xMsgMessage(topic, str(data)) except Exception as e: raise ClaraException("Cannot create message: " + e)