Skip to content

Commit

Permalink
Merge pull request #16 from prayagnshah/main
Browse files Browse the repository at this point in the history
adding flag for user confirmation
  • Loading branch information
biobootloader authored Apr 15, 2023
2 parents 10fd162 + c20b79e commit 2f5a026
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
30 changes: 26 additions & 4 deletions wolverine.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
"""
Expand Down Expand Up @@ -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)

Expand All @@ -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="")
Expand All @@ -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):
Expand Down Expand Up @@ -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")


Expand Down

0 comments on commit 2f5a026

Please sign in to comment.