Spaces:
Sleeping
Sleeping
| #!/usr/bin/env python3 | |
| """ | |
| Code formatting and quality checker for TranscriptorEnhanced | |
| Checks for common formatting issues, syntax errors, and code quality problems | |
| """ | |
| import os | |
| import sys | |
| import py_compile | |
| import ast | |
| from pathlib import Path | |
| def check_syntax(filepath): | |
| """Check if Python file has valid syntax""" | |
| try: | |
| with open(filepath, 'r', encoding='utf-8') as f: | |
| code = f.read() | |
| ast.parse(code) | |
| return True, "β Valid syntax" | |
| except SyntaxError as e: | |
| return False, f"β Syntax error at line {e.lineno}: {e.msg}" | |
| except Exception as e: | |
| return False, f"β Error: {str(e)}" | |
| def check_indentation(filepath): | |
| """Check for mixed tabs/spaces and indentation issues""" | |
| issues = [] | |
| try: | |
| with open(filepath, 'r', encoding='utf-8') as f: | |
| lines = f.readlines() | |
| has_tabs = False | |
| has_spaces = False | |
| for i, line in enumerate(lines, 1): | |
| if line.startswith('\t'): | |
| has_tabs = True | |
| if line.startswith(' '): | |
| has_spaces = True | |
| # Check for trailing whitespace | |
| if line.rstrip() != line.rstrip('\n').rstrip('\r'): | |
| issues.append(f"Line {i}: Trailing whitespace") | |
| if has_tabs and has_spaces: | |
| issues.append("Mixed tabs and spaces detected") | |
| return len(issues) == 0, issues if issues else ["β Clean indentation"] | |
| except Exception as e: | |
| return False, [f"β Error checking indentation: {str(e)}"] | |
| def check_line_length(filepath, max_length=120): | |
| """Check for overly long lines""" | |
| long_lines = [] | |
| try: | |
| with open(filepath, 'r', encoding='utf-8') as f: | |
| lines = f.readlines() | |
| for i, line in enumerate(lines, 1): | |
| if len(line.rstrip()) > max_length: | |
| long_lines.append(f"Line {i}: {len(line.rstrip())} chars") | |
| if long_lines: | |
| return False, long_lines[:5] # Show first 5 only | |
| return True, ["β All lines within limit"] | |
| except Exception as e: | |
| return False, [f"β Error: {str(e)}"] | |
| def check_file(filepath): | |
| """Run all checks on a Python file""" | |
| print(f"\n{'='*80}") | |
| print(f"Checking: {filepath}") | |
| print('='*80) | |
| all_passed = True | |
| # Syntax check | |
| print("\n1. SYNTAX CHECK:") | |
| passed, result = check_syntax(filepath) | |
| print(f" {result}") | |
| all_passed = all_passed and passed | |
| # Indentation check | |
| print("\n2. INDENTATION CHECK:") | |
| passed, results = check_indentation(filepath) | |
| for r in results[:5]: # Show first 5 issues | |
| print(f" {r}") | |
| if len(results) > 5: | |
| print(f" ... and {len(results) - 5} more issues") | |
| all_passed = all_passed and passed | |
| # Line length check | |
| print("\n3. LINE LENGTH CHECK (max 120 chars):") | |
| passed, results = check_line_length(filepath) | |
| for r in results: | |
| print(f" {r}") | |
| all_passed = all_passed and passed | |
| return all_passed | |
| def main(): | |
| """Check all Python files in the project""" | |
| base_dir = Path('/home/john/TranscriptorEnhanced') | |
| # Core Python files to check | |
| core_files = [ | |
| 'app.py', | |
| 'llm.py', | |
| 'quote_extractor.py', | |
| 'story_writer.py', | |
| 'production_logger.py', | |
| 'narrative_report_generator.py', | |
| 'reporting.py', | |
| 'validation.py', | |
| 'report_parser.py', | |
| 'table_builder.py' | |
| ] | |
| print("="*80) | |
| print("TRANSCRIPTORAI - CODE FORMATTING CHECK") | |
| print("="*80) | |
| all_passed = True | |
| checked = 0 | |
| failed = [] | |
| for filename in core_files: | |
| filepath = base_dir / filename | |
| if filepath.exists(): | |
| checked += 1 | |
| passed = check_file(str(filepath)) | |
| if not passed: | |
| failed.append(filename) | |
| all_passed = False | |
| else: | |
| print(f"\nβ οΈ File not found: {filename}") | |
| # Summary | |
| print("\n" + "="*80) | |
| print("SUMMARY") | |
| print("="*80) | |
| print(f"Files checked: {checked}") | |
| print(f"Files passed: {checked - len(failed)}") | |
| print(f"Files failed: {len(failed)}") | |
| if failed: | |
| print("\nβ Files with issues:") | |
| for f in failed: | |
| print(f" - {f}") | |
| else: | |
| print("\nβ All files passed formatting checks!") | |
| print("="*80) | |
| return 0 if all_passed else 1 | |
| if __name__ == "__main__": | |
| sys.exit(main()) | |