分片匹配模式,并按格式写文件内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# -*- 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)

  

posted @   ReluStarry  阅读(12)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· .NET Core 中如何实现缓存的预热?
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
点击右上角即可分享
微信分享提示