def search_function_to_replace(line, dictionary):
    """
    For each line, it searchs for function name, creates new variables and saves them in a dictionary.

    :param line: A single line from tokenizer.tokenize_file(...).
    :param dictionary: Variable dictionary.
    """
    token_line = tokenizer.tokenize_line(line)  #spezzo la line
    for ind, tok in enumerate(token_line):
        old = ''

        if token_line[ind][1] == 'def' and token_line[ind + 1][
                0] == token.NAME:  #se la line è una dichiarazione di funzione
            #quindi ha una def
            old = token_line[ind + 1][1]  #salvo l'od line

        replace = generate(
        )  #genero un nuovo nuìome per la funzione (generate() in generate_replacement.py)
        if replace not in dictionary.values(
        ) and old not in replacement_dic.keys(
        ) and not old == '':  #se questo nome non è
            #già nel dizionario e non è quello che si vuole cambiare

            # Non seve il controllo se un nuovo nome di variabile esiste o meno, siamo sicuri che sia univoco
            # per il discorso di probabilità
            #while replace in replacement_dic.values(): #se il nuovo nome è già presente nel dizionario dei nomi delle funzioni
            #    replace = generate() #nel caso, genero un altro nome random

            replacement_dic[
                old] = replace  #sostituisco all'indice dell'old il nuovo nome della funzione
Пример #2
0
def search_function_to_replace(line,dictionary):
    """
    For each line, it searchs for function name, creates new variables and saves them in a dictionary.

    :param line: A single line from tokenizer.tokenize_file(...).
    :param dictionary: Variable dictionary.
    """
    token_line = tokenizer.tokenize_line(line)
    for ind, tok in enumerate(token_line):
        old = ''

        if token_line[ind][1] == 'def' and token_line[ind+1][0] == token.NAME:
            old = token_line[ind+1][1]

        replace = generate()
        if replace not in dictionary.values() and old not in replacement_dic.keys() and not old == '':
            while replace in replacement_dic.values():
                replace = generate()
            replacement_dic[old] = replace
Пример #3
0
def search_variable_to_replace(line):
    """
    For each line, it searchs for variables name, creates new variables and saves them in a dictionary.

    :param line: A single line from tokenizer.tokenize_file(...).
    """
    token_line = tokenizer.tokenize_line(line)
    for ind, tok in enumerate(token_line):
        old = ''
        # case 1: (var) or (var,
        if token_line[ind][1] == '(' and token_line[
                ind + 1][0] == token.NAME and (token_line[ind + 2][1] == ')' or
                                               token_line[ind + 2][1] == ','):
            old = token_line[ind + 1][1]

        # case 2: (var ) or (var ,
        elif token_line[ind][1] == '(' and token_line[
                ind +
                1][0] == token.NAME and token_line[ind + 2][1] == ' ' and (
                    token_line[ind + 3][1] == ')'
                    or token_line[ind + 3][1] == ','):
            old = token_line[ind + 1][1]

        # case 3: ( var) or ( var,
        elif token_line[ind][1] == '(' and token_line[
                ind +
                1][1] == ' ' and token_line[ind + 2][0] == token.NAME and (
                    token_line[ind + 3][1] == ')'
                    or token_line[ind + 3][1] == ','):
            old = token_line[ind + 2][1]

        # case 4: ( var ) or ( var ,
        elif token_line[ind][1] == '(' and token_line[
                ind + 1][1] == ' ' and token_line[
                    ind +
                    2][0] == token.NAME and token_line[ind + 3][1] == ' ' and (
                        token_line[ind + 4][1] == ')'
                        or token_line[ind + 4][1] == ','):
            old = token_line[ind + 2][1]

        # case 5 ,var) or ,var,
        elif token_line[ind][1] == ',' and token_line[
                ind + 1][0] == token.NAME and (token_line[ind + 2][1] == ')' or
                                               token_line[ind + 2][1] == ','):
            old = token_line[ind + 1][1]

        # case 6: , var) or , var,
        elif token_line[ind][1] == ',' and token_line[
                ind +
                1][1] == ' ' and token_line[ind + 2][0] == token.NAME and (
                    token_line[ind + 3][1] == ')'
                    or token_line[ind + 3][1] == ','):
            old = token_line[ind + 2][1]

        # case 7: ,var ) or ,var ,
        elif token_line[ind][1] == ',' and token_line[
                ind +
                1][0] == token.NAME and token_line[ind + 2][1] == ' ' and (
                    token_line[ind + 3][1] == ')'
                    or token_line[ind + 3][1] == ','):
            old = token_line[ind + 1][1]

        # case 8: , var ) or , var ,
        elif token_line[ind][1] == ',' and token_line[
                ind + 1][1] == ' ' and token_line[
                    ind +
                    2][0] == token.NAME and token_line[ind + 3][1] == ' ' and (
                        token_line[ind + 4][1] == ')'
                        or token_line[ind + 4][1] == ','):
            old = token_line[ind + 2][1]

        # case 9: assignment
        elif token_line[ind][0] == token.NAME and (
                token_line[ind + 1][1] == '='
                or token_line[ind + 2][1] == '='):
            old = token_line[ind][1]

        # case 10: as var :
        elif token_line[ind][1] == 'as' and (
            (token_line[ind + 1][0] == token.NAME and token_line[ind + 2][1]
             == ':') or token_line[ind + 1][0] == token.NAME):
            old = token_line[ind + 1][1]

        # case 11: for var
        elif token_line[ind][1] == 'for' and token_line[ind +
                                                        1][0] == token.NAME:
            old = token_line[ind + 1][1]

        # case 12: if var
        elif token_line[ind][1] == 'if' and token_line[
                ind +
                1][0] == token.NAME and not token_line[ind + 2][1] == '(':
            old = token_line[ind + 1][1]

        # case 13: save import module
        elif token_line[ind][1] == 'import' and token_line[ind +
                                                           1][0] == token.NAME:
            import_list.append(token_line[ind + 1][1])

        if old not in replacement_dic.keys() and not old == '':
            replace = generate()
            while replace in replacement_dic.values():
                replace = generate()
            replacement_dic[old] = replace
