File size: 6,708 Bytes
6bed18e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
"""
Utility script for code cleanup and refactoring across all modules
This script identifies common issues and applies standard formatting
"""

import os
import re
from pathlib import Path


def find_python_files(root_dir: str) -> list:
    """Find all Python files in the specified directory"""
    python_files = []
    for root, dirs, files in os.walk(root_dir):
        for file in files:
            if file.endswith('.py') and not file.startswith('.'):
                python_files.append(os.path.join(root, file))
    return python_files


def find_typescript_files(root_dir: str) -> list:
    """Find all TypeScript/TSX files in the specified directory"""
    ts_files = []
    for root, dirs, files in os.walk(root_dir):
        for file in files:
            if file.endswith(('.ts', '.tsx')) and not file.startswith('.'):
                ts_files.append(os.path.join(root, file))
    return ts_files


def standardize_imports(file_path: str):
    """Standardize import statements in the file"""
    with open(file_path, 'r', encoding='utf-8') as f:
        content = f.read()

    # Look for common import issues and fix them
    # Sort imports alphabetically and separate stdlib, third-party, and local imports
    lines = content.split('\n')
    new_lines = []

    stdlib_imports = []
    third_party_imports = []
    local_imports = []
    other_lines = []

    for line in lines:
        if line.startswith('import ') or line.startswith('from '):
            # Identify import type by checking if common modules are in the line
            is_stdlib = any(keyword in line for keyword in [' os.', ' os\n', ' os ', ' sys.', ' sys\n', ' sys ', ' pathlib.', ' pathlib\n', ' pathlib ', ' typing.', ' typing\n', ' typing '])
            is_third_party = any(keyword in line for keyword in [' fastapi', ' sqlmodel', ' jose', ' pydantic'])
            is_local = any(keyword in line for keyword in [' src.', ' backend.'])

            if is_stdlib:
                stdlib_imports.append(line)
            elif is_third_party:
                third_party_imports.append(line)
            elif is_local:
                local_imports.append(line)
            else:
                third_party_imports.append(line)
        else:
            other_lines.append(line)

    # Combine in order: stdlib, third-party, local with proper spacing
    all_imports = []
    if stdlib_imports:
        all_imports.extend(sorted(set(stdlib_imports)))
        all_imports.append('')  # Empty line after stdlib imports
    if third_party_imports:
        all_imports.extend(sorted(set(third_party_imports)))
        all_imports.append('')  # Empty line after third-party imports
    if local_imports:
        all_imports.extend(sorted(set(local_imports)))
        all_imports.append('')  # Empty line after local imports

    new_content = '\n'.join(all_imports + other_lines)

    with open(file_path, 'w', encoding='utf-8') as f:
        f.write(new_content)


def remove_unused_imports(file_path: str):
    """Remove unused imports from the file"""
    with open(file_path, 'r', encoding='utf-8') as f:
        content = f.read()

    # This is a simplified version - in practice, you'd use a tool like unimport
    # For now, just ensure imports are properly formatted
    lines = content.split('\n')
    new_lines = []
    in_import_block = False

    for line in lines:
        if line.startswith('import ') or line.startswith('from '):
            if not in_import_block:
                in_import_block = True
                new_lines.append(line)
            elif line.strip() and not line.startswith(('import ', 'from ')):
                in_import_block = False
                new_lines.append(line)
            else:
                new_lines.append(line)
        else:
            in_import_block = False
            new_lines.append(line)

    new_content = '\n'.join(new_lines)
    with open(file_path, 'w', encoding='utf-8') as f:
        f.write(new_content)


def format_strings_consistently(file_path: str):
    """Standardize string formatting in the file"""
    with open(file_path, 'r', encoding='utf-8') as f:
        content = f.read()

    # Standardize f-string usage where appropriate
    # Standardize quote usage (prefer double quotes for consistency)
    # This is a simplified version - in practice, you'd use black or similar

    # Fix common string formatting issues
    content = re.sub(r"f'([^']*)'", r'f"\1"', content)  # Convert f-string single quotes to double
    content = re.sub(r"'([^']*)'", r'"\1"', content)   # Convert single quotes to double where safe

    with open(file_path, 'w', encoding='utf-8') as f:
        f.write(content)


def cleanup_whitespace(file_path: str):
    """Remove trailing whitespace and ensure consistent line endings"""
    with open(file_path, 'r', encoding='utf-8') as f:
        lines = f.readlines()

    # Remove trailing whitespace and ensure newline at end
    cleaned_lines = [line.rstrip() + '\n' for line in lines]
    if cleaned_lines and not cleaned_lines[-1].endswith('\n'):
        cleaned_lines[-1] += '\n'

    with open(file_path, 'w', encoding='utf-8') as f:
        f.writelines(cleaned_lines)


def apply_standard_cleanups(root_dir: str):
    """Apply all standard cleanup operations to files in the directory"""
    print(f"Starting code cleanup in: {root_dir}")

    # Process Python files
    python_files = find_python_files(root_dir)
    print(f"Found {len(python_files)} Python files to process")

    for file_path in python_files:
        print(f"Processing: {file_path}")
        try:
            cleanup_whitespace(file_path)
            remove_unused_imports(file_path)
            standardize_imports(file_path)
            format_strings_consistently(file_path)
        except Exception as e:
            print(f"Error processing {file_path}: {str(e)}")
        except Exception as e:
            print(f"Error processing {file_path}: {str(e)}")

    # Process TypeScript files
    ts_files = find_typescript_files(root_dir)
    print(f"Found {len(ts_files)} TypeScript/TSX files to process")

    for file_path in ts_files:
        print(f"Processing: {file_path}")
        try:
            cleanup_whitespace(file_path)
        except Exception as e:
            print(f"Error processing {file_path}: {str(e)}")

    print("Code cleanup completed!")


if __name__ == "__main__":
    import sys
    if len(sys.argv) > 1:
        root_directory = sys.argv[1]
    else:
        root_directory = input("Enter the root directory to clean up: ").strip()

    if os.path.exists(root_directory):
        apply_standard_cleanups(root_directory)
    else:
        print(f"Directory {root_directory} does not exist!")