def delete_node(self, *args): """arg should be like this: .//managedObject[@class="LNCEL"][@distName="0"]/list/item/p[@name="dFpucchF1b"] """ for each_node in args: each_node = self._re_config_path(each_node) self._log.debug("new_node_path:%s", each_node) elements = self._xml_inst.get_elements(self._xml_parser, each_node) if not elements: raise TAFileException( "not found any elements, please check your xpath!--%s." % each_node) for ele in elements: self._xml_inst.remove_elements(self._xml_parser, each_node)
def modify_node_text(self, *args): """arg should be like this: './/managedObject[@class="LNBTS"]/p[@name="actDLCAggr"]:false' """ for each_node in args: ret_temp = each_node.split(":") node_path = ":".join(ret_temp[:-1]) node_path = self._re_config_path(node_path) value = ret_temp[-1] self._log.debug("node_path:%s, value:%s", node_path, value) elements = self._xml_inst.get_elements(self._xml_parser, node_path) if not elements: raise TAFileException( "not found any elements, please check your xpath!--%s." % node_path) for ele in elements: self._xml_inst.set_element_text(ele, value)
def get_node_attribute(self, *args): """arg should be like this: .//managedObject[@class\="LNCEL"]:@version """ ret_list = [] for arg in args: attr_name = arg.split(':@')[-1] each_node = self._re_config_path(arg.split(':@')[0]) elements = self._xml_inst.get_elements(self._xml_parser, each_node) if not elements: raise TAFileException( "not found any elements, please check your xpath!--%s." % each_node) for ele in elements: ret_list.append( self._xml_inst.get_element_attribute(ele, attr_name)) return ret_list
def delete_node(self, *args): """arg should be like this: 0x10049 """ total_line = len(self._txt_data) for each_node in args: param = each_node self._log.debug("try to delete param:'%s'", param) deleted = False for i in range(total_line): assignment = self._txt_data[i].split("#")[0] get_param = assignment.split("=")[0].strip() if param == get_param: self._txt_data[i] = "" deleted = True if deleted is not True: raise TAFileException("not found any value for param %s, please check your param!" % param)
def find_tag_position(self, tagname): """location the tag lines """ pos_value_list = [] target_pos = 0 get_tag = b'' get_value = b'' read_tag = False read_value = False self._file_object.seek(-990, 2) while True: try: ch = self._file_object.read(1) single = struct.unpack('c', ch)[0] if single == '<'.encode('utf-8'): read_tag = True read_value = False if len(get_value) != 0: target_pos = self._file_object.tell() - len( get_value) - 1 pos_value_list.append((target_pos, get_value)) self._log.info("find tag '%s' at 'pos': %s", tagname, target_pos) break elif single == '>'.encode('utf-8'): read_tag = False get_tag = b'' elif read_tag is True: get_tag += single if get_tag == tagname.encode('utf-8'): read_value = True else: read_value = False elif read_value is True and read_tag is False: get_value += single except struct.error: self._log.info("end of file") break if len(pos_value_list) == 0: raise TAFileException("Not find tag '%s'" % tagname) else: return pos_value_list
def get_csv_columns_list(self, *cared_title): """to get the columns with the cared titles """ csv_columns_list = [] self.get_valid_line_lists() for title in cared_title: if title not in self.title_list: raise TAFileException("Title '%s' cann't be find." % title) else: column_pos = self.title_list.index(title) column_list = [] for valid in self.valid_line_list: value = valid[column_pos].strip() if re.match('^\d+$', value): value = int(value) elif re.match('^\d+.\d+$', value): value = float(value) column_list.append(value) csv_columns_list.append(column_list) result_list = [csv_columns_list, csv_columns_list[0]][len(csv_columns_list) == 1] return result_list
def modify_node(self, *args): """arg should be like this: 0x10042:0x0A691892 0x10043:51015 """ total_line = len(self._txt_data) for each_node in args: ret_temp = each_node.split(":") param = ret_temp[0] value = ":".join(ret_temp[1:]) self._log.debug("try to modify param:'%s', value:'%s'", param, value) modified = False comment = '' for i in range(total_line): assignment = self._txt_data[i].split("#")[0] comment = self._txt_data[i][self._txt_data[i].find("#")+1:] get_param = assignment.split("=")[0].strip() if param == get_param: if comment == self._txt_data[i] or comment.strip() == '': self._txt_data[i] = "%s = %s\n" % (param, value) else: self._txt_data[i] = "%s = %s #%s" % (param, value, comment) modified = True if modified is not True: raise TAFileException("not found any value for param %s, please check your param!" % param)