# Path: sourceutils/pythonASTs/PythonASTProcessor.py
# class PythonASTProcessor():
#     def __init__(self):
#         
#         self.handlers = dict()
#         self.currentFile = None
#         
#     def loadTreeFromFile(self, treefilename):
# 
#         self.currentFile = self._treefileToSourceFile(treefilename)
#         self.tree = pickle.load(open(treefilename, "rb"))
#     
#     def _treefileToSourceFile(self, treefile):
#         return ('/'.join(treefile.split('/')[:-1]))
#     
#     def processTree(self, node):
#         
#         traverseChildren = self._callHandlers(node)
#         if traverseChildren:
#             self.processChildren(node)
# 
#     def processChildren(self, node = None):
#         if node == None:
#             node = self.tree
#         
#         for child in node.children:
#             self.processTree(child)
# 
#     def _callHandlers(self, node):
#         nodeType = node.row[0]
#         if nodeType in self.handlers:
#             handler = self.handlers[nodeType]
#             return handler(node)


import os, pickle
from sourceutils.pythonASTs.PythonASTProcessor import PythonASTProcessor

class PythonASTToPrunedAST(PythonASTProcessor):
    def __init__(self):
        PythonASTProcessor.__init__(self)
        
        self.handlers['func'] = self.handleFunction
        self.filter = None
    
    def setFilter(self, f):
        self.filter = f

    def setRow2StringConverter(self, row2StringConverter):
        self.filter.setRow2StringConverter(row2StringConverter)
        
    def handleFunction(self, node):
        self._setFunctionRow(node)
        prunedTree = self._pruneTree(node)
        self.handlePrunedTree(prunedTree)
        return False
    
    def _setFunctionRow(self, node):
        self.functionRow = ','.join(node.row)
        self.functionRow += ',' + self.currentFile
    
    def _pruneTree(self, node):
        if not self.filter: return node

# Path: sourceutils/pythonASTs/PythonASTProcessor.py
# class PythonASTProcessor():
#     def __init__(self):
#         
#         self.handlers = dict()
#         self.currentFile = None
#         
#     def loadTreeFromFile(self, treefilename):
# 
#         self.currentFile = self._treefileToSourceFile(treefilename)
#         self.tree = pickle.load(open(treefilename, "rb"))
#     
#     def _treefileToSourceFile(self, treefile):
#         return ('/'.join(treefile.split('/')[:-1]))
#     
#     def processTree(self, node):
#         
#         traverseChildren = self._callHandlers(node)
#         if traverseChildren:
#             self.processChildren(node)
# 
#     def processChildren(self, node = None):
#         if node == None:
#             node = self.tree
#         
#         for child in node.children:
#             self.processTree(child)
# 
#     def _callHandlers(self, node):
#         nodeType = node.row[0]
#         if nodeType in self.handlers:
#             handler = self.handlers[nodeType]
#             return handler(node)


# Path: sourceutils/pythonASTFilter/PythonASTToPrunedAST.py
import os, pickle
from sourceutils.pythonASTs.PythonASTProcessor import PythonASTProcessor


class PythonASTToPrunedAST(PythonASTProcessor):
    def __init__(self):
        PythonASTProcessor.__init__(self)
        
        self.handlers['func'] = self.handleFunction
        self.filter = None
    
    def setFilter(self, f):
        self.filter = f

    def setRow2StringConverter(self, row2StringConverter):
        self.filter.setRow2StringConverter(row2StringConverter)
        
    def handleFunction(self, node):
        self._setFunctionRow(node)
        prunedTree = self._pruneTree(node)
        self.handlePrunedTree(prunedTree)
        return False
    
    def _setFunctionRow(self, node):
        self.functionRow = ','.join(node.row)
        self.functionRow += ',' + self.currentFile
    
    def _pruneTree(self, node):
        if not self.filter: return node

        return self.filter.prune(node)