def __init__(self, ratio: ObjectifiedElement): self.id = ratio.get('id') self.label = ratio.get('label', ratio.get('id').replace('_', ' ').title()) self.description = next(ratio.itertext()).strip() self.source = self.Sources(a=ratio.compute.get('source.a'), b=ratio.compute.get('source.b'))
def _compute_query_post(compute_result: list, query: ObjectifiedElement) -> float: if not query.get('post'): return compute_result[0] if query.get('post') == 'sum': return sum(compute_result) elif query.get('post') == 'static': return float(query.get('value')) else: raise ValueError(f'Unsupported post: {query.get("post")}')
def xtest_set_payload_direct_payload_case_2a(self): # basestring, dict, list, tuple, bool, Number + (EtreeElement, ObjectifiedElement) data_01 = b'abc' data_02 = u'def' data_03 = [1, 2, 3] data_04 = (5, 6, 7) data_05 = True data_06 = False data_07 = 1 data_08 = 2.0 data_09 = EtreeElement() data_10 = ObjectifiedElement() elems = [ data_01, data_02, data_03, data_04, data_05, data_06, data_07, data_08, data_09, data_10 ] response = Response() response.init('abc', None, DATA_FORMAT.CSV) for elem in elems: response.payload = elem self.assertIs(response.payload, elem)
def get_valid_root(tree: objectify.ObjectifiedElement) -> str: root = tree.getroot() for attr in ("sig", "publickey"): if not hasattr(root, attr): oem = create_element_maker() print( f"{Path(root.base).name}: Element '{attr}' is missing from '{root.tag}'" ) root.append(oem(attr, "Missing '{attr}'", {"ver": 0})) return root
def _format(self, result: ObjectifiedElement, remove_prefix: bool = False) -> Dict[str, Any]: formatted_result = {} for field in result.getchildren(): name = field.tag if remove_prefix: name = self._remove_prefix(name) name = self._underscore(name) formatted_result[name] = field.text return formatted_result
def parse_cu(node: ObjectifiedElement) -> CUNode: n = CUNode(node.get("id")) n.type = NodeType(int(node.get("type"))) n.source_file, n.start_line = parse_id(node.get("startsAtLine")) _, n.end_line = parse_id(node.get("endsAtLine")) n.name = node.get("name") n.instructions_count = node.get("instructionsCount", 0) if hasattr(node, 'funcArguments') and hasattr(node.funcArguments, 'arg'): n.args = [ Variable(v.get('type'), v.text) for v in node.funcArguments.arg ] # TODO recursive calls unused if n.type == NodeType.CU: if hasattr(node.localVariables, 'local'): n.local_vars = [ Variable(v.get('type'), v.text) for v in node.localVariables.local ] if hasattr(node.globalVariables, 'global'): n.global_vars = [ Variable(v.get('type'), v.text) for v in getattr(node.globalVariables, 'global') ] # TODO self.graph.vp.instructionsCount[v] = node.instructionsCount # TODO self.graph.vp.BasicBlockID[v] = node.BasicBlockID return n
def parse_cu(node: ObjectifiedElement) -> CUNode: n = CUNode(node.get("id")) n.type = NodeType(int(node.get("type"))) n.source_file, n.start_line = parse_id(node.get("startsAtLine")) _, n.end_line = parse_id(node.get("endsAtLine")) n.name = node.get("name") n.instructions_count = node.get("instructionsCount", 0) if hasattr(node, 'funcArguments') and hasattr(node.funcArguments, 'arg'): n.args = [Variable(v.get('type'), v.text) for v in node.funcArguments.arg] if hasattr(node, 'callsNode') and hasattr(node.callsNode, 'recursiveFunctionCall'): n.recursive_function_calls = [n.text for n in node.callsNode.recursiveFunctionCall] if n.type == NodeType.CU: if hasattr(node.localVariables, 'local'): n.local_vars = [Variable(v.get('type'), v.text) for v in node.localVariables.local] if hasattr(node.globalVariables, 'global'): n.global_vars = [Variable(v.get('type'), v.text) for v in getattr(node.globalVariables, 'global')] if hasattr(node, 'BasicBlockID'): n.basic_block_id = getattr(node, 'BasicBlockID') if hasattr(node, 'returnInstructions'): n.return_instructions_count = int(getattr(node, 'returnInstructions').get('count')) if hasattr(node.callsNode, 'nodeCalled'): n.node_calls = [{"cuid": v.text, "atLine": v.get('atLine')} for v in getattr(node.callsNode, 'nodeCalled') if v.get('atLine') is not None] return n
def calculate_ratio(cls, ratio: ObjectifiedElement, fact_context: dict[str, float]) -> Metric: try: A = cls.calculate_compute(ratio.compute.get('source.a'), fact_context) B = cls.calculate_compute(ratio.compute.get('source.b'), fact_context) except BaseException: ic(ratio.get('id')) ic(ratio.compute.get('source.a')) ic(ratio.compute.get('source.b')) ic(fact_context) raise return cls.Metric(a=A, b=B, ratio=A / B)
def read_from_xml_node(xml_node: objectify.ObjectifiedElement, attrib_name: str, do_not_warn: bool = False): attribs = xml_node.attrib if attribs: prot = attribs.get(attrib_name) if prot is not None: return prot else: if not do_not_warn: logger.warning(f"There is no attrib with the name: {attrib_name} " f"in a tag {xml_node.tag} of {xml_node.base}") return None else: logger.warning(f"Node {xml_node.tag} of {xml_node.base} is empty!") if xml_node.tag == "comment": log_comment(xml_node, xml_node.getparent()) return None
def check_mono_xml_node(xml_node: objectify.ObjectifiedElement, expected_child_name: str, ignore_comments: bool = False): children = xml_node.getchildren() if len(children) > 0: for child in children: if child.tag != expected_child_name: if child.tag == "comment" and not ignore_comments: comment = unescape(str(etree.tostring(child))).strip("b'<!-- ").strip(" -->'") path = unquote(xml_node.base).replace(f'file:/{WORKING_DIRECTORY}', '') logger.debug(f"Comment '{comment}' " f"in tag: '{xml_node.tag}'' " f"in file: {path}.") else: logger.warning(f"Unexpected node with a name {child.tag} found " f"in xml node: {xml_node.tag} in {xml_node.base}!") else: logger.error(f"Empty node with a name {xml_node.tag} when expecting to find child " f"nodes with a name {expected_child_name} in {xml_node.base}")
def _compute_query(query: ObjectifiedElement, report_data: Report) -> list: if not (source := getattr(report_data, query.get('source'), None)): raise ValueError(f'Unknown source: {query.get("source")}')
def __init__(self, fact: ObjectifiedElement): self.fact = fact self.id = fact.get('id') self.label = fact.get('label', fact.get('id').replace('_', ' ').title())