SXXXXXXX_PyUCC/tools/export_ucc_extended.py

134 lines
3.4 KiB
Python

"""Example: Export UCC-style extended metrics to CSV.
This demonstrates how to use the extended counting features
to produce UCC-compatible reports.
"""
from pathlib import Path
import csv
from typing import List, Dict, Any
from pyucc.core.ucc_extended_counting import (
analyze_file_ucc_style,
format_ucc_table_header,
format_ucc_table_line,
)
def export_ucc_extended_csv(files: List[Path], output_path: Path):
"""
Export extended UCC-style metrics to CSV.
Args:
files: List of source files to analyze
output_path: Output CSV file path
"""
results = []
print(f"Analyzing {len(files)} files...")
for i, file_path in enumerate(files, 1):
if i % 10 == 0:
print(f" Progress: {i}/{len(files)}")
try:
result = analyze_file_ucc_style(file_path)
result["name"] = file_path.name
result["path"] = str(file_path)
results.append(result)
except Exception as e:
print(f" Error analyzing {file_path}: {e}")
# Write to CSV
fieldnames = [
"name",
"path",
"total_lines",
"blank_lines",
"comment_whole",
"comment_embedded",
"compiler_directives",
"data_declarations",
"exec_instructions",
"logical_sloc",
"physical_sloc",
"language",
]
with open(output_path, "w", newline="", encoding="utf-8") as f:
writer = csv.DictWriter(f, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(results)
print(f"\n✅ Exported to: {output_path}")
# Print summary in UCC format
print("\n" + "=" * 100)
print("UCC-Style Summary:")
print(format_ucc_table_header())
for result in results[:10]: # Show first 10
print(format_ucc_table_line(result))
if len(results) > 10:
print(f"... ({len(results) - 10} more files)")
# Print totals
totals = calculate_totals(results)
print("-" * 100)
print(format_ucc_table_line(totals, "TOTAL"))
def calculate_totals(results: List[Dict[str, Any]]) -> Dict[str, Any]:
"""Calculate aggregate totals across all files."""
totals = {
"total_lines": 0,
"blank_lines": 0,
"comment_whole": 0,
"comment_embedded": 0,
"compiler_directives": 0,
"data_declarations": 0,
"exec_instructions": 0,
"logical_sloc": 0,
"physical_sloc": 0,
"file": "TOTAL",
}
for result in results:
for key in totals:
if key != "file":
totals[key] += result.get(key, 0)
return totals
def demo_extended_counting():
"""Demo the extended counting on test files."""
# Find some C/C++ files to analyze
test_dir = Path(r"C:\__temp\Metrics\attuale\REP\Projects\DSP")
if not test_dir.exists():
print("⚠️ Test directory not found. Using current directory...")
test_dir = Path.cwd() / "pyucc"
# Collect files
files = []
for pattern in ["**/*.c", "**/*.cpp", "**/*.h"]:
files.extend(list(test_dir.glob(pattern))[:20]) # Limit to 20 files
if not files:
print("No C/C++ files found to analyze.")
return
print(f"Found {len(files)} files to analyze")
# Export
output_path = Path("ucc_extended_output.csv")
export_ucc_extended_csv(files, output_path)
if __name__ == "__main__":
demo_extended_counting()