Spaces:
Sleeping
Sleeping
File size: 4,516 Bytes
5c4bfe8 | 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 | #!/usr/bin/env python3
"""
Code Cleanup Script for Feature 006: Urdu Translation
Removes unused imports and organizes code
Run: python3 cleanup_code.py
"""
import os
import re
from pathlib import Path
def remove_unused_imports(file_path):
"""Remove commented imports and organize imports"""
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
original_content = content
lines = content.split('\n')
cleaned_lines = []
import_section = []
in_import_section = True
for line in lines:
# Skip empty lines in import section
if in_import_section and line.strip() == '':
continue
# Collect imports
if in_import_section and (line.startswith('import ') or line.startswith('from ')):
import_section.append(line)
continue
# End of import section
if in_import_section and not line.startswith('import ') and not line.startswith('from ') and line.strip():
in_import_section = False
# Add organized imports
import_section.sort()
cleaned_lines.extend(import_section)
cleaned_lines.append('')
cleaned_lines.append(line)
# Write back if changed
new_content = '\n'.join(cleaned_lines)
if new_content != original_content:
with open(file_path, 'w', encoding='utf-8') as f:
f.write(new_content)
return True
return False
def cleanup_trailing_whitespace(file_path):
"""Remove trailing whitespace from lines"""
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
lines = content.split('\n')
cleaned_lines = [line.rstrip() for line in lines]
new_content = '\n'.join(cleaned_lines)
if new_content != content:
with open(file_path, 'w', encoding='utf-8') as f:
f.write(new_content)
return True
return False
def check_pep8_line_length(file_path):
"""Check for lines exceeding PEP 8 line length (120 chars is acceptable for modern code)"""
issues = []
with open(file_path, 'r', encoding='utf-8') as f:
for i, line in enumerate(f, 1):
if len(line.rstrip()) > 120:
issues.append(f" Line {i}: {len(line.rstrip())} characters")
return issues
def main():
"""Main cleanup function"""
backend_dir = Path(__file__).parent
# Files to cleanup
files_to_check = [
'api/translation.py',
'api/feedback.py',
'services/translation_service.py',
'services/rate_limiter.py',
'models/translation_feedback.py',
'main.py'
]
print("π§Ή Starting code cleanup...")
print(f"π Backend directory: {backend_dir}\n")
total_files = 0
files_modified = 0
for file_rel_path in files_to_check:
file_path = backend_dir / file_rel_path
if not file_path.exists():
print(f"β οΈ {file_rel_path} - NOT FOUND")
continue
total_files += 1
print(f"π Checking {file_rel_path}...")
modified = False
# Cleanup trailing whitespace
if cleanup_trailing_whitespace(file_path):
print(f" β
Removed trailing whitespace")
modified = True
# Check line length
long_lines = check_pep8_line_length(file_path)
if long_lines:
print(f" β οΈ Found {len(long_lines)} lines exceeding 120 characters:")
for issue in long_lines[:3]: # Show first 3
print(issue)
if len(long_lines) > 3:
print(f" ... and {len(long_lines) - 3} more")
if modified:
files_modified += 1
else:
print(f" β No changes needed")
print()
print("β" * 50)
print(f"β
Cleanup complete!")
print(f"π Files checked: {total_files}")
print(f"βοΈ Files modified: {files_modified}")
print("β" * 50)
# Additional recommendations
print("\nπ Additional Recommendations:")
print(" 1. Run: pip install black flake8 # Install linters")
print(" 2. Run: black backend/ # Auto-format code")
print(" 3. Run: flake8 backend/ # Check PEP 8 compliance")
print(" 4. Add .flake8 config to exclude tests/ from strict checks")
if __name__ == '__main__':
main()
|