总述
编写代码时一般都有一个相对规范的函数注释,源文件写好后,提取注释与函数出来除了复制粘贴还有什么高效办法呢?
具体实现
-
注释规范
/**
*@author whiteblack
*@brief push数据到堆栈
*@Input params: 堆栈对象
*@Input params: 数据
*@Input params:
*@Output params:
*@Output params:
*@return 0 successed -1 failed
*/ -
python代码实现
import os
import sys
import re
'''
用正则提取函数注释以及函数
'''
test_path = r"XXXXXXXXXX"
all_dir_path = []
#获取目录下所有有效文件路径
def get_dir_all_path(code_path_dir):
for root, dirs, files in os.walk(code_path_dir): #获取目录下的文件和子目录 root为根目录
for i in files:
if i.endswith(".c"):
all_dir_path.append(os.path.join(root, i))
#print(i)
#获取注释与函数名
def get_func_briefandname(code_path):
all_str = ''
base_name = os.path.basename(code_path).split('.')[0].upper()
save_path =os.path.join(os.path.dirname(code_path), os.path.basename(code_path).split('.')[0]+".h")
with open(code_path, "r", encoding="utf-8") as f:
for i in f.readlines():
all_str += i.strip(' ')
#print(all_str)
rule = re.compile(r"/[*]{2}[^{]+[(].+[)]") # 从/**开始 中间不可跨越函数 以函数()结尾
re_str = re.findall(rule, all_str)
#print(re_str)
if len(re_str) > 0:
with open(save_path, 'w') as f:
f.write("#ifndef __" + base_name + '\n')
f.write("#define __" + base_name + '\n\n')
for i in re_str:
f.write(i + ";\n\n\n")
f.write("\n\n#endif")
print(save_path + " save successed\r\n")
if __name__ == "__main__":
get_dir_all_path(r"test_path")
for i in all_dir_path:
#print(i)
get_func_briefandname(i)