Merge pull request #16 from prayagnshah/main

adding flag for user confirmation
pull/21/head^2
biobootloader 2023-04-15 11:20:20 -07:00 zatwierdzone przez GitHub
commit 2f5a026ff9
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
2 zmienionych plików z 30 dodań i 4 usunięć

Wyświetl plik

@ -33,6 +33,10 @@ If you want to use GPT-3.5 by default instead of GPT-4 uncomment the default mod
DEFAULT_MODEL=gpt-3.5-turbo
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:

Wyświetl plik

@ -117,7 +117,7 @@ def send_error_to_gpt(file_path, args, error_message, model=DEFAULT_MODEL):
return json_validated_response(model, messages)
def apply_changes(file_path, changes: list):
def apply_changes(file_path, changes: list, confirm=False):
"""
Pass changes as loaded json (list of dicts)
"""
@ -146,6 +146,25 @@ def apply_changes(file_path, changes: list):
elif operation == "InsertAfter":
file_lines.insert(line, content + "\n")
# Ask for user confirmation before writing changes
print("\nChanges to be made:")
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="")
# 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)
with open(file_path, "w") as f:
f.writelines(file_lines)
@ -156,7 +175,8 @@ def apply_changes(file_path, changes: list):
# Show the diff
print("\nChanges:")
diff = difflib.unified_diff(original_file_lines, file_lines, lineterm="")
diff = difflib.unified_diff(
original_file_lines, file_lines, lineterm="")
for line in diff:
if line.startswith("+"):
cprint(line, "green", end="")
@ -165,8 +185,10 @@ def apply_changes(file_path, changes: list):
else:
print(line, end="")
print("Changes applied.")
def main(script_name, *script_args, revert=False, model=DEFAULT_MODEL):
def main(script_name, *script_args, revert=False, model=DEFAULT_MODEL, confirm=False):
if revert:
backup_file = script_name + ".bak"
if os.path.exists(backup_file):
@ -198,7 +220,7 @@ def main(script_name, *script_args, revert=False, model=DEFAULT_MODEL):
model=model,
)
apply_changes(script_name, json_response)
apply_changes(script_name, json_response, confirm=confirm)
cprint("Changes applied. Rerunning...", "blue")