1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3 4# 5# Copyright (c) 2024 Huawei Device Co., Ltd. 6# Licensed under the Apache License, Version 2.0 (the "License"); 7# you may not use this file except in compliance with the License. 8# You may obtain a copy of the License at 9# 10# http://www.apache.org/licenses/LICENSE-2.0 11# 12# Unless required by applicable law or agreed to in writing, software 13# distributed under the License is distributed on an "AS IS" BASIS, 14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15# See the License for the specific language governing permissions and 16# limitations under the License. 17# 18 19def get_sub_str(input_str, keyword, value_separator_count=0, is_last=False): 20 if input_str is None or keyword is None: 21 return None 22 index = input_str.find(keyword) 23 if index == -1: 24 return None 25 start_index = index + len(keyword) 26 if is_last: 27 end_index = input_str.find("\n", start_index) 28 if end_index == -1: 29 end_index = len(input_str) 30 else: 31 if value_separator_count == 0: 32 end_index = input_str.find(",", start_index) 33 if end_index == -1: 34 # we are reaching the end 35 end_index = len(input_str) 36 else: 37 # 查找 value_separator_count 数量的逗号 38 temp_start_index = start_index 39 for i in range(value_separator_count): 40 temp_start_index = input_str.find(",", temp_start_index) + 1 41 end_index = input_str.find(",", temp_start_index) 42 if end_index == -1: 43 print("no result for ", keyword, " in input string: ", input_str) 44 return None 45 sub_str = input_str[start_index:end_index] 46 return sub_str 47 48 49# 从给定的字符串中查找给定的关键字,并将该关键字后面直到逗号或行尾之前的内容转换为数字之后返回 50# value_separator_count: the count of comma in value 51def get_value_as_int(input_str, keyword, value_separator_count=0, is_last=False): 52 sub_str = get_sub_str(input_str, keyword, value_separator_count, is_last) 53 if sub_str is None: 54 return None 55 return int(sub_str) 56 57 58def get_value_as_float(input_str, keyword, value_separator_count=0, is_last=False): 59 sub_str = get_sub_str(input_str, keyword, value_separator_count, is_last) 60 if sub_str is None: 61 return None 62 return float(sub_str) 63 64 65def get_value_as_str(input_str, keyword, value_separator_count=0, is_last=False): 66 sub_str = get_sub_str(input_str, keyword, value_separator_count, is_last) 67 if sub_str is None: 68 return None 69 return sub_str 70 71 72def pack_string_until_next_keyword(texts, start_index, start_keyword, end_keywords): 73 if texts is None or start_keyword is None or end_keywords is None: 74 return None 75 if len(texts) == 0 or len(start_keyword) == 0 or len(end_keywords) == 0: 76 return None 77 if start_index < 0 or start_index >= len(texts): 78 return None 79 current_index = 0 80 pack_result = "" 81 is_start_found = False 82 for line in texts: 83 if current_index < start_index: 84 current_index += 1 85 continue 86 reach_end = False 87 line = line.rstrip("\n") 88 if (is_start_found == False) and line.find(start_keyword) != -1: 89 is_start_found = True 90 pack_result = line 91 current_index += 1 92 continue 93 for keyword in end_keywords: 94 if line.find(keyword) != -1: 95 reach_end = True 96 break 97 if reach_end: 98 break 99 pack_result = f'{pack_result}\n{line}' 100 current_index += 1 101 return [pack_result, current_index] 102