diff --git a/README.md b/README.md index 55813f7..c988276 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,10 @@ You can also run with other models, but be warned they may not adhere to the edi python wolverine.py --model=gpt-3.5-turbo buggy_script.py "subtract" 20 3 +You can also use flag `--confirm=True` which will ask you `yes or no` before making changes to the file. If flag is not used then it will apply the changes to the file + + python wolverine.py buggy_script.py "subtract" 20 3 --confirm=True + ## Future Plans This is just a quick prototype I threw together in a few hours. There are many possible extensions and contributions are welcome: diff --git a/wolverine.py b/wolverine.py index 19fc95b..42001b6 100644 --- a/wolverine.py +++ b/wolverine.py @@ -66,7 +66,8 @@ def send_error_to_gpt(file_path, args, error_message, model): return response.choices[0].message.content.strip() -def apply_changes(file_path, changes_json): +# Added the flag confirm. Once user use flag confirm then it will ask for confirmation before applying the changes. +def apply_changes(file_path, changes_json, confirm=False): with open(file_path, "r") as f: original_file_lines = f.readlines() @@ -106,38 +107,38 @@ def apply_changes(file_path, changes_json): else: print(line, end="") - confirmation = input("Do you want to apply these changes? (y/n): ") - if confirmation.lower() == "y": - with open(file_path, "w") as f: - f.writelines(file_lines) + # Checking if user used confirm flag + if confirm: + confirmation = input("Do you want to apply these changes? (y/n): ") + if confirmation.lower() != "y": + print("Changes not applied") + sys.exit(0) - # Print explanations - cprint("Explanations:", "blue") - for explanation in explanations: - cprint(f"- {explanation}", "blue") + with open(file_path, "w") as f: + f.writelines(file_lines) - # Show the diff - print("\nChanges:") - diff = difflib.unified_diff( - original_file_lines, file_lines, lineterm="") - for line in diff: - if line.startswith("+"): - cprint(line, "green", end="") - elif line.startswith("-"): - cprint(line, "red", end="") - else: - print(line, end="") + # Print explanations + cprint("Explanations:", "blue") + for explanation in explanations: + cprint(f"- {explanation}", "blue") - print("Changes applied.") - # sys.exit(0) + # Show the diff + print("\nChanges:") + diff = difflib.unified_diff( + original_file_lines, file_lines, lineterm="") + for line in diff: + if line.startswith("+"): + cprint(line, "green", end="") + elif line.startswith("-"): + cprint(line, "red", end="") + else: + print(line, end="") - # Ending the code once they hit n or N - else: - print("Changes not applied") - sys.exit(0) + print("Changes applied.") -def main(script_name, *script_args, revert=False, model="gpt-4"): +# Added the flag confirm. Once user use flag confirm then it will ask for confirmation before applying the changes. +def main(script_name, *script_args, revert=False, model="gpt-4", confirm=False): if revert: backup_file = script_name + ".bak" if os.path.exists(backup_file): @@ -168,7 +169,7 @@ def main(script_name, *script_args, revert=False, model="gpt-4"): error_message=output, model=model, ) - apply_changes(script_name, json_response) + apply_changes(script_name, json_response, confirm=confirm) cprint("Changes applied. Rerunning...", "blue")