Repository navigation
Expand file tree
/
Copy pathmigrate_errors.py
More file actions
135 lines (108 loc) · 4.64 KB
/
Copy pathmigrate_errors.py
File metadata and controls
135 lines (108 loc) · 4.64 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
#!/usr/bin/env python3
"""
Script to migrate all Scala files to use the new error handling system.
"""
import os
import re
import sys
def add_imports(content):
"""Add necessary imports if not already present"""
if "import org.llm4s.szork.error._" not in content:
# Find the last import line
import_lines = []
for i, line in enumerate(content.split('\n')):
if line.startswith('import '):
import_lines.append(i)
if import_lines:
last_import_idx = import_lines[-1]
lines = content.split('\n')
# Add imports after last import
lines.insert(last_import_idx + 1, "import org.llm4s.szork.error._")
lines.insert(last_import_idx + 2, "import org.llm4s.szork.error.ErrorHandling._")
content = '\n'.join(lines)
else:
# Add after package declaration
lines = content.split('\n')
for i, line in enumerate(lines):
if line.startswith('package '):
lines.insert(i + 1, "")
lines.insert(i + 2, "import org.llm4s.szork.error._")
lines.insert(i + 3, "import org.llm4s.szork.error.ErrorHandling._")
break
content = '\n'.join(lines)
return content
def migrate_file(filepath):
"""Migrate a single Scala file"""
# Skip already migrated files and error handling files themselves
if 'error/SzorkError.scala' in filepath or 'error/ErrorHandling.scala' in filepath:
return False
with open(filepath, 'r') as f:
content = f.read()
original_content = content
# Check if file needs migration
needs_migration = False
if 'Either[String,' in content or 'Either[List[String],' in content:
needs_migration = True
if not needs_migration:
return False
# Add imports
content = add_imports(content)
# Replace type signatures
content = re.sub(r':\s*Either\[String,\s*([^]]+)\]', r': SzorkResult[\1]', content)
content = re.sub(r':\s*Either\[List\[String\],\s*([^]]+)\]', r': SzorkResult[\1]', content)
# Replace Left(string) with appropriate error types based on context
# This is a simplified heuristic - may need manual refinement
# For persistence operations
if 'save' in filepath.lower() or 'persistence' in filepath.lower():
content = re.sub(r'Left\(([^)]+)\)', r'Left(PersistenceError(\1, "operation"))', content)
# For parsing operations
elif 'parse' in filepath.lower() or 'parser' in filepath.lower():
content = re.sub(r'Left\(([^)]+)\)', r'Left(ParseError(\1))', content)
# For validation
elif 'valid' in filepath.lower():
content = re.sub(r'Left\(([^)]+)\)', r'Left(ValidationError(List(\1)))', content)
# For media generation
elif 'music' in filepath.lower():
content = re.sub(r'Left\(([^)]+)\)', r'Left(MusicGenerationError(\1))', content)
elif 'tts' in filepath.lower() or 'speech' in filepath.lower():
content = re.sub(r'Left\(([^)]+)\)', r'Left(AudioGenerationError(\1))', content)
elif 'image' in filepath.lower():
content = re.sub(r'Left\(([^)]+)\)', r'Left(ImageGenerationError(\1))', content)
# For network operations
elif 'network' in filepath.lower() or 'api' in filepath.lower():
content = re.sub(r'Left\(([^)]+)\)', r'Left(NetworkError(\1, "service"))', content)
# Default to GameStateError
else:
content = re.sub(r'Left\(([^)]+)\)', r'Left(GameStateError(\1))', content)
# Make loggers implicit if they're not already
content = re.sub(r'private val logger = LoggerFactory', r'private implicit val logger = LoggerFactory', content)
# Write back only if changed
if content != original_content:
with open(filepath, 'w') as f:
f.write(content)
print(f"Migrated: {filepath}")
return True
return False
def main():
"""Migrate all Scala files in the project"""
src_dir = 'src/main/scala'
migrated_count = 0
for root, dirs, files in os.walk(src_dir):
for file in files:
if file.endswith('.scala'):
filepath = os.path.join(root, file)
if migrate_file(filepath):
migrated_count += 1
print(f"\nMigration complete. {migrated_count} files migrated.")
# List files that might need manual review
print("\nFiles that may need manual review:")
manual_review = [
'TypedWebSocketServer.scala',
'SzorkServer.scala',
'StreamingAgent.scala',
'GameTools.scala'
]
for file in manual_review:
print(f" - {file}")
if __name__ == '__main__':
main()