- 在一个单独文件夹中建立此python文件,并将ino文件拖入文件夹中
- 运行前需对ino文件进行格式化
import re
from pathlib import Path
# 查找路径下所有ino文件
def find_all_ino(dirs='.'):
files = []
# 过滤规则
filter_rulers = ["*.ino"]
for filter in filter_rulers:
for dir_cur in dirs:
p = Path(dir_cur).rglob(filter)
for i in p:
# print(i)
# 获取地址字符串
file_path = str(i)
# 地址转义
new_file = "\\".join(file_path.split("\\"))
files.append(new_file)
return files
def get_function_declaration(str_in):
regex = r"(^[a-zA-Z_]+\s+[a-zA-Z_]+\(.*\))\s+\{"
functions = []
matches = re.finditer(regex, str_in, re.MULTILINE)
for matchNum, match in enumerate(matches, start=1):
# print("Match {matchNum} was found at {start}-{end}: {match}".format(
# matchNum=matchNum, start=match.start(), end=match.end(), match=match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
# print("Group {groupNum} found at {start}-{end}: {group}".format(groupNum=groupNum,
# start=match.start(groupNum), end=match.end(groupNum), group=match.group(groupNum)))
functions.append(match.group(groupNum))
return functions
# 将列表存为文件
def save_list_to_file(list_in, file_name):
with open(file_name, 'w') as f:
for item in list_in:
f.write("%s;\n" % item)
if __name__ == '__main__':
print("Start")
ino_files = find_all_ino()
file_path = ino_files[0]
str_in = Path(file_path).read_text(encoding='utf-8')
functions = get_function_declaration(str_in)
save_list_to_file(functions, "functions.txt")
print("Done !")