-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclean_submit_code.py
More file actions
executable file
·55 lines (45 loc) · 1.6 KB
/
clean_submit_code.py
File metadata and controls
executable file
·55 lines (45 loc) · 1.6 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
#!/usr/bin/env python3
import sys
import re
import subprocess
import textwrap
def main():
try:
code = subprocess.check_output(['pbpaste']).decode('utf-8')
except Exception as e:
print(f"Failed to read pasteboard: {e}", file=sys.stderr)
sys.exit(1)
# Clean trailing whitespace from each line
lines = [line.rstrip() for line in code.splitlines()]
code = '\n'.join(lines) + '\n'
# Dedent the code to fix indentation issues
try:
code = textwrap.dedent(code)
except:
pass # Ignore if dedent fails
# Remove package declaration if present
lines = code.splitlines()
if lines and lines[0].strip().startswith('package '):
lines.pop(0)
code = '\n'.join(lines) + '\n'
print('Removed package declaration')
# Check if it's Java code
if 'public class' not in code:
print('Not Java code')
sys.exit(1)
# Find and rename class (MainPro, MainPlus, etc. to Main)
match = re.search(r'public\s+class\s+(\w+)', code)
if match:
old_class = match.group(1)
if old_class.startswith('Main') and old_class != 'Main' and len(old_class) > 4:
code = re.sub(r'\b' + re.escape(old_class) + r'\b', 'Main', code)
print(f'Renamed {old_class} to Main')
# Copy cleaned code back to pasteboard
try:
subprocess.run(['pbcopy'], input=code.encode('utf-8'), check=True)
except Exception as e:
print(f"Failed to write to pasteboard: {e}", file=sys.stderr)
sys.exit(1)
print('The code is adjusted, please submit')
if __name__ == '__main__':
main()