Files
gh-epieczko-betty/skills/file.compare/file_compare.py
2025-11-29 18:26:08 +08:00

116 lines
3.1 KiB
Python
Executable File

#!/usr/bin/env python3
"""
file.compare - Compare two files and generate detailed diff reports showing line-by-line differences
Generated by meta.skill
"""
import os
import sys
import json
import yaml
from pathlib import Path
from typing import Dict, List, Any, Optional
from betty.config import BASE_DIR
from betty.logging_utils import setup_logger
logger = setup_logger(__name__)
class FileCompare:
"""
Compare two files and generate detailed diff reports showing line-by-line differences
"""
def __init__(self, base_dir: str = BASE_DIR):
"""Initialize skill"""
self.base_dir = Path(base_dir)
def execute(self, file_path_1: Optional[str] = None, file_path_2: Optional[str] = None, output_format_optional: Optional[str] = None) -> Dict[str, Any]:
"""
Execute the skill
Returns:
Dict with execution results
"""
try:
logger.info("Executing file.compare...")
# TODO: Implement skill logic here
# Implementation notes:
# Use Python's difflib to compare files line by line. Support multiple output formats: - unified: Standard unified diff format - context: Context diff format - html: HTML diff with color coding - json: Structured JSON with line-by-line changes Include statistics: - Total lines added - Total lines removed - Total lines modified - Percentage similarity Handle binary files by detecting and reporting as non-text.
# Placeholder implementation
result = {
"ok": True,
"status": "success",
"message": "Skill executed successfully"
}
logger.info("Skill completed successfully")
return result
except Exception as e:
logger.error(f"Error executing skill: {e}")
return {
"ok": False,
"status": "failed",
"error": str(e)
}
def main():
"""CLI entry point"""
import argparse
parser = argparse.ArgumentParser(
description="Compare two files and generate detailed diff reports showing line-by-line differences"
)
parser.add_argument(
"--file-path-1",
help="file_path_1"
)
parser.add_argument(
"--file-path-2",
help="file_path_2"
)
parser.add_argument(
"--output-format-optional",
help="output_format (optional)"
)
parser.add_argument(
"--output-format",
choices=["json", "yaml"],
default="json",
help="Output format"
)
args = parser.parse_args()
# Create skill instance
skill = FileCompare()
# Execute skill
result = skill.execute(
file_path_1=args.file_path_1,
file_path_2=args.file_path_2,
output_format_optional=args.output_format_optional,
)
# Output result
if args.output_format == "json":
print(json.dumps(result, indent=2))
else:
print(yaml.dump(result, default_flow_style=False))
# Exit with appropriate code
sys.exit(0 if result.get("ok") else 1)
if __name__ == "__main__":
main()