Python生成java项目文件系统树并提取文档注释(支持多模块)

6 篇文章 0 订阅
4 篇文章 0 订阅

这里的文档注释能提取格式为下面这两种:

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)

效果如图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值