-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_aggregator_api.py
More file actions
304 lines (250 loc) · 8.81 KB
/
code_aggregator_api.py
File metadata and controls
304 lines (250 loc) · 8.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import argparse
import json
import os
import queue
import sys
from typing import Any
# 导入核心函数
from utils import (
aggregate_code,
find_files,
generate_file_tree,
get_unique_filepath,
)
class CodeAggregatorAPI:
"""代码聚合工具的API接口类
提供简洁的编程接口,无需GUI即可使用所有核心功能。
"""
def __init__(self):
self.default_extensions = [".py"]
self.default_ignore_items = {
"venv",
"__pycache__",
".git",
".vscode",
"node_modules",
"dist",
"build",
".pytest_cache",
}
self.config_file = "code_aggregator_config.json"
def aggregate_directory(
self,
directory: str,
output_file: str = None,
extensions: list[str] = None,
ignore_items: set[str] = None,
output_format: str = ".md",
auto_rename: bool = True,
verbose: bool = True,
) -> str | None:
"""聚合指定目录中的代码文件
Args:
directory: 要扫描的根目录
output_file: 输出文件路径(如果为None,则在当前目录生成)
extensions: 要包含的文件扩展名列表
ignore_items: 要忽略的文件/文件夹名称或路径
output_format: 输出格式(.md 或 .txt)
auto_rename: 如果文件已存在,是否自动重命名
verbose: 是否输出详细日志
Returns:
成功时返回输出文件路径,失败时返回None
"""
# 参数处理
if extensions is None:
extensions = self.default_extensions.copy()
if ignore_items is None:
ignore_items = self.default_ignore_items.copy()
if output_file is None:
output_file = os.path.join(os.getcwd(), f"code_summary{output_format}")
# 确保目录存在
if not os.path.isdir(directory):
if verbose:
print(f"错误:目录不存在 - {directory}")
return None
# 创建日志队列
log_queue = queue.Queue()
progress_queue = queue.Queue()
try:
# 查找文件
found_files = find_files(directory, extensions, ignore_items, log_queue)
# 输出日志
if verbose:
self._print_queue_messages(log_queue)
if not found_files:
if verbose:
print("未找到符合条件的文件")
return None
# 处理输出文件路径
if auto_rename:
output_dir = os.path.dirname(output_file) or os.getcwd()
output_filename = os.path.splitext(os.path.basename(output_file))[0]
output_ext = os.path.splitext(output_file)[1] or output_format
final_output_path = get_unique_filepath(
output_dir, output_filename, output_ext, log_queue
)
if verbose:
self._print_queue_messages(log_queue)
else:
final_output_path = output_file
# 聚合代码
aggregate_code(
directory,
found_files,
final_output_path,
output_format,
log_queue,
progress_queue,
)
# 输出最终日志
if verbose:
self._print_queue_messages(log_queue)
print(f"\n✨ 聚合完成!文件已保存到: {final_output_path}")
return final_output_path
except Exception as e:
if verbose:
print(f"发生错误: {e}")
return None
def generate_tree_only(
self,
directory: str,
extensions: list[str] = None,
ignore_items: set[str] = None,
) -> str | None:
"""仅生成文件结构树,不聚合代码内容
Args:
directory: 要扫描的根目录
extensions: 要包含的文件扩展名列表
ignore_items: 要忽略的文件/文件夹名称或路径
Returns:
文件结构树字符串,失败时返回None
"""
if extensions is None:
extensions = self.default_extensions.copy()
if ignore_items is None:
ignore_items = self.default_ignore_items.copy()
log_queue = queue.Queue()
try:
found_files = find_files(directory, extensions, ignore_items, log_queue)
if found_files:
return generate_file_tree(directory, found_files, log_queue)
return None
except Exception:
return None
def save_config(self, config: dict[str, Any], config_path: str = None) -> bool:
"""保存配置到文件
Args:
config: 配置字典
config_path: 配置文件路径(可选)
Returns:
保存成功返回True,否则返回False
"""
if config_path is None:
config_path = self.config_file
try:
with open(config_path, "w", encoding="utf-8") as f:
json.dump(config, f, indent=4, ensure_ascii=False)
return True
except Exception:
return False
def load_config(self, config_path: str = None) -> dict[str, Any] | None:
"""从文件加载配置
Args:
config_path: 配置文件路径(可选)
Returns:
配置字典,失败时返回None
"""
if config_path is None:
config_path = self.config_file
if not os.path.exists(config_path):
return None
try:
with open(config_path, encoding="utf-8") as f:
return json.load(f)
except Exception:
return None
def _print_queue_messages(self, log_queue: queue.Queue):
"""打印队列中的所有消息"""
while not log_queue.empty():
try:
message = log_queue.get_nowait()
print(message)
except queue.Empty:
break
def main():
"""命令行接口入口"""
parser = argparse.ArgumentParser(
description="代码聚合工具 - 命令行接口",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
使用示例:
python code_aggregator_api.py /path/to/project
python code_aggregator_api.py /path/to/project --ext .py,.js,.html
python code_aggregator_api.py /path/to/project --output ./result.md
python code_aggregator_api.py /path/to/project --tree-only
""",
)
parser.add_argument("directory", help="要扫描的根目录路径")
parser.add_argument(
"--output", "-o", help="输出文件路径(默认:当前目录下的code_summary.md)"
)
parser.add_argument(
"--ext",
"--extensions",
help="要包含的文件扩展名,用逗号分隔(默认:.py)",
default=".py",
)
parser.add_argument(
"--ignore",
help="要忽略的文件/文件夹名称,用逗号分隔",
default="venv,__pycache__,.git,.vscode,node_modules,dist,build",
)
parser.add_argument(
"--format", choices=[".md", ".txt"], default=".md", help="输出格式(默认:.md)"
)
parser.add_argument(
"--no-auto-rename",
action="store_true",
help="如果输出文件已存在,不自动重命名而是覆盖",
)
parser.add_argument(
"--tree-only",
action="store_true",
help="仅生成并打印文件结构树,不聚合代码内容",
)
parser.add_argument(
"--quiet", "-q", action="store_true", help="安静模式,不输出详细日志"
)
args = parser.parse_args()
# 解析参数
directory = os.path.abspath(args.directory)
extensions = [ext.strip() for ext in args.ext.split(",") if ext.strip()]
ignore_items = {item.strip() for item in args.ignore.split(",") if item.strip()}
# 创建API实例
api = CodeAggregatorAPI()
if args.tree_only:
# 仅生成文件结构树
tree = api.generate_tree_only(directory, extensions, ignore_items)
if tree:
print(tree)
return 0
else:
print("生成文件结构树失败")
return 1
else:
# 聚合代码
result = api.aggregate_directory(
directory=directory,
output_file=args.output,
extensions=extensions,
ignore_items=ignore_items,
output_format=args.format,
auto_rename=not args.no_auto_rename,
verbose=not args.quiet,
)
if result:
return 0
else:
return 1
if __name__ == "__main__":
sys.exit(main())