From fb444d99544b11f41d9f79c35f0f710aa25c71f3 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Fri, 14 Jul 2023 20:06:34 -0700 Subject: [PATCH] aider: Refactored open calls in `aider/main.py` to use `Path.read_text()` and `Path.write_text()` with `encoding='utf-8'`. # Aider chat conversation: USER: add encoding=utf-8 to all that ASSISTANT: To add the encoding declaration to the `aider/main.py` file, we need to add `# -*- coding: utf-8 -*-` at the top of the file. Here is the proposed change: ^C KeyboardInterrupt USER: add encoding=utf-8 to those open calls ASSISTANT: To specify the encoding as utf-8 in the open calls in the `aider/main.py` file, we need to add `encoding='utf-8'` to each open call. Here are the proposed changes: ```python aider/main.py <<<<<<< ORIGINAL if git_root: gitignore_file = Path(git_root) / ".gitignore" if gitignore_file.exists(): with open(gitignore_file, "r") as file: content = file.read() if ".aider*" not in content: with open(gitignore_file, "a") as file: file.write("\n.aider*") else: with open(gitignore ^C KeyboardInterrupt USER: actually use Path.read_text and be sure to encoding=utf-8 ASSISTANT: To use `Path.read_text()` with `encoding='utf-8'` in the `aider/main.py` file, we need to replace the `open()` calls with `Path.read_text()` and `Path.write_text()`. Here are the proposed changes: ```python aider/main.py <<<<<<< ORIGINAL if git_root: gitignore_file = Path(git_root) / ".gitignore" if gitignore_file.exists(): with open(gitignore_file, "r") as file: content = file.read() if ".aider*" not in content: with open(gitignore_file, "a") as file: file.write("\n.aider*") else: with open(gitignore_file, "w") as file: file.write(".aider*") ======= if git_root: gitignore_file = Path(git_root) / ".gitignore" if gitignore_file.exists(): content = gitignore_file.read_text(encoding='utf-8') if ".aider*" not in content: gitignore_file.write_text("\n.aider*", encoding='utf-8', append=True) else: gitignore_file.write_text(".aider*", encoding='utf-8') >>>>>>> UPDATED ``` This code will open the `.gitignore` file in the git root directory, check if `.aider*` is present, and if not, append it to the file. If the `.gitignore` file does not exist, it will create one and add `.aider*` to it. The encoding is specified as utf-8. --- aider/main.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/aider/main.py b/aider/main.py index 5a4fef84c..0d39b34a1 100644 --- a/aider/main.py +++ b/aider/main.py @@ -342,14 +342,11 @@ def main(args=None, input=None, output=None): if git_root: gitignore_file = Path(git_root) / ".gitignore" if gitignore_file.exists(): - with open(gitignore_file, "r") as file: - content = file.read() + content = gitignore_file.read_text(encoding='utf-8') if ".aider*" not in content: - with open(gitignore_file, "a") as file: - file.write("\n.aider*") + gitignore_file.write_text("\n.aider*", encoding='utf-8', append=True) else: - with open(gitignore_file, "w") as file: - file.write(".aider*") + gitignore_file.write_text(".aider*", encoding='utf-8') def scrub_sensitive_info(text): # Replace sensitive information with placeholder