Spaces:
Sleeping
Sleeping
File size: 4,609 Bytes
52d0298 |
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 |
#!/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())
|