<commit_before>import os
import shutil

import requests
from client_default import defaultpath, currentserver, port, DEBUG


def get_history():
    r = requests.get(currentserver + ":" + port + "/getHistory", data={'path': defaultpath})
    # print(r.json())
    op_history = r.json()['history']
    return op_history


def remove_file_or_folder(file):
    if os.path.isfile(file):
        os.remove(file)
    elif (os.path.exists(file)):
        shutil.rmtree(file)
    else:
        return -1


def execute_history(op_history):
    history_for_file = dict()
    for operation, filepath, others in op_history:
        if operation != 'mod':
            if filepath in history_for_file:
                history_for_file[filepath] = (operation, others)
            else:
                history_for_file.setdefault(filepath, (operation, others))
        elif operation != 'remarks':  # operation is mod rename
            tmp = history_for_file.get(filepath, None)
            if tmp is not None:  # there has been some changes to the original file
                history_for_file.pop(others, None)
                remove_file_or_folder(filepath)
                history_for_file.setdefault(others, tmp)
            else:  # there is no change in the file content, just move
                history_for_file.setdefault(others, ('mod', filepath))  # SPECIAL: filepath is the source file
    if DEBUG: print(history_for_file)
    for file in history_for_file:
        operation, others = history_for_file[file]
        if operation == 'del':
            remove_file_or_folder(file)
        elif operation == 'mod':
            shutil.move(others, file)
        elif operation == 'upd' or operation == 'new':
            ri = requests.get(currentserver + ":" + port + "/getfile", data={'filename': file})
            file = open(file, 'w')
            data = ri.json()['data']
            file.write(data)
            file.close()<commit_msg> change mod update new strategy<commit_after>import os
import shutil

import requests
from client_default import defaultpath, currentserver, port, DEBUG


def get_history():
    r = requests.get(currentserver + ":" + port + "/getHistory", data={'path': defaultpath})
    # print(r.json())
    op_history = r.json()['history']
    return op_history


def remove_file_or_folder(file):
    if os.path.isfile(file):
        os.remove(file)
    elif (os.path.exists(file)):
        shutil.rmtree(file)
    else:
        return -1


def execute_history(op_history):
    history_for_file = dict()
    for operation, filepath, others in op_history:
        if operation != 'mod':
            if filepath in history_for_file:
                history_for_file[filepath] = (operation, others)
            else:
                history_for_file.setdefault(filepath, (operation, others))
        elif operation != 'remarks':  # operation is mod rename
            tmp = history_for_file.get(filepath, None)
            if tmp is not None:  # there has been some changes to the original file
                history_for_file.pop(others, None)
                remove_file_or_folder(filepath)
                history_for_file.setdefault(others, tmp)
            else:  # there is no change in the file content, just move
                history_for_file.setdefault(others, ('mod', filepath))  # SPECIAL: filepath is the source file
    if DEBUG: print(history_for_file)
    for file in history_for_file:
        operation, others = history_for_file[file]
        if operation == 'del':
            remove_file_or_folder(file)
        # elif operation == 'mod':
        #     try:
        #         shutil.move(others, file)
        #     except:
        #         pass
        # elif operation == 'upd' or operation == 'new':
        elif 'upd' in operation or 'new' in operation or 'mod' in operation:
            ri = requests.get(currentserver + ":" + port + "/getfile", data={'filename': file})
            file = open(file, 'w')
            if ri is not None:
                data = ri.json()['data']
                file.write(data)
            file.close()
{'fraction_non_alphanumeric': 0.10425844346549193, 'fraction_numerical': 0.0004894762604013706, 'mean_word_length': 2.3173701298701297, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '440952', 'n_tokens_mistral': 1172, 'n_tokens_neox': 1153, 'n_words': 320}