forked from harrischristiansen/generals-bot
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathfix_function_references.py
More file actions
109 lines (89 loc) · 3.57 KB
/
fix_function_references.py
File metadata and controls
109 lines (89 loc) · 3.57 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
#!/usr/bin/env python3
"""
Automated script to fix function references using the parsed mappings.
"""
import re
from pathlib import Path
def load_function_mappings():
"""Load function mappings from the parsed file."""
mappings = {}
with open('function_mappings.txt', 'r', encoding='utf-8') as f:
lines = f.readlines()
for line in lines[1:]: # Skip header
if line.strip():
parts = line.strip().split(',')
if len(parts) >= 3:
func_name = parts[0]
module_name = parts[1]
target_function = parts[2]
mappings[func_name] = {
'module': module_name,
'target': target_function
}
return mappings
def fix_file(file_path, mappings):
"""Fix all function references in a file."""
try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
original_content = content
changes_made = 0
# Fix self.function_name( -> Module.function_name(self,
for func_name, mapping in mappings.items():
pattern = rf'self\.{re.escape(func_name)}\('
replacement = f"{mapping['module']}.{mapping['target']}(self, "
matches = re.findall(pattern, content)
if matches:
content = re.sub(pattern, replacement, content)
changes_made += len(matches)
print(f" Fixed {len(matches)} instances of self.{func_name}(")
# Fix bot.function_name( -> Module.function_name(bot,
for func_name, mapping in mappings.items():
pattern = rf'bot\.{re.escape(func_name)}\('
replacement = f"{mapping['module']}.{mapping['target']}(bot, "
matches = re.findall(pattern, content)
if matches:
content = re.sub(pattern, replacement, content)
changes_made += len(matches)
print(f" Fixed {len(matches)} instances of bot.{func_name}(")
# Only write if changes were made
if content != original_content:
with open(file_path, 'w', encoding='utf-8') as f:
f.write(content)
return changes_made
else:
return 0
except Exception as e:
print(f"Error processing {file_path}: {e}")
return 0
def main():
print("🔧 Fixing function references automatically...")
mappings = load_function_mappings()
print(f"Loaded {len(mappings)} function mappings")
total_changes = 0
# Fix bot_ek0x45.py first
bot_file = Path('bot_ek0x45.py')
if bot_file.exists():
print(f"\n📁 Fixing {bot_file}...")
changes = fix_file(bot_file, mappings)
total_changes += changes
if changes > 0:
print(f" ✅ Fixed {changes} references")
# Fix BotModules directory
botmodules_dir = Path('BotModules')
if botmodules_dir.exists():
print(f"\n📁 Fixing BotModules directory...")
for py_file in sorted(botmodules_dir.glob('*.py')):
print(f" 📄 {py_file.name}...")
changes = fix_file(py_file, mappings)
total_changes += changes
if changes > 0:
print(f" ✅ Fixed {changes} references")
print(f"\n🎉 Total fixes applied: {total_changes}")
if total_changes > 0:
print("\n✅ All function references have been updated!")
print("💡 You may want to run the test again to see if it passes now.")
else:
print("\nℹ️ No changes were needed.")
if __name__ == "__main__":
main()