Итерация 3, финальная
This commit is contained in:
@@ -6,6 +6,8 @@ import re
|
||||
@tool
|
||||
def read_file(path: str, start_line: int = 0, end_line: int = None) -> str:
|
||||
"""Прочитать содержимое файла. Можно указать диапазон строк (1-индекс)."""
|
||||
if not os.path.exists(path):
|
||||
return f"File not found: {path}"
|
||||
with open(path, 'r', encoding='utf-8') as f:
|
||||
lines = f.readlines()
|
||||
if start_line > 0:
|
||||
@@ -17,7 +19,10 @@ def read_file(path: str, start_line: int = 0, end_line: int = None) -> str:
|
||||
@tool
|
||||
def write_file(path: str, content: str) -> str:
|
||||
"""Создать или перезаписать файл с указанным содержимым."""
|
||||
os.makedirs(os.path.dirname(path), exist_ok=True)
|
||||
# Создаем директорию только если путь содержит директорию
|
||||
dir_name = os.path.dirname(path)
|
||||
if dir_name:
|
||||
os.makedirs(dir_name, exist_ok=True)
|
||||
with open(path, 'w', encoding='utf-8') as f:
|
||||
f.write(content)
|
||||
return f"Файл {path} записан"
|
||||
@@ -25,20 +30,24 @@ def write_file(path: str, content: str) -> str:
|
||||
@tool
|
||||
def delete_lines(path: str, start_line: int, end_line: int) -> str:
|
||||
"""Удалить строки из файла (1-индекс, включительно)."""
|
||||
with open(path, 'r') as f:
|
||||
if not os.path.exists(path):
|
||||
return f"File not found: {path}"
|
||||
with open(path, 'r', encoding='utf-8') as f:
|
||||
lines = f.readlines()
|
||||
del lines[start_line-1:end_line]
|
||||
with open(path, 'w') as f:
|
||||
with open(path, 'w', encoding='utf-8') as f:
|
||||
f.writelines(lines)
|
||||
return f"Удалены строки {start_line}-{end_line} из {path}"
|
||||
|
||||
@tool
|
||||
def replace_text_in_file(path: str, old: str, new: str) -> str:
|
||||
"""Заменить все вхождения old на new в файле."""
|
||||
with open(path, 'r') as f:
|
||||
if not os.path.exists(path):
|
||||
return f"File not found: {path}"
|
||||
with open(path, 'r', encoding='utf-8') as f:
|
||||
content = f.read()
|
||||
new_content = content.replace(old, new)
|
||||
with open(path, 'w') as f:
|
||||
with open(path, 'w', encoding='utf-8') as f:
|
||||
f.write(new_content)
|
||||
return f"Заменено {content.count(old)} вхождений"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user