61 lines
2.1 KiB
Python
61 lines
2.1 KiB
Python
import os
|
|
from typing import List
|
|
import subprocess
|
|
def internal_listFiles(path: str) -> List[str]:
|
|
"""List all files in the given path"""
|
|
if os.path.exists(path) and os.path.isdir(path):
|
|
result = os.listdir(path)
|
|
return result
|
|
print(f"Path does not exist or is not a directory: {path}")
|
|
return []
|
|
|
|
def internal_readFile(path: str) -> str:
|
|
"""Read the contents of a file"""
|
|
if os.path.exists(path) and os.path.isfile(path):
|
|
try:
|
|
with open(path, "r", encoding="utf-8") as f:
|
|
content = f.read()
|
|
return content
|
|
except Exception as e:
|
|
error_msg = f"Error reading file: {str(e)}"
|
|
return error_msg
|
|
return ""
|
|
|
|
def internal_writeFile(path: str, content: str) -> bool:
|
|
"""Write the contents of a file"""
|
|
try:
|
|
with open(path, "w", encoding="utf-8") as f:
|
|
f.write(content)
|
|
return True
|
|
except Exception as e:
|
|
print(f"Error writing file: {str(e)}")
|
|
return False
|
|
|
|
def internal_executeCommand(command: str) -> str:
|
|
"""Execute a command"""
|
|
try:
|
|
result = subprocess.run(command, shell=True, capture_output=True, text=True)
|
|
output = {
|
|
"stderr": result.stderr,
|
|
"stdout": result.stdout,
|
|
"returncode": result.returncode
|
|
}
|
|
return output
|
|
except Exception as e:
|
|
print(f"Error executing command: {str(e)}")
|
|
return ""
|
|
|
|
def internal_writePythonFile(path: str, content: str) -> str:
|
|
"""Write a Python file handling streaming and escape characters correctly."""
|
|
content = content.encode('utf-8').decode('unicode_escape') if '\\n' in content else content
|
|
if "```python" in content:
|
|
content = content.split("```python")[1].split("```")[0].strip()
|
|
elif "```" in content:
|
|
content = content.split("```")[1].split("```")[0].strip()
|
|
|
|
try:
|
|
with open(path, "w", encoding="utf-8") as f:
|
|
f.write(content)
|
|
return f"File saved correctly in {path}"
|
|
except Exception as e:
|
|
return f"Error: {str(e)}" |