这里的文档注释能提取格式为下面这两种:
import os
import re
def get_comment_from_file(file_path):
try:
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
menu_match = re.search(r'\@menu\s+(.+)', content)
if menu_match:
return menu_match.group(1).strip()
else:
personal_info_match = re.search(r'/\*\*\n\s+\*\s+(.+)\n\s+\*', content)
if personal_info_match:
comment = personal_info_match.group(1).strip()
if not comment.startswith("@Author"):
return comment
return "个人信息 业务处理"
except Exception as e:
print(f"Error reading {file_path}: {e}")
return None
def print_directory_contents(path, output_file, prefix=''):
if os.path.isdir(path):
for item in os.listdir(path):
item_path = os.path.join(path, item)
if os.path.isdir(item_path):
if 'target' not in item_path.split(os.path.sep):
output_file.write(f"{prefix}├── {item}\n")
new_prefix = prefix + "│ "
print_directory_contents(item_path, output_file, new_prefix)
else:
if item.endswith('.java'):
comment = get_comment_from_file(item_path)
if comment:
output_file.write(f"{prefix}│ ├── {item} // {comment}\n")
else:
# 对于非Java文件,仅输出文件名
output_file.write(f"{prefix}│ ├── {item}\n")
else:
comment = get_comment_from_file(path)
if path.endswith('.java') and comment:
output_file.write(f"{prefix}├── {os.path.basename(path)} // {comment}\n")
else:
# 对于单个非Java文件的情况
output_file.write(f"{prefix}├── {os.path.basename(path)}\n")
# 替换以下路径为你的目标文件夹路径
folder_path = "D:\\yingchong\\wms-product-master"
output_path = "D:\\yingchong\\directory_structure.txt"
with open(output_path, 'w', encoding='utf-8') as output_file:
print_directory_contents(folder_path, output_file)
效果如图: