python class中插入代码

该脚本在E:/pbrt-v3/src目录下搜索所有非.h,.cpp,.c,.hpp文件,读取内容,寻找类定义。当找到类定义行时,打印出类名,并在找到访问修饰符(public,private,protected)时标记类定义结束,最后将修改后的内容写回文件。
import re
import os
import glob

# search all files inside a specific folder
# *.* means file name with any extension
dir_path = r'E:/pbrt-v3/src/**\*.*'
for file in glob.glob(dir_path, recursive=True):
    if not file.endswith(('.h', '.cpp', '.c', '.hpp')):
        print("file_name, ", file)
        continue

    file_content = ''
    try:
        with open(file, 'r', encoding='utf-8') as file_obj:
            flag = 0
            for line in file_obj:
                file_content += line            
                if flag == 0 and re.match(r'.*class.*', line):
                    print(line.split(' ')[1])
                    flag = 1
                elif flag ==1 and (re.match(r'.*public:.*', line) or re.match(r'.*private:.*', line) or re.match(r'.*protected:.*', line)):
                    flag = 2
                    file_content += "CLASS_FINED"

                    
    except:
        print('error catched', file)
        continue

    with open(file, 'w', encoding='utf-8') as file_obj:
        file_obj.write(file_content)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值