分片匹配模式,并按格式写文件内容
# -*- coding: utf-8 -*- """ 1、解析封装函数的模式 2、根据common文件匹配def和api 3、把匹配的内容按格式填充到hub_api3文件中 """ import os import re # 记录任务个数 SuccCount = 0 FailCount = 0 content = """ @pin_action( "qctest/iotcloud/apiv3_monitor/topic_test/topic_test_common.py", "create_topic_policy", "CreateTopicPolicy", ) def create_topic_policy(self, *args, **argv): \""" action: CreateTopicPolicy \""" return topic_test_common.create_topic_policy(*args, **argv) """ def write_format_content(file_name, def_name, api_name): format_string = """ @pin_action( "qctest/iotcloud/api_test/%s", "%s", "%s", ) def %s(self, *args, **argv): \""" action: %s \""" return topic_test_common.%s(*args, **argv) """ % (file_name, def_name, api_name, def_name, api_name, def_name) return format_string def write_format_lib(dir_path): print(len(dir_path)) for fl in dir_path: #print(fl) dir_path = re.findall(r'api_test/(.*?).py', fl)[0] + '.py' #print(type(dir_path)) path = re.findall(r'(.*?)/', dir_path)[0] #print(path) file_name = re.findall(r'/(.*?).py', dir_path)[0] #print(file_name) format_string = """ from qctest.iotcloud.api_test.%s import %s """ % (path, file_name) print(format_string) # with open('imoprt_lib.py', 'w') as f: # f.write(format_string) # f.write('\n') # print(format_string) def file_path_dir(path): g = os.walk(path) file_path_list = [] for path, dir_list, file_list in g: for file_name in file_list: file_path = os.path.join(path, file_name) #print(file_path) if file_path.endswith('_common.py'): file_path_list.append(file_path) #print(file_path) return file_path_list def str2Hump(text): #arr = filter(None, text.lower().split('_')) arr = text.lower().split('_') #print(arr) res = '' j = 0 for i in arr: if j == 0: res = i[0].upper() + i[1:] else: res = res + i[0].upper() + i[1:] j += 1 return res if __name__ == '__main__': # 读取common文件 dir_path = '/Users/mecexia/PycharmProjects/workspace/gitcode/IotTest/qctest/iotcloud/api_test' file_path_list = file_path_dir(dir_path) #write_format_lib(file_path_list) save_name = 'hub.py' with open(save_name, 'w') as f: for file_path in file_path_list: print(file_path) with open(file_path, 'r') as fs: common_content = fs.read() content_list = common_content.split('return rsp') #print(content_list) file_name = re.findall(r'api_test/(.*?).py', file_path)[0] + '.py' #print(file_name) api_name = [] def_name = [] for content in content_list: def_name_list = re.findall(r'def (.*?)\(', content) api_name_1 = re.findall(r"request\('(.*?)'", content) api_name_2 = re.findall(r'request\("(.*?)"', content) api_name_3 = re.findall(r'action = "(.*?)"', content) api_name_list = api_name_1 or api_name_2 or api_name_3 if api_name_list: # def_name.append(def_name_list[0]) def_name.extend(def_name_list) # api_name.append(api_name_list[0]) api_name.extend(api_name_list) for i in range(0, len(def_name)): try: format_string = write_format_content(file_name, def_name[i], api_name[i]) print("match success", def_name[i], api_name[i]) f.write(format_string) f.write('\n') SuccCount += 1 except: print("第{0}条数据处理失败".format(def_name[i])) print("第{0}条数据处理失败".format(api_name[i])) FailCount += 1 print("SuccCount", SuccCount) print("FailCount", FailCount)
本文来自博客园,作者:ReluStarry,转载请注明原文链接:https://www.cnblogs.com/relustarry/p/16131367.html