From 5f9947a7279d6a1e7339f16fca4ac1ed2b56080d Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Wed, 5 Jul 2023 19:33:59 -0700 Subject: [PATCH] aider: Add read_text function to InputOutput class and --encoding command-line switch. # Context: USER: Add a read_text function to the InputOutput class. It takes the file name as argument, and returns the text for that file. Internally, if we get a Unicode error or a FileNotFound error, it prints ToolErrorOutput to notify the user, and then returns None. Add a command-line switch, and dash-dash encoding, which defaults to UTF-8. That's the encoding which will be used by the read_text function. ASSISTANT: To implement the requested changes, we need to modify the following files: 1. `aider/io.py`: To add the `read_text` function to the `InputOutput` class. 2. `aider/main.py`: To add the `--encoding` command-line switch. Let's start with the changes to `aider/io.py`: ```python aider/io.py <<<<<<< ORIGINAL class InputOutput: num_error_outputs = 0 num_user_asks = 0 def __init__( self, pretty=True, yes=False, input_history_file=None, chat_history_file=None, input=None, output=None, user_input_color="blue", tool_output_color=None, tool_error_color="red", ): no_color = os.environ.get("NO_COLOR") if no_color is not None and no_color != "": pretty = False self.user_input_color = user_input_color if pretty else None self.tool_output_color = tool_output_color if pretty else None self.tool_error_color = tool_error_color if pretty else None self.input = input self.output = output self.pretty = pretty if self.output: self.pretty = False self.yes = yes self.input_history_file = input_history_file if chat_history_file is not None: self.chat_history_file = Path(chat_history_file) else: self.chat_history_file = None if pretty: self.console = Console() else: self.console = Console(force_terminal=False, no_color=True) current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S") self.append_chat_history(f"\n# aider chat started at {current_time}\n\n") ======= class InputOutput: num_error_outputs = 0 num_user_asks = 0 def __init__( self, pretty=True, yes=False, input_history_file=None, chat_history_file=None, input=None, output=None, user_input_color="blue", tool_output_color=None, tool_error_color="red", encoding="utf-8", ): no_color = os.environ.get("NO_COLOR") if no_color is not None and no_color != "": pretty = False self.user_input_color = user_input_color if pretty else None self.tool_output_color = tool_output_color if pretty else None self.tool_error_color = tool_error_color if pretty else None self.input = input self.output = output self.pretty = pretty if self.output: self.pretty = False self.yes = yes self.input_history_file = input_history_file if chat_history_file is not None: self.chat_history_file = Path(chat_history_file) else: self.chat_history_file = None self.encoding = encoding if pretty: self.console = Console() else: self.console = Console(force_terminal=False, no_color=True) current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S") self.append_chat_history(f"\n# aider chat started at {current_time}\n\n") def read_text(self, filename): try: with open(filename, "r", encoding=self.encoding) as f: return f.read() except (FileNotFoundError, UnicodeError) as e: self.tool_error(str(e)) return None >>>>>>> UPDATED ``` Now, let's add the `--encoding` command-line switch to `aider/main.py`: ```python aider/main.py <<<<<<< ORIGINAL parser.add_argument( "--no-dirty-commits", action="store_false", dest="dirty_commits", help="Disable commits when repo is found dirty", ) parser.add_argument( "--openai-api-key", metavar="OPENAI_API_KEY", help="Specify the OpenAI API key", env_var="OPENAI_API_KEY", ) parser.add_argument( "--openai-api-base", metavar="OPENAI_API_BASE", default="https://api.openai.com/v1", help="Specify the OpenAI API base endpoint (default: https://api.openai.com/v1)", ) ======= parser.add_argument( "--no-dirty-commits", action="store_false", dest="dirty_commits", help="Disable commits when repo is found dirty", ) parser.add_argument( "--encoding", default="utf-8", help="Specify the encoding to use when reading files (default: utf-8)", ) parser.add_argument( "--openai-api-key", metavar="OPENAI_API_KEY", help="Specify the OpenAI API key", env_var="OPENAI_API_KEY", ) parser.add_argument( "--openai-api-base", metavar="OPENAI_API_BASE", default="https://api.openai.com/v1", help="Specify the OpenAI API base endpoint (default: https://api.openai.com/v1)", ) >>>>>>> UPDATED ``` These changes should implement the requested functionality. Please review and let me know if any further changes are needed. --- aider/io.py | 11 +++++++++++ aider/main.py | 5 +++++ 2 files changed, 16 insertions(+) diff --git a/aider/io.py b/aider/io.py index 1d275716e..ff3cab386 100644 --- a/aider/io.py +++ b/aider/io.py @@ -100,6 +100,7 @@ class InputOutput: user_input_color="blue", tool_output_color=None, tool_error_color="red", + encoding="utf-8", ): no_color = os.environ.get("NO_COLOR") if no_color is not None and no_color != "": @@ -124,6 +125,8 @@ class InputOutput: else: self.chat_history_file = None + self.encoding = encoding + if pretty: self.console = Console() else: @@ -132,6 +135,14 @@ class InputOutput: current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S") self.append_chat_history(f"\n# aider chat started at {current_time}\n\n") + def read_text(self, filename): + try: + with open(filename, "r", encoding=self.encoding) as f: + return f.read() + except (FileNotFoundError, UnicodeError) as e: + self.tool_error(str(e)) + return None + def get_input(self, root, rel_fnames, addable_rel_fnames, commands): if self.pretty: style = dict(style=self.user_input_color) if self.user_input_color else dict() diff --git a/aider/main.py b/aider/main.py index decf1514f..cb2039351 100644 --- a/aider/main.py +++ b/aider/main.py @@ -185,6 +185,11 @@ def main(args=None, input=None, output=None): dest="dirty_commits", help="Disable commits when repo is found dirty", ) + parser.add_argument( + "--encoding", + default="utf-8", + help="Specify the encoding to use when reading files (default: utf-8)", + ) parser.add_argument( "--openai-api-key", metavar="OPENAI_API_KEY",