def traverseTree(self, t, path=None): if path == None: path = self.path treeSpot = t for i in range(len(path) - 1, 0, -1): move = path[i] if type(move) == tuple: if hasattr(treeSpot, move[0]): treeSpot = getattr(treeSpot, move[0]) else: log.error("Change Vector\ttraverseTree\t\tMissing attr: " + str(move[0]) + "\n" + printFunction(t)) return -99 elif type(move) == int: if type(treeSpot) == list: if move >= 0 and move < len(treeSpot): treeSpot = treeSpot[move] else: log.error( "Change Vector\ttraverseTree\t\tMissing position: " + str(move) + "," + str(treeSpot) + "\n" + printFunction(t)) return -99 else: log.error("Change Vector\ttraverseTree\t\tNot a list: " + str(treeSpot) + "\n" + printFunction(t)) return -99 else: # wat? log.error("Change Vector\ttraverseTree\t\tBad Path: " + str(move) + "\n" + printFunction(t)) return -99 return treeSpot
def __repr__(self): oldStr = printFunction(self.oldSubtree, 0) if isinstance( self.oldSubtree, ast.AST) else repr(self.oldSubtree) newStr = printFunction(self.newSubtree, 0) if isinstance( self.newSubtree, ast.AST) else repr(self.newSubtree) if self.oldPath != None: return "Swap: " + oldStr + " : " + str(self.oldPath) + "\n" + \ newStr + " : " + str(self.newPath) else: return "Swap: " + oldStr + " - " + newStr + " : " + str(self.path)
def applyChange(self, caller=None): tree = deepcopy(self.start) treeSpot = self.traverseTree(tree) if treeSpot == -99: return None # Make the change in the last position location = self.path[0] if type(location) == tuple and hasattr(treeSpot, location[0]): oldSpot = getattr(treeSpot, location[0]) # Make life easier for ourselves when applying multiple changes at once if self.newSubtree != None and oldSpot != None: if hasattr(oldSpot, "lineno"): self.newSubtree.lineno = oldSpot.lineno if hasattr(oldSpot, "col_offset"): self.newSubtree.col_offset = oldSpot.col_offset if compareASTs(oldSpot, self.oldSubtree, checkEquality=True) != 0: log.error("ChangeVector\tapplyChange\t" + str(caller) + "\t" + "Change old values don't match: " + str(self) + "\n" + str(printFunction(self.start))) setattr(treeSpot, location[0], self.newSubtree) # SPECIAL CASE. If we're changing the variable name, get rid of metadata if type(treeSpot) == ast.Name and location[0] == "id": if hasattr(treeSpot, "originalId"): del treeSpot.originalId if hasattr(treeSpot, "dontChangeName"): del treeSpot.dontChangeName if hasattr(treeSpot, "randomVar"): del treeSpot.randomVar elif type(treeSpot) == ast.arg and location[0] == "arg": if hasattr(treeSpot, "originalId"): del treeSpot.originalId if hasattr(treeSpot, "dontChangeName"): del treeSpot.dontChangeName if hasattr(treeSpot, "randomVar"): del treeSpot.randomVar elif type(location) == int and type(treeSpot) == list: # Need to swap out whatever is in this location if location >= 0 and location < len(treeSpot): if hasattr(treeSpot[location], "lineno"): self.newSubtree.lineno = treeSpot[location].lineno if hasattr(treeSpot[location], "col_offset"): self.newSubtree.col_offset = treeSpot[location].col_offset treeSpot[location] = self.newSubtree else: log.error("ChangeVector\tapplyChange\tDoesn't fit in list: " + str(location) + "\n" + printFunction(self.start)) else: log.error("ChangeVector\tapplyChange\t\tBroken at: " + str(location)) return tree
def applyChange(self, caller=None): tree = deepcopy(self.start) treeSpot = self.traverseTree(tree) if treeSpot == -99: return None # Make the change in the last position location = self.path[0] if type(treeSpot) == list: # Remove the old line if location < len(treeSpot): if compareASTs(treeSpot[location], self.oldSubtree, checkEquality=True) != 0: log.error("DeleteVector\tapplyChange\t" + str(caller) + "\t" + "Delete old values don't match: " + str(self) + "\n" + str(printFunction(self.start))) del treeSpot[location] else: log.error("DeleteVector\tapplyChange\t\tBad location: " + str(location) + "\t" + str(self.oldSubtree)) return None else: log.error("DeleteVector\tapplyChange\t\tBroken at: " + str(location)) return None return tree
def getSwapees(self): if self.oldPath == None: treeSpot = self.traverseTree(self.start) if type(treeSpot) == list and self.oldSubtree < len(treeSpot) and \ self.newSubtree < len(treeSpot): return (treeSpot[self.oldSubtree], treeSpot[self.newSubtree]) else: log.error("SwapVector\tgetSwapees\tBroken: \n" + printFunction(treeSpot, 0) + "," + printFunction(self.oldSubtree, 0) + "," + printFunction(self.newSubtree, 0) + "\n" + printFunction(self.start, 0)) else: oldTreeSpot = self.traverseTree(self.start, path=self.oldPath) newTreeSpot = self.traverseTree(self.start, path=self.newPath) if type(self.oldPath[0]) == int and type( oldTreeSpot ) == list and self.oldPath[0] < len(oldTreeSpot): oldValue = oldTreeSpot[self.oldPath[0]] elif type(self.oldPath[0]) == tuple and hasattr( oldTreeSpot, self.oldPath[0][0]): oldValue = getattr(oldTreeSpot, self.oldPath[0][0]) else: log.info("SwapVector\tgetSwapees\tBroken oldValue") oldValue = None if type(self.newPath[0]) == int and type( newTreeSpot ) == list and self.newPath[0] < len(newTreeSpot): newValue = newTreeSpot[self.newPath[0]] elif type(self.newPath[0]) == tuple and hasattr( newTreeSpot, self.newPath[0][0]): newValue = getattr(newTreeSpot, self.newPath[0][0]) else: log.info("SwapVector\tgetSwapees\tBroken newValue") newValue = None return (oldValue, newValue) return (None, None)
def __repr__(self): oldStr = printFunction(self.oldSubtree, 0) if isinstance( self.oldSubtree, ast.AST) else repr(self.oldSubtree) newStr = printFunction(self.newSubtree, 0) if isinstance( self.newSubtree, ast.AST) else repr(self.newSubtree) return "Move: " + oldStr + " - " + newStr + " : " + str(self.path)
def get_code_from_tree(tree: Optional[ast.AST]) -> str: return printFunction(tree)
def get_cleaned_code(source: str) -> str: return printFunction(get_ast(source))