From beaf11cc89c7d91840be2678b25f495361aee6d1 Mon Sep 17 00:00:00 2001 From: garylin2099 Date: Mon, 3 Jun 2024 22:47:50 +0800 Subject: [PATCH] update editor --- metagpt/tools/libs/editor.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/metagpt/tools/libs/editor.py b/metagpt/tools/libs/editor.py index e032dcef5..354e5330d 100644 --- a/metagpt/tools/libs/editor.py +++ b/metagpt/tools/libs/editor.py @@ -25,8 +25,11 @@ class Editor: self.resource = EditorReporter() def write(self, path: str, content: str): - """Write the whole content to a file.""" - with open(path, "w") as f: + """Write the whole content to a file. When used, make sure content arg contains the full content of the file.""" + directory = os.path.dirname(path) + if not os.path.exists(directory): + os.makedirs(directory) + with open(path, "w", encoding="utf-8") as f: f.write(content) self.resource.report(path, "path") @@ -117,7 +120,7 @@ class Editor: file_path (str): The file path to write the new block content. start_line (int): start line of the original block to be updated (inclusive). end_line (int): end line of the original block to be updated (inclusive). - new_block_content (str): The new block content to write. + new_block_content (str): The new block content to write. Don't include row number in the content. Returns: str: A message indicating the status of the write operation. @@ -161,7 +164,9 @@ class Editor: if new_block_content: # Split the new_block_content by newline and ensure each line ends with a newline character - new_content_lines = [line + "\n" for line in new_block_content.split("\n")] + new_content_lines = new_block_content.splitlines( + keepends=True + ) # FIXME: This will split \n within a line, such as ab\ncd if end_line >= start_line: # This replaces the block between start_line and end_line with new_block_content # irrespective of the length difference between the original and new content.