# Path: sourceutils/csvASTs/CSVProcessor.py
# class CSVProcessor:
#     def __init__(self):
#         self.handlers = dict()
#         self.defaultHandler = None
#         
#         self.reader = None
#         self.currentFile = None
#     
#     def processCSVRows(self, metaDataFile):
#         self.currentFile = metaDataFile
#         self.reader = csv.reader(open(metaDataFile), delimiter="\t",
#                             quotechar=None, escapechar=None)
#         
#         for row in self.reader:
#             self._processCSVRow(row)
#         
#     def _processCSVRow(self, row):
#         
#         if self.defaultHandler:
#             executeOtherHandlers = self.defaultHandler(row)
#             if not executeOtherHandlers: return
#         
#         lineType = getCSVRowType(row)
#         handler = self._getHandlerForType(lineType)
#         if not handler: return
#         handler(row)
# 
#     def registerChildHandler(self, level, childHandler):
#         self.parentLevel = level
#         self.defaultHandler = childHandler
#     
#     def unregisterChildHandler(self):
#         self.parentLevel = None
#         self.defaultHandler = None
# 
#     def _getHandlerForType(self, lineType):
#         try:
#             return self.handlers[lineType]
#         except:
#             return None
# 
# Path: sourceutils/csvASTs/CSVRowAccessors.py
# def getCSVRowLevel(row):
#     return row[LEVEL]
# 
# 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


import pickle
from sourceutils.csvASTs.CSVProcessor import CSVProcessor
from sourceutils.csvASTs.CSVRowAccessors import getCSVRowLevel
from sourceutils.pythonASTs.PythonASTTreeNode import PythonASTTreeNode
        
        # code below fails if level ever
        # increases by more than one at once
        level = int(getCSVRowLevel(row))
        if level > len(self.parentStack) - 1:
            # moved down one level, push previous node
            self.parentStack.append(self.previousNode)
        elif level < len(self.parentStack) -1:
            while(level < len(self.parentStack) - 1):
                self.parentStack.pop()
        else:
            # stayed on a level, no need to adjust parentStack
            pass
                
        parentNode = self.parentStack[-1]
        parentNode.appendChild(newNode)
        
        self.previousNode = newNode
    
    def prettyPrintTree(self):
        numberOfTabs = 0
        self._prettyPrintTree(self.rootNode, numberOfTabs)
    
    def _prettyPrintTree(self, node, numberOfTabs):
        outString = '\t'*numberOfTabs
        outString += str(node.row)
        print(outString)
        for child in node.children:
            self._prettyPrintTree(child, numberOfTabs + 1)
    

# Path: sourceutils/csvASTs/CSVProcessor.py
# class CSVProcessor:
#     def __init__(self):
#         self.handlers = dict()
#         self.defaultHandler = None
#         
#         self.reader = None
#         self.currentFile = None
#     
#     def processCSVRows(self, metaDataFile):
#         self.currentFile = metaDataFile
#         self.reader = csv.reader(open(metaDataFile), delimiter="\t",
#                             quotechar=None, escapechar=None)
#         
#         for row in self.reader:
#             self._processCSVRow(row)
#         
#     def _processCSVRow(self, row):
#         
#         if self.defaultHandler:
#             executeOtherHandlers = self.defaultHandler(row)
#             if not executeOtherHandlers: return
#         
#         lineType = getCSVRowType(row)
#         handler = self._getHandlerForType(lineType)
#         if not handler: return
#         handler(row)
# 
#     def registerChildHandler(self, level, childHandler):
#         self.parentLevel = level
#         self.defaultHandler = childHandler
#     
#     def unregisterChildHandler(self):
#         self.parentLevel = None
#         self.defaultHandler = None
# 
#     def _getHandlerForType(self, lineType):
#         try:
#             return self.handlers[lineType]
#         except:
#             return None
# 
# Path: sourceutils/csvASTs/CSVRowAccessors.py
# def getCSVRowLevel(row):
#     return row[LEVEL]
# 
# 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/pythonASTs/CSVToPythonAST.py
import pickle
from sourceutils.csvASTs.CSVProcessor import CSVProcessor
from sourceutils.csvASTs.CSVRowAccessors import getCSVRowLevel
from sourceutils.pythonASTs.PythonASTTreeNode import PythonASTTreeNode

        
        # code below fails if level ever
        # increases by more than one at once
        level = int(getCSVRowLevel(row))
        if level > len(self.parentStack) - 1:
            # moved down one level, push previous node
            self.parentStack.append(self.previousNode)
        elif level < len(self.parentStack) -1:
            while(level < len(self.parentStack) - 1):
                self.parentStack.pop()
        else:
            # stayed on a level, no need to adjust parentStack
            pass
                
        parentNode = self.parentStack[-1]
        parentNode.appendChild(newNode)
        
        self.previousNode = newNode
    
    def prettyPrintTree(self):
        numberOfTabs = 0
        self._prettyPrintTree(self.rootNode, numberOfTabs)
    
    def _prettyPrintTree(self, node, numberOfTabs):
        outString = '\t'*numberOfTabs
        outString += str(node.row)
        print(outString)
        for child in node.children:
            self._prettyPrintTree(child, numberOfTabs + 1)
    

    def saveResults(self, filename):