import linecache def linecount(in_file_path): # 第三种方法放入缓存防止内存过载 count = -1 for count, line in enumerate(open(in_file_path,'rb').readlines()): count += 1 return count #取字符串中两个符号之间的东东,按第一次出现子串进行截取,start_str和end_str为首次出现的 def txt_wrap_by_first(lineString,start_str, end_str): start = lineString.find(start_str) #python提供了从尾部搜索的方法,这种方法也是可行的。start = lineString.rfind(start_str) +2表示跳过:: if start >= 0: start += len(start_str) end = lineString.find(end_str) if end >= 0: return lineString[start:end].strip() #取字符串中两个符号之间的东东,start_str和end_str为最后一次出现的 def txt_wrap_by(lineString,start_str, end_str): start = find_last(lineString,start_str)#python提供了从尾部搜索的方法,这种方法也是可行的。start = lineString.rfind(start_str) if start >= 0: start += len(start_str) #end = lineString.find(end_str) end = find_last(lineString,end_str) if end >= 0: return lineString[start:end].strip() #取字符串中两个符号之间的东东,start_str为首次出现的,end_str为最后一次出现的 def txt_wrap_by_first_last(lineString,start_str,end_str): start = lineString.find(start_str)#python提供了从尾部搜索的方法,这种方法也是可行的。start = lineString.rfind(start_str) if start >= 0: start += len(start_str) end = lineString.rfind(end_str) if end >= 0: return lineString[start:end].strip() #在字符串中寻找目标最后一次出现的位置(python) def find_last(lineString,start_str): last_position=-1 while True: position=lineString.find(start_str,last_position+1) if position==-1: return last_position last_position=position return -1 #取字符串中某个字符之前的东东,直到遇到空格截止 def txt_wrap_before(lineString, end_str): lineSplit=lineString.split(' ')[4] end = lineSplit.find(end_str) if end >= 0: return lineSplit[:end].strip() ''' #逆序从最后一行往前读取文件内容,并写入另一个文件 def get_reverse_line(in_file_path,out_file_path): line_number=linecount() lineString = '' while line_number>0: lineString = lineString + linecache.getline(in_file_path, line_number) #print(lineString) line_number=line_number-1 file = open(out_file_path,'w') file.write(lineString) file.close() #从最后一个字符倒着读取文件内容,并写入另一个文件 def get_reverse_context(in_file_path,out_file_path): with open(in_file_path, "rt") as in_f, open(out_file_path, "wt") as out_f: for line in in_f: line = line.strip() reversed_line = ''.join(reversed(line)) out_f.write(reversed_line + " ") #顺序读取文件内容,并原样写入另一个文件 def get_same_context(in_file_path,out_file_path): in_file = open(in_file_path,'r') lineString = '' for lien in in_file.readlines(): #lien = lien.strip() lineString = lineString + str(lien) #print(str(lien)) #print(lineString) out_file = open(out_file_path,'w') out_file.write(lineString) out_file.close() ''' #顺序读取文件内容,拼接处理后展示最后结果 def get_result(in_file_path,out_file_path): line_number=linecount(in_file_path) #获取文件行数 lineString0 = '' #提取文件名 lineString1 = '' #提取函数名 outString0 = '' #拼接本行RenderThread -> DrawFrameTask: run outString1 = '' #拼接上一行activate DrawFrameTask outString = '@startuml'+' '+'autonumber'+' ' #拼接成最终结果 while line_number-1>0: lineString0 = linecache.getline(in_file_path, line_number) lineString1 = linecache.getline(in_file_path, line_number-1) line_number=line_number-1 outString0 = outString0 + str(txt_wrap_by(lineString0,'/','.'))+' -> '+str(txt_wrap_by(lineString1,'/','.'))+': ' if lineString1.find('.') >= 0: #包含.的行为有效行 if lineString1.find('(') < 0: #不包含括号 outString0+=str(txt_wrap_before(lineString1,'+')) elif lineString1.find('(') >= 0 and lineString1.split(' ')[2].find('::') < 0 : #包含括号但不包含:: outString0+=str(txt_wrap_before(lineString1,'(')) else:#包含括号包含:: outString0+=str(txt_wrap_by(lineString1,'::','(')) outString1 = outString1 + 'activate '+str(txt_wrap_by(lineString1,'/','.')) outString = outString + outString0 + ' ' + outString1 + ' ' outString0 = '' outString1 = '' #lineString = '' #print('outString'+outString) outString+='@enduml' file = open(out_file_path,'w') file.write(outString) file.close() #顺序读取文件内容,拼接处理后展示最后结果 def get_result_at(in_file_path,out_file_path): line_number=linecount(in_file_path) #获取文件行数 lineString0 = '' #提取文件名 lineString1 = '' #提取函数名 outString0 = '' #拼接本行RenderThread -> DrawFrameTask: run outString1 = '' #拼接上一行activate DrawFrameTask outString = '@startuml'+' '+'autonumber'+' ' #拼接成最终结果 while line_number-1>0: lineString0 = linecache.getline(in_file_path, line_number) lineString1 = linecache.getline(in_file_path, line_number-1) line_number=line_number-1 #print('lineString0'+lineString0) #print('lineString1'+lineString1) if lineString0.find('.cpp') >= 0 and lineString1.find('.cpp') >= 0: #包含.cpp为有效行 if lineString1.find('::') > 0 : #包含::,函数名字提取 outString0 = outString0 + str(txt_wrap_by(lineString0,'/','.cpp'))+' -> '+str(txt_wrap_by(lineString1,'/','.cpp'))+': '+str(txt_wrap_by_first(str(txt_wrap_by(lineString1,': ',' at')),'::','('))#设配这种情况:: Routine Gift_sendLayer::initView(int A::a, int b) at elif lineString1.find('::') < 0 : #不包含::,函数名字提取 outString0 = outString0 + str(txt_wrap_by(lineString0,'/','.cpp'))+' -> '+str(txt_wrap_by(lineString1,'/','.cpp'))+': '+str(lineString1.split(' ')[-3]) outString1 = outString1 + 'activate '+str(txt_wrap_by(lineString1,'/','.cpp')) outString = outString + outString0 + ' ' + outString1 + ' ' outString0 = '' outString1 = '' #print(outString) #print('outString'+outString) outString+='@enduml' file = open(out_file_path,'w') file.write(outString) file.close() #顺序读取文件内容,拼接处理后展示最后结果 def get_result_crash_info(in_file_path,out_file_path): line_number=linecount(in_file_path) #获取文件行数 lineString0 = '' #提取文件名 lineString1 = '' #提取函数名 outString0 = '' #拼接本行RenderThread -> DrawFrameTask: run outString1 = '' #拼接上一行activate DrawFrameTask outString = '@startuml'+' '+'autonumber'+' ' #拼接成最终结果 while line_number-1>0: lineString0 = linecache.getline(in_file_path, line_number) lineString1 = linecache.getline(in_file_path, line_number-1) line_number = line_number-1 #print('lineString0'+lineString0) #print('lineString1'+lineString1) if lineString0.find('#') >= 0 and lineString1.find('#') >= 0: #包含(为有效行 if lineString0.find('::') >= 0 and lineString1.find('::') >= 0: #包含.cpp为有效行 outString0 = outString0 + str(txt_wrap_by_first_last(lineString0,'::','(')).split('::')[0]+' -> '+str(txt_wrap_by_first_last(lineString1,'::','(')).split('::')[0]+': '+str(txt_wrap_by_first_last(lineString1,'::','(')).split('::')[1]#设配这种情况:: Routine Gift_sendLayer::initView(int A::a, int b) at outString1 = outString1 + 'activate '+str(txt_wrap_by_first_last(lineString1,'::','(')).split('::')[0] elif lineString0.find('::') >= 0 and lineString1.find('::') < 0: #包含.cpp为有效行 outString0 = outString0 + str(txt_wrap_by_first_last(lineString0,'::','(')).split('::')[0]+' -> '+str(txt_wrap_by(lineString1.split(' ')[-2],'/','.'))+': '+str(txt_wrap_by(lineString1,'(','+'))#设配这种情况:: Routine Gift_sendLayer::initView(int A::a, int b) at outString1 = outString1 + 'activate '+str(txt_wrap_by(lineString1.split(' ')[-2],'/','.')) elif lineString0.find('::') < 0 and lineString1.find('::') < 0: #包含.cpp为有效行 outString0 = outString0 + str(txt_wrap_by(lineString0.split(' ')[-2],'/','.'))+' -> '+str(txt_wrap_by(lineString1.split(' ')[-2],'/','.'))+': '+str(txt_wrap_by(lineString1,'(','+'))#设配这种情况:: Routine Gift_sendLayer::initView(int A::a, int b) at outString1 = outString1 + 'activate '+str(txt_wrap_by(lineString1.split(' ')[-2],'/','.')) elif lineString0.find('::') < 0 and lineString1.find('::') >= 0: #包含.cpp为有效行 outString0 = outString0 + str(txt_wrap_by(lineString0.split(' ')[-2],'/','.'))+' -> '+str(txt_wrap_by_first_last(lineString1,'::','(')).split('::')[0]+': '+str(txt_wrap_by_first_last(lineString1,'::','(')).split('::')[1]#设配这种情况:: Routine Gift_sendLayer::initView(int A::a, int b) at outString1 = outString1 + 'activate '+str(txt_wrap_by_first_last(lineString1,'::','(')).split('::')[0] outString = outString + outString0 + ' ' + outString1 + ' ' outString0 = '' outString1 = '' print('outString'+outString) outString+='@enduml' file = open(out_file_path,'w') file.write(outString) file.close() if __name__ == '__main__': in_file_path = r". est_input.txt" out_file_path = r". est_output.txt" get_result(in_file_path,out_file_path) in_file_path_at = r". est_input_at.txt" out_file_path_at = r". est_output_at.txt" get_result_at(in_file_path_at,out_file_path_at) in_file_path_crash_info = r". est_input_crash_info.txt" out_file_path_crash_info = r". est_output_crash_info.txt" get_result_crash_info(in_file_path_crash_info,out_file_path_crash_info)