-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcleanup_code.py
More file actions
174 lines (144 loc) · 5.52 KB
/
cleanup_code.py
File metadata and controls
174 lines (144 loc) · 5.52 KB
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
#!/usr/bin/env python3
import re
import os
def remove_comments_and_emojis(content, file_type='python'):
"""Remove comments and emojis from code while preserving functionality"""
if file_type == 'python':
lines = content.split('\n')
cleaned_lines = []
in_multiline = False
multiline_char = None
for line in lines:
stripped = line.lstrip()
if in_multiline:
if multiline_char in line:
in_multiline = False
continue
if stripped.startswith('"""') or stripped.startswith("'''"):
multiline_char = '"""' if '"""' in line else "'''"
if line.count(multiline_char) == 2:
continue
else:
in_multiline = True
continue
if '#' in line:
code_part = line.split('#')[0]
if code_part.strip():
line = code_part
else:
continue
line = remove_emojis(line)
if line.strip():
cleaned_lines.append(line)
elif not line.strip() and cleaned_lines and cleaned_lines[-1].strip():
cleaned_lines.append('')
return '\n'.join(cleaned_lines)
elif file_type == 'javascript':
lines = content.split('\n')
cleaned_lines = []
in_multiline = False
for line in lines:
if '/*' in line and '*/' in line:
comment_start = line.find('/*')
comment_end = line.find('*/')
if comment_start < comment_end:
line = line[:comment_start] + line[comment_end+2:]
elif '/*' in line:
in_multiline = True
comment_start = line.find('/*')
line = line[:comment_start]
elif in_multiline and '*/' in line:
in_multiline = False
comment_end = line.find('*/')
line = line[comment_end+2:]
elif in_multiline:
continue
if '//' in line:
comment_start = line.find('//')
line = line[:comment_start]
line = remove_emojis(line)
if line.strip():
cleaned_lines.append(line)
elif not line.strip() and cleaned_lines and cleaned_lines[-1].strip():
cleaned_lines.append('')
return '\n'.join(cleaned_lines)
elif file_type == 'html':
content = remove_emojis(content)
lines = content.split('\n')
cleaned_lines = []
for line in lines:
if '<!--' in line and '-->' in line:
comment_start = line.find('<!--')
comment_end = line.find('-->')
line = line[:comment_start] + line[comment_end+3:]
if line.strip():
cleaned_lines.append(line)
elif not line.strip() and cleaned_lines and cleaned_lines[-1].strip():
cleaned_lines.append('')
return '\n'.join(cleaned_lines)
return content
def remove_emojis(text):
"""Remove emoji characters from text"""
emoji_pattern = re.compile(
"["
"\U0001F600-\U0001F64F"
"\U0001F300-\U0001F5FF"
"\U0001F680-\U0001F6FF"
"\U0001F1E0-\U0001F1FF"
"\U00002702-\U000027B0"
"\U000024C2-\U0001F251"
"\U0001f926-\U0001f937"
"\U00010000-\U0010ffff"
"\u2640-\u2642"
"\u2600-\u2B55"
"\u200d"
"\u23cf"
"\u23e9"
"\u231a"
"\ufe0f"
"\u3030"
"]+",
flags=re.UNICODE
)
return emoji_pattern.sub('', text)
def clean_file(filepath, file_type):
"""Clean a single file"""
try:
with open(filepath, 'r', encoding='utf-8') as f:
content = f.read()
cleaned_content = remove_comments_and_emojis(content, file_type)
with open(filepath, 'w', encoding='utf-8') as f:
f.write(cleaned_content)
return True, f"Cleaned {filepath}"
except Exception as e:
return False, f"Error cleaning {filepath}: {str(e)}"
def main():
files_to_clean = [
('/home/ahad/Desktop/help/enhanced_api.py', 'python'),
('/home/ahad/Desktop/help/cms/ids_manager.py', 'python'),
('/home/ahad/Desktop/help/cms/deployment_manager.py', 'python'),
('/home/ahad/Desktop/help/cms/health_monitor.py', 'python'),
('/home/ahad/Desktop/help/static/js/dashboard.js', 'javascript'),
('/home/ahad/Desktop/help/templates/dashboard.html', 'html'),
]
print("=" * 70)
print("CODE CLEANUP UTILITY")
print("Removing comments and emojis from all files")
print("=" * 70)
print()
success_count = 0
for filepath, ftype in files_to_clean:
if os.path.exists(filepath):
success, message = clean_file(filepath, ftype)
status = "[OK]" if success else "[ERROR]"
print(f"{status} {message}")
if success:
success_count += 1
else:
print(f"[SKIP] File not found: {filepath}")
print()
print("=" * 70)
print(f"Cleanup complete: {success_count}/{len([f for f in files_to_clean if os.path.exists(f[0])])} files processed")
print("=" * 70)
if __name__ == '__main__':
main()