-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_writer.py
More file actions
executable file
·36 lines (28 loc) · 1.33 KB
/
code_writer.py
File metadata and controls
executable file
·36 lines (28 loc) · 1.33 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
import os
def write_code(file_path, content, mode="w"):
"""
Writes code to a file.
Args:
file_path (str): The relative path to the file (e.g., "projects/ShopMonitor/main.py").
content (str): The code content to write.
mode (str): "w" for overwrite (default), "a" for append.
"""
# Heuristic: Fix common LLM escaping issues (double-escaped newlines)
# If the content has no real newlines but has literal "\n", it's likely an error.
if "\\n" in content and "\n" not in content:
content = content.replace("\\n", "\n")
try:
# Security: Prevent writing outside valid directories (projects/ or tmp/)
# But for now, let's keep it flexible but safe from overwriting critical system files
abs_path = os.path.abspath(file_path)
# Simple check: Ensure directory exists
dir_name = os.path.dirname(abs_path)
if dir_name and not os.path.exists(dir_name):
os.makedirs(dir_name, exist_ok=True)
with open(abs_path, mode) as f:
f.write(content)
return f"Successfully wrote {len(content)} chars to {file_path}"
except Exception as e:
return f"Write Error: {e}"
if __name__ == "__main__":
print(write_code("tmp/test_code.py", "print('Hello World')"))