Project Name: biobootloader+wolverine
Language: Python
Statement Type: API

Source Code:
def apply_changes(file_path: str, changes: List, confirm: bool = False):
    """
    Pass changes as loaded json (list of dicts)
    """
    with open(file_path) as f:
        original_file_lines = f.readlines()

    # Filter out explanation elements
    operation_changes = [change for change in changes if "operation" in change]
    explanations = [
        change["explanation"] for change in changes if "explanation" in change
    ]

    # Sort the changes in reverse line order
    operation_changes.sort(key=lambda x: x["line"], reverse=True)

    file_lines = original_file_lines.copy()
    for change in operation_changes:
        operation = change["operation"]
        line = change["line"]
        content = change["content"]

        if operation == "Replace":
            file_lines[line - 1] = content + "\n"
        elif operation == "Delete":
            del file_lines[line - 1]
        elif operation == "InsertAfter":
            file_lines.insert(line, content + "\n")

    # Print explanations
    cprint("Explanations:", "blue")
    for explanation in explanations:
        cprint(f"- {explanation}", "blue")

    # Display changes diff
    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="")

    if confirm:
        # check if user wants to apply changes or exit
        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)
    print("Changes applied.")

apply_changes(file_path='/tmp/tmp6qrrn_j9', changes=[{'operation': 'Replace', 'line': 2, 'content': 'new second line'}], confirm=False)

Selected Statement: with open(file_path, "w") as f:

Function Input:
file_path: '/tmp/tmp6qrrn_j9'
changes: operation: Replace
line: 2
content: new second line
confirm: False

Variable Values Before Statement:
file_path: '/tmp/tmp6qrrn_j9'

Value After Statement Execution: <_io.TextIOWrapper name

Variable States During Runtime (line, value):
file_path: 1
'/tmp/tmp6qrrn_j9'
changes: 1
operation: Replace
line: 2
content: new second line
confirm: 1
False
f: 5
<_io.TextIOWrapper name='/tmp/tmp6qrrn_j9' mode='r' encoding='UTF-8'>
53
<_io.TextIOWrapper name='/tmp/tmp6qrrn_j9' mode='w' encoding='UTF-8'>
original_file_lines: 6
first line

second line

third line
operation_changes: 9
operation: Replace
line: 2
content: new second line
explanations: 10
file_lines: 17
first line

second line

third line
24
first line

new second line

third line
change: 18
operation: Replace
line: 2
content: new second line
operation: 19
'Replace'
line: 20
2
38
'--- '
38
'+++ '
38
'@@ -1,3 +1,3 @@'
38
' first line\n'
38
'-second line\n'
38
'+new second line\n'
38
' third line'
content: 21
'new second line'
diff: 37
<generator object unified_diff at 0x7f5b71b2cba0>