def search_variable_to_replace(line):
    """
    For each line, it searchs for variables name, creates new variables and saves them in a dictionary.

    :param line: A single line from tokenizer.tokenize_file(...).
    """
    token_line = tokenizer.tokenize_line(line) #spezzo la line
    #prendo tutti i nomi delle variabili per sostituirli con variabili a caso
    for ind, tok in enumerate(token_line):
        old = ''
        # case 1: (var) or (var,
        if token_line[ind][1] == '(' and token_line[ind+1][0] == token.NAME and (token_line[ind+2][1] == ')' or token_line[ind+2][1] == ','):
            old = token_line[ind+1][1]

        # case 2: (var ) or (var ,
        elif token_line[ind][1] == '(' and token_line[ind+1][0] == token.NAME and token_line[ind+2][1] == ' ' and (token_line[ind+3][1] == ')' or token_line[ind+3][1] == ','):
            old = token_line[ind+1][1]

        # case 3: ( var) or ( var,
        elif token_line[ind][1] == '(' and token_line[ind+1][1] == ' ' and token_line[ind+2][0] == token.NAME and (token_line[ind+3][1] == ')' or token_line[ind+3][1] == ','):
            old = token_line[ind+2][1]

        # case 4: ( var ) or ( var ,
        elif token_line[ind][1] == '(' and token_line[ind+1][1] == ' ' and token_line[ind+2][0] == token.NAME and token_line[ind+3][1] == ' ' and (token_line[ind+4][1] == ')' or token_line[ind+4][1] == ','):
            old = token_line[ind+2][1]

        # case 5 ,var) or ,var,
        elif token_line[ind][1] == ',' and token_line[ind+1][0] == token.NAME and (token_line[ind+2][1] == ')' or token_line[ind+2][1] == ','):
            old = token_line[ind+1][1]

        # case 6: , var) or , var,
        elif token_line[ind][1] == ',' and token_line[ind+1][1] == ' ' and token_line[ind+2][0] == token.NAME and (token_line[ind+3][1] == ')' or token_line[ind+3][1] == ','):
            old = token_line[ind+2][1]

        # case 7: ,var ) or ,var ,
        elif token_line[ind][1] == ',' and token_line[ind+1][0] == token.NAME and token_line[ind+2][1] == ' ' and (token_line[ind+3][1] == ')' or token_line[ind+3][1] == ','):
            old = token_line[ind+1][1]

        # case 8: , var ) or , var ,
        elif token_line[ind][1] == ',' and token_line[ind+1][1] == ' ' and token_line[ind+2][0] == token.NAME and token_line[ind+3][1] == ' ' and (token_line[ind+4][1] == ')' or token_line[ind+4][1] == ','):
            old = token_line[ind+2][1]

        # case 9: assignment
        elif token_line[ind][0] == token.NAME and (token_line[ind+1][1] == '=' or token_line[ind+2][1] == '='):
            old = token_line[ind][1]

        # case 10: as var :
        elif token_line[ind][1] == 'as' and ((token_line[ind+1][0] == token.NAME and token_line[ind+2][1] == ':') or token_line[ind+1][0] == token.NAME):
            old = token_line[ind+1][1]

        # case 11: for var
        elif token_line[ind][1] == 'for' and token_line[ind+1][0] == token.NAME:
            old = token_line[ind+1][1]

        # case 12: if var
        elif token_line[ind][1] == 'if' and token_line[ind+1][0] == token.NAME and not token_line[ind+2][1] == '(':
            old = token_line[ind+1][1]

        # case 13: save import module
        elif token_line[ind][1] == 'import' and token_line[ind+1][0] == token.NAME:
            import_list.append(token_line[ind+1][1])

        if old not in replacement_dic.keys() and not old == '': #se old non è già presente nel dizionario di quello che si deve sostituire
            replace = generate() #genero un nome di una funziomne a caso 
            
            # Non seve il controllo se un nuovo nome di variabile esiste o meno, siamo sicuri che sia univoco
            # per il discorso di probabilità
            #while replace in replacement_dic.values(): #nel caso in cui creo un nome di una variabile già presente
            #    replace = generate() #genero un nome di una funziomne a caso
            
            replacement_dic[old] = replace #cambio il vecchio nome della funzione con quello nuovo!