forked from DreamLab-AI/origin-logseq-AR
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathrefactor_responses.py
More file actions
executable file
·249 lines (203 loc) · 8.6 KB
/
refactor_responses.py
File metadata and controls
executable file
·249 lines (203 loc) · 8.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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#!/usr/bin/env python3
"""
HTTP Response Standardization Script
Task 1.4 - Phase 1: API Specialist Agent
Replaces direct HttpResponse construction with response macros
"""
import re
import os
import sys
from pathlib import Path
from typing import List, Tuple, Dict
# Refactoring patterns
PATTERNS = [
# Pattern 1: HttpResponse::Ok().json(...) → ok_json!(...)
{
'name': 'Ok responses',
'pattern': r'HttpResponse::Ok\(\)\.json\(([^)]+)\)',
'replacement': r'ok_json!(\1)',
'macro': 'ok_json'
},
# Pattern 2: return HttpResponse::Ok().json(...) → return ok_json!(...)
{
'name': 'Ok responses with return',
'pattern': r'return\s+HttpResponse::Ok\(\)\.json\(([^)]+)\)',
'replacement': r'return ok_json!(\1)',
'macro': 'ok_json'
},
# Pattern 3: HttpResponse::InternalServerError().json(...) → error_json!(...)
{
'name': 'Internal server errors',
'pattern': r'HttpResponse::InternalServerError\(\)\.json\([^)]*\)',
'replacement': lambda m: extract_error_message(m.group(0)),
'macro': 'error_json'
},
# Pattern 4: HttpResponse::BadRequest().json(...) → bad_request!(...)
{
'name': 'Bad request responses',
'pattern': r'HttpResponse::BadRequest\(\)\.json\([^)]*\)',
'replacement': lambda m: extract_error_message(m.group(0), 'bad_request'),
'macro': 'bad_request'
},
# Pattern 5: HttpResponse::NotFound().json(...) → not_found!(...)
{
'name': 'Not found responses',
'pattern': r'HttpResponse::NotFound\(\)\.json\([^)]*\)',
'replacement': lambda m: extract_error_message(m.group(0), 'not_found'),
'macro': 'not_found'
},
# Pattern 6: HttpResponse::Created().json(...) → created_json!(...)
{
'name': 'Created responses',
'pattern': r'HttpResponse::Created\(\)\.json\(([^)]+)\)',
'replacement': r'created_json!(\1)',
'macro': 'created_json'
},
]
# Required imports
REQUIRED_IMPORTS = """
// Response macros - Task 1.4 HTTP Standardization
use crate::{ok_json, error_json, bad_request, not_found, created_json};
use crate::utils::handler_commons::HandlerResponse;
"""
def extract_error_message(match_str: str, macro_name: str = 'error_json') -> str:
"""Extract error message from HttpResponse::InternalServerError().json(...) patterns."""
# Try to extract "error": "message" pattern
error_match = re.search(r'"error":\s*"([^"]+)"', match_str)
if error_match:
return f'{macro_name}!("{error_match.group(1)}")'
# Try to extract "message": value pattern
msg_match = re.search(r'"message":\s*([^,}]+)', match_str)
if msg_match:
msg_value = msg_match.group(1).strip()
# Remove quotes if it's a string literal
if msg_value.startswith('"') and msg_value.endswith('"'):
return f'{macro_name}!({msg_value})'
else:
return f'{macro_name}!({msg_value}.to_string())'
# Fallback: just use generic error
return f'{macro_name}!("Internal server error")'
def add_imports_if_needed(content: str) -> str:
"""Add necessary imports if not already present."""
# Check if imports already exist
if 'use crate::ok_json' in content or 'use crate::{ok_json' in content:
return content
# Find the last 'use' statement
use_statements = list(re.finditer(r'^use\s+', content, re.MULTILINE))
if use_statements:
# Insert after the last use statement
last_use = use_statements[-1]
# Find the end of this use statement (semicolon)
semicolon_pos = content.find(';', last_use.start())
if semicolon_pos != -1:
# Insert imports after the semicolon and newline
insertion_point = semicolon_pos + 1
return content[:insertion_point] + '\n' + REQUIRED_IMPORTS + content[insertion_point:]
return content
def refactor_file(file_path: Path) -> Tuple[int, List[str]]:
"""Refactor a single file and return count of replacements and changes made."""
try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# Skip if no HttpResponse found
if 'HttpResponse::' not in content:
return 0, []
original_content = content
changes = []
total_replacements = 0
# Add imports
content = add_imports_if_needed(content)
# Apply each pattern
for pattern_def in PATTERNS:
pattern = pattern_def['pattern']
replacement = pattern_def['replacement']
matches = list(re.finditer(pattern, content))
if matches:
if callable(replacement):
# For complex replacements with functions
for match in reversed(matches): # Reverse to maintain positions
new_text = replacement(match)
content = content[:match.start()] + new_text + content[match.end():]
total_replacements += 1
changes.append(f" - {pattern_def['name']}: {match.group(0)[:50]}...")
else:
# For simple regex replacements
content, count = re.subn(pattern, replacement, content)
total_replacements += count
if count > 0:
changes.append(f" - {pattern_def['name']}: {count} occurrences")
# Only write if changes were made
if content != original_content:
with open(file_path, 'w', encoding='utf-8') as f:
f.write(content)
return total_replacements, changes
except Exception as e:
print(f"ERROR processing {file_path}: {e}", file=sys.stderr)
return 0, [f"ERROR: {e}"]
def main():
"""Main refactoring function."""
handlers_dir = Path("/home/devuser/workspace/project/src/handlers")
if not handlers_dir.exists():
print(f"ERROR: Handlers directory not found: {handlers_dir}")
sys.exit(1)
print("=" * 70)
print("HTTP Response Standardization - Task 1.4")
print("=" * 70)
print(f"Target directory: {handlers_dir}\n")
# Find all Rust files
rust_files = list(handlers_dir.rglob("*.rs"))
print(f"Found {len(rust_files)} Rust files\n")
total_files_changed = 0
total_replacements = 0
detailed_changes: Dict[str, List[str]] = {}
# Process each file
for rust_file in sorted(rust_files):
rel_path = rust_file.relative_to(handlers_dir)
count, changes = refactor_file(rust_file)
if count > 0:
total_files_changed += 1
total_replacements += count
detailed_changes[str(rel_path)] = changes
print(f"✓ {rel_path}: {count} replacements")
# Summary
print("\n" + "=" * 70)
print("SUMMARY")
print("=" * 70)
print(f"Files processed: {len(rust_files)}")
print(f"Files changed: {total_files_changed}")
print(f"Total replacements: {total_replacements}")
# Verify remaining direct HttpResponse usage
print("\n" + "=" * 70)
print("VERIFICATION")
print("=" * 70)
remaining_count = 0
for rust_file in rust_files:
with open(rust_file, 'r', encoding='utf-8') as f:
content = f.read()
# Count non-standard HttpResponse usage (excluding imports)
lines = content.split('\n')
for line in lines:
if 'HttpResponse::' in line and \
'use actix' not in line and \
'response_macros' not in line and \
'handler_commons' not in line:
remaining_count += 1
print(f"Remaining direct HttpResponse usages: {remaining_count}")
if remaining_count == 0:
print("\n✓ SUCCESS: All HTTP responses standardized!")
else:
print(f"\n⚠ WARNING: {remaining_count} direct HttpResponse usages still remain")
print("Manual review may be required for complex cases")
# Detailed changes
if detailed_changes and len(detailed_changes) <= 10:
print("\n" + "=" * 70)
print("DETAILED CHANGES")
print("=" * 70)
for file_name, changes in detailed_changes.items():
print(f"\n{file_name}:")
for change in changes:
print(change)
print("\nRefactoring complete!")
return 0 if remaining_count == 0 else 1
if __name__ == "__main__":
sys.exit(main())