# Path: sourceutils/pythonASTs/PythonASTTreeNode.py
# class PythonASTTreeNode():
#     def __init__(self, row):
#         self.row = row
#         self.children = []
# 
#     def appendChild(self, child):
#         self.children.append(child)
# 
#     def applyFunc(self, f):
#         self.row = f(self.row)
#         for child in self.children:
#             child.applyFunc(f)
#     
#     def __str__(self):
#         retStr = '@+'
#         retStr += str(self.row)
#         for child in self.children:
#             retStr += str(child)
#         retStr += '+@'
#         return retStr


from sourceutils.pythonASTs.PythonASTTreeNode import PythonASTTreeNode
class ParseTreeFilter():
    def __init__(self):       
        self.nodeTypesOfInterest = list()
        self.keepNodesOfInterest = True
    
    def setNodeTypesOfInterest(self, l):
        self.nodeTypesOfInterest = l
    
    def setRow2StringConverter(self, r):
        self.row2StringConverter = r
    
    def prune(self, node):    
        if self._mustPruneNode(node):
            r = prunedRow
        else:
            r = node.row
        
        rf = self.row2StringConverter.convert(r)
        
        newRootNode = PythonASTTreeNode(rf)
        self.addPrunedChildren(node, newRootNode)
        return newRootNode
    
    def addPrunedChildren(self, node, root):
        for child in node.children:
            self._attachPruned(child, root)
    
    def _pruneNode(self, node, root):
        # If a leaf is to be pruned, discard it altogether
        if len(node.children) == 0: return

# Path: sourceutils/pythonASTs/PythonASTTreeNode.py
# class PythonASTTreeNode():
#     def __init__(self, row):
#         self.row = row
#         self.children = []
# 
#     def appendChild(self, child):
#         self.children.append(child)
# 
#     def applyFunc(self, f):
#         self.row = f(self.row)
#         for child in self.children:
#             child.applyFunc(f)
#     
#     def __str__(self):
#         retStr = '@+'
#         retStr += str(self.row)
#         for child in self.children:
#             retStr += str(child)
#         retStr += '+@'
#         return retStr


# Path: sourceutils/pythonASTFilter/pruning/ParseTreeFilter.py
from sourceutils.pythonASTs.PythonASTTreeNode import PythonASTTreeNode

class ParseTreeFilter():
    def __init__(self):       
        self.nodeTypesOfInterest = list()
        self.keepNodesOfInterest = True
    
    def setNodeTypesOfInterest(self, l):
        self.nodeTypesOfInterest = l
    
    def setRow2StringConverter(self, r):
        self.row2StringConverter = r
    
    def prune(self, node):    
        if self._mustPruneNode(node):
            r = prunedRow
        else:
            r = node.row
        
        rf = self.row2StringConverter.convert(r)
        
        newRootNode = PythonASTTreeNode(rf)
        self.addPrunedChildren(node, newRootNode)
        return newRootNode
    
    def addPrunedChildren(self, node, root):
        for child in node.children:
            self._attachPruned(child, root)
    
    def _pruneNode(self, node, root):
        # If a leaf is to be pruned, discard it altogether
        if len(node.children) == 0: return

        newNode = PythonASTTreeNode(prunedRow)