Repository navigation
Expand file tree
/
Copy pathcli.py
More file actions
372 lines (300 loc) · 11.2 KB
/
Copy pathcli.py
File metadata and controls
372 lines (300 loc) · 11.2 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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#!/usr/bin/env python3
"""Command-line interface for PythonProjectHelper with modular exporters."""
from __future__ import annotations
import argparse
import json
import sys
from pathlib import Path
from typing import Dict, List
from analysis import AnalysisEngine
from exporters import ExporterRegistry
from report import ProjectReport
from scan import scan_project
def main() -> None:
"""Main CLI entry point."""
parser = argparse.ArgumentParser(
description="Python Project Helper - Analyze and export Python projects",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
%(prog)s --gui # Launch GUI interface
%(prog)s /path/to/project # Analyze project using GUI
%(prog)s /path/to/project --format llm-tds --output report.json
%(prog)s /path/to/project --format basic-markdown --output docs.md
%(prog)s /path/to/project --list-formats # Show available formats
%(prog)s /path/to/project --bundle --output bundle.json
"""
)
# Positional arguments
parser.add_argument(
'path',
nargs='?',
help='Path to project directory to analyze'
)
# GUI mode
parser.add_argument(
'--gui',
action='store_true',
help='Launch GUI interface'
)
# Export options
parser.add_argument(
'--format', '-f',
help='Export format (use --list-formats to see available)'
)
parser.add_argument(
'--output', '-o',
help='Output file path'
)
parser.add_argument(
'--bundle',
action='store_true',
help='Bundle multiple formats into one file'
)
parser.add_argument(
'--formats',
nargs='*',
help='Formats to include in bundle (default: all)'
)
parser.add_argument(
'--include-content',
action='store_true',
help='Include file contents in analysis-only exports (full-content formats always include content)'
)
# Information
parser.add_argument(
'--list-formats',
action='store_true',
help='List available export formats'
)
parser.add_argument(
'--format-info',
help='Show detailed information about a specific format'
)
# Analysis options
parser.add_argument(
'--max-files',
type=int,
help='Maximum number of files to include'
)
parser.add_argument(
'--max-size',
type=int,
help='Maximum file size in bytes'
)
parser.add_argument(
'--include-tests',
action='store_true',
help='Include test files in analysis'
)
parser.add_argument(
'--compact',
action='store_true',
help='Use compact output format where supported'
)
# Verbosity
parser.add_argument(
'--verbose', '-v',
action='store_true',
help='Verbose output'
)
args = parser.parse_args()
# Handle GUI mode
if args.gui or (not args.path and not args.list_formats and not args.format_info):
launch_gui()
return
# Handle format listing
if args.list_formats:
list_formats()
return
if args.format_info:
show_format_info(args.format_info)
return
# Validate required arguments
if not args.path:
parser.error("Project path is required for CLI analysis")
project_path = Path(args.path).resolve()
if not project_path.exists():
print(f"Error: Project path '{project_path}' does not exist", file=sys.stderr)
sys.exit(1)
if not project_path.is_dir():
print(f"Error: Project path '{project_path}' is not a directory", file=sys.stderr)
sys.exit(1)
# Analyze project
try:
if args.verbose:
print(f"Analyzing project: {project_path}")
# Build analysis options
analysis_options = {}
if args.max_files:
analysis_options['max_files'] = args.max_files
if args.max_size:
analysis_options['max_size'] = args.max_size
if args.include_tests:
analysis_options['include_tests'] = args.include_tests
if args.compact:
analysis_options['compact'] = args.compact
# Scan and analyze
project_report = scan_project(project_path)
engine = AnalysisEngine()
analysis = engine.analyze_project(project_report, analysis_options)
if args.verbose:
print(f"Found {len(analysis.files)} files, {analysis.project.totals.sloc} SLOC")
# Handle bundle mode
if args.bundle:
handle_bundle(analysis, project_report, args, args.verbose)
else:
handle_single_export(analysis, project_report, args, args.verbose)
except Exception as e:
print(f"Error during analysis: {e}", file=sys.stderr)
if args.verbose:
import traceback
traceback.print_exc()
sys.exit(1)
def launch_gui() -> None:
"""Launch the GUI interface."""
try:
from gui import run_gui
run_gui()
except ImportError as e:
print(f"Error: GUI dependencies not available: {e}", file=sys.stderr)
sys.exit(1)
except Exception as e:
print(f"Error launching GUI: {e}", file=sys.stderr)
sys.exit(1)
def list_formats() -> None:
"""List all available export formats."""
from exporters import list_available_formats, get_exporter
formats = list_available_formats()
if not formats:
print("No export formats available")
return
print("Available export formats:")
print()
for format_id in formats:
exporter = get_exporter(format_id)
if exporter:
print(f" {format_id}")
print(f" Name: {exporter.name}")
print(f" MIME Type: {exporter.mimetype()}")
print(f" LLM Friendly: {exporter.is_llm_friendly()}")
print(f" Lossless: {exporter.is_lossless()}")
print()
def show_format_info(format_id: str) -> None:
"""Show detailed information about a specific format."""
from exporters import get_exporter
exporter = get_exporter(format_id)
if not exporter:
print(f"Error: Unknown format '{format_id}'", file=sys.stderr)
print("\nUse --list-formats to see available formats", file=sys.stderr)
sys.exit(1)
print(f"Format: {format_id}")
print(f"Name: {exporter.name}")
print(f"MIME Type: {exporter.mimetype()}")
print(f"LLM Friendly: {exporter.is_llm_friendly()}")
print(f"Lossless: {exporter.is_lossless()}")
print()
print("Example usage:")
print(f" python cli.py /path/to/project --format {format_id} --output report.out")
def handle_single_export(analysis, project_report, args, verbose: bool) -> None:
"""Handle single format export."""
if not args.format:
print("Error: --format is required for single export", file=sys.stderr)
sys.exit(1)
from exporters import get_exporter
exporter = get_exporter(args.format)
if not exporter:
print(f"Error: Unknown format '{args.format}'", file=sys.stderr)
print("\nUse --list-formats to see available formats", file=sys.stderr)
sys.exit(1)
try:
# Convert analysis to dict for compatibility
analysis_dict = analysis.model_dump() if hasattr(analysis, 'model_dump') else analysis.__dict__
# Prepare export options with project report for full-content exporters
export_options = args.__dict__.copy()
export_options['project_report'] = project_report
# Generate export
export_data = exporter.render(analysis_dict, export_options)
# Determine output path
if args.output:
output_path = Path(args.output)
else:
# Auto-generate filename
output_path = Path(f"{analysis.project.name}_{args.format}.out")
# Write output
output_path.parent.mkdir(parents=True, exist_ok=True)
if isinstance(export_data, str):
output_path.write_text(export_data, encoding='utf-8')
else:
output_path.write_bytes(export_data)
if verbose:
print(f"Exported to: {output_path}")
print(f"Size: {output_path.stat().st_size:,} bytes")
else:
print(str(output_path))
except Exception as e:
print(f"Error during export: {e}", file=sys.stderr)
sys.exit(1)
def handle_bundle(analysis, project_report, args, verbose: bool) -> None:
"""Handle bundle export with multiple formats."""
from exporters import list_available_formats, get_exporter
# Determine which formats to include
if args.formats:
format_ids = args.formats
else:
# Use all available formats
format_ids = list_available_formats()
if verbose:
print(f"Bundling formats: {', '.join(format_ids)}")
bundle = {
'project': analysis.project.model_dump() if hasattr(analysis.project, 'model_dump') else analysis.project.__dict__,
'exports': {},
'metadata': {
'bundle_version': '1.0',
'generated_by': 'PythonProjectHelper CLI',
'formats': format_ids
}
}
# Convert analysis to dict for compatibility
analysis_dict = analysis.model_dump() if hasattr(analysis, 'model_dump') else analysis.__dict__
# Prepare export options with project report for full-content exporters
export_options = args.__dict__.copy()
export_options['project_report'] = project_report
# Generate exports for each format
for format_id in format_ids:
try:
exporter = get_exporter(format_id)
if not exporter:
print(f" ✗ {format_id}: Unknown format", file=sys.stderr)
continue
export_data = exporter.render(analysis_dict, export_options)
bundle['exports'][format_id] = {
'name': exporter.name,
'data': export_data.decode('utf-8') if isinstance(export_data, bytes) else export_data,
'metadata': {
'mime_type': exporter.mimetype(),
'llm_friendly': exporter.is_llm_friendly(),
'lossless': exporter.is_lossless()
}
}
if verbose:
print(f" ✓ {format_id}")
except Exception as e:
print(f" ✗ {format_id}: {e}", file=sys.stderr)
continue
# Determine output path
if args.output:
output_path = Path(args.output)
else:
output_path = Path(f"{analysis.project.name}_bundle.json")
# Write bundle
output_path.parent.mkdir(parents=True, exist_ok=True)
output_path.write_text(json.dumps(bundle, indent=2), encoding='utf-8')
if verbose:
print(f"Bundle exported to: {output_path}")
print(f"Size: {output_path.stat().st_size:,} bytes")
print(f"Formats included: {len(bundle['exports'])}")
else:
print(str(output_path))
if __name__ == "__main__":
main()