118 lines
3.6 KiB
Python
118 lines
3.6 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Script to fix code quality issues in the karaoke downloader codebase.
|
|
This script addresses:
|
|
1. Unused imports (F401)
|
|
2. F-string missing placeholders (F541)
|
|
3. Unused variables (F841)
|
|
4. Missing type annotations
|
|
"""
|
|
|
|
import re
|
|
import subprocess
|
|
from pathlib import Path
|
|
from typing import List, Set
|
|
|
|
|
|
def fix_unused_imports(file_path: Path) -> None:
|
|
"""Remove unused imports from a file."""
|
|
content = file_path.read_text(encoding='utf-8')
|
|
lines = content.split('\n')
|
|
|
|
# Track which imports are actually used
|
|
used_imports: Set[str] = set()
|
|
|
|
# Find all import statements
|
|
import_pattern = re.compile(r'^from\s+(\S+)\s+import\s+(.+)$')
|
|
import_simple_pattern = re.compile(r'^import\s+(.+)$')
|
|
|
|
for line in lines:
|
|
# Check for from ... import statements
|
|
match = import_pattern.match(line.strip())
|
|
if match:
|
|
module = match.group(1)
|
|
imports = match.group(2)
|
|
# Parse individual imports
|
|
for imp in imports.split(','):
|
|
imp = imp.strip()
|
|
if ' as ' in imp:
|
|
imp = imp.split(' as ')[0].strip()
|
|
used_imports.add(imp)
|
|
|
|
# Check for simple import statements
|
|
match = import_simple_pattern.match(line.strip())
|
|
if match:
|
|
module = match.group(1)
|
|
if ' as ' in module:
|
|
module = module.split(' as ')[1].strip()
|
|
used_imports.add(module)
|
|
|
|
# Check which imports are actually used in the code
|
|
code_content = '\n'.join(lines)
|
|
actually_used: Set[str] = set()
|
|
|
|
for imp in used_imports:
|
|
if imp in code_content:
|
|
actually_used.add(imp)
|
|
|
|
print(f"Used imports in {file_path.name}: {actually_used}")
|
|
|
|
|
|
def fix_f_string_placeholders(file_path: Path) -> None:
|
|
"""Fix f-strings that are missing placeholders."""
|
|
content = file_path.read_text(encoding='utf-8')
|
|
lines = content.split('\n')
|
|
|
|
f_string_pattern = re.compile(r'f"([^"]*)"')
|
|
|
|
for i, line in enumerate(lines):
|
|
matches = f_string_pattern.findall(line)
|
|
for match in matches:
|
|
if not re.search(r'\{[^}]*\}', match):
|
|
print(f"F541: Line {i+1} in {file_path.name}: f-string missing placeholders")
|
|
print(f" {line.strip()}")
|
|
|
|
|
|
def fix_unused_variables(file_path: Path) -> None:
|
|
"""Find unused variables."""
|
|
content = file_path.read_text(encoding='utf-8')
|
|
lines = content.split('\n')
|
|
|
|
# Pattern to find variable assignments
|
|
var_pattern = re.compile(r'^\s*(\w+)\s*=\s*')
|
|
|
|
for i, line in enumerate(lines):
|
|
match = var_pattern.match(line)
|
|
if match:
|
|
var_name = match.group(1)
|
|
# Check if variable is used later in the file
|
|
remaining_content = '\n'.join(lines[i+1:])
|
|
if var_name not in remaining_content:
|
|
print(f"F841: Line {i+1} in {file_path.name}: unused variable '{var_name}'")
|
|
|
|
|
|
def main():
|
|
"""Main function to fix code quality issues."""
|
|
karaoke_dir = Path("karaoke_downloader")
|
|
|
|
if not karaoke_dir.exists():
|
|
print("Error: karaoke_downloader directory not found")
|
|
return
|
|
|
|
python_files = list(karaoke_dir.glob("*.py"))
|
|
|
|
print("🔍 Analyzing code quality issues...")
|
|
print("=" * 50)
|
|
|
|
for file_path in python_files:
|
|
print(f"\n📁 {file_path.name}:")
|
|
fix_unused_imports(file_path)
|
|
fix_f_string_placeholders(file_path)
|
|
fix_unused_variables(file_path)
|
|
|
|
print("\n" + "=" * 50)
|
|
print("✅ Analysis complete!")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main() |