# 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 getCSVRowType(row):
#     return row[ROW_TYPE]
# 
# def getCSVRowLevel(row):
#     return row[LEVEL]
# 
# Path: sourceutils/pythonCFGs/CFG.py
# class CFG:
#     def __init__(self):
#         self.nodes = list() # list of basic blocks
#         self.edges = list() # (src,dst)-pairs
#         self.labeledNodes = list() # list of (id, row) pairs
#     
#     # returns the ID
#     def addNode(self, s):
#         self.nodes.append(s)
#         return len(self.nodes) - 1
# 
#     def addEdge(self, src, dst, predicate = None):
#         self.edges.append((src, dst, predicate))
# 
#     def removeEdgesFrom(self, src):
#         for i in xrange(len(self.edges)):
#             if self.edges[i][0] == src:
#                 del self.edges[i]
#                 return
#     
#     def removeEdge(self, src, dst):
#         
#         for i in xrange(len(self.edges)):
#             if self.edges[i][0] == src and self.edges[i][1] == dst:
#                 del self.edges[i]
#                 return
#         
#     def getNodeById(self, i):
#         return self.nodes[i]
#     
#     def getCurrentNodeId(self):
#         return len(self.nodes) - 1
# 
#     def appendToLatestNode(self, s):
#         lastBasicBlock = self.nodes[-1]
#         lastBasicBlock.rows.append(s)
#     
#     def registerSuccessors(self):
#         for (src, dst, pred) in self.edges:
#             self.nodes[src].successors.append((dst,pred))
# 
# class BasicBlock:
#     def __init__(self, firstRow = None):
#         self.rows =list()
#         if firstRow:
#             self.rows.append(firstRow)
#         
#         self.successors = []
#       
#         self.blockType = ''
#       
#       
#     def getLastInstrType(self):
#         return self.rows[-1][0]
#     
#     def getCondition(self):
#         return getCSVRowCondition(self.rows[0])


import pickle, os
from sourceutils.csvASTs.CSVProcessor import CSVProcessor
from sourceutils.csvASTs.CSVRowAccessors import getCSVRowType, getCSVRowLevel
from sourceutils.pythonCFGs.CFG import CFG, BasicBlock
    
    def handleNodeInFunction(self, row):
        level = int(getCSVRowLevel(row))
        
        if self.isInScope(level):
            self.handleNodeInScope(row, level)
        else:
            self.leaveScope(row, level)
    
    def handleNodeInScope(self, row, level):
        
        if self.isScopeIncreaseNode(row):
            self.handleScopeIncreaseNode(row, level)
        elif self.isControlStatementNode(row):
            self.handleControlStatement(row, level)
        elif self.isLabelNode(row):
            self.handleLabelNode(row, level)
        else:
            self.defaultNodeHandler(row, level)
    
    def isInScope(self, level):
        return level >= self.getCurrentLevel()
    
    def isScopeIncreaseNode(self, row):
        return getCSVRowType(row) in scopeIncreaseNodes

    def isControlStatementNode(self, row):
        return getCSVRowType(row) in controlStatementNodes

    def isLabelNode(self, row):

# 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 getCSVRowType(row):
#     return row[ROW_TYPE]
# 
# def getCSVRowLevel(row):
#     return row[LEVEL]
# 
# Path: sourceutils/pythonCFGs/CFG.py
# class CFG:
#     def __init__(self):
#         self.nodes = list() # list of basic blocks
#         self.edges = list() # (src,dst)-pairs
#         self.labeledNodes = list() # list of (id, row) pairs
#     
#     # returns the ID
#     def addNode(self, s):
#         self.nodes.append(s)
#         return len(self.nodes) - 1
# 
#     def addEdge(self, src, dst, predicate = None):
#         self.edges.append((src, dst, predicate))
# 
#     def removeEdgesFrom(self, src):
#         for i in xrange(len(self.edges)):
#             if self.edges[i][0] == src:
#                 del self.edges[i]
#                 return
#     
#     def removeEdge(self, src, dst):
#         
#         for i in xrange(len(self.edges)):
#             if self.edges[i][0] == src and self.edges[i][1] == dst:
#                 del self.edges[i]
#                 return
#         
#     def getNodeById(self, i):
#         return self.nodes[i]
#     
#     def getCurrentNodeId(self):
#         return len(self.nodes) - 1
# 
#     def appendToLatestNode(self, s):
#         lastBasicBlock = self.nodes[-1]
#         lastBasicBlock.rows.append(s)
#     
#     def registerSuccessors(self):
#         for (src, dst, pred) in self.edges:
#             self.nodes[src].successors.append((dst,pred))
# 
# class BasicBlock:
#     def __init__(self, firstRow = None):
#         self.rows =list()
#         if firstRow:
#             self.rows.append(firstRow)
#         
#         self.successors = []
#       
#         self.blockType = ''
#       
#       
#     def getLastInstrType(self):
#         return self.rows[-1][0]
#     
#     def getCondition(self):
#         return getCSVRowCondition(self.rows[0])


# Path: sourceutils/pythonCFGs/CSVToCFG.py
import pickle, os
from sourceutils.csvASTs.CSVProcessor import CSVProcessor
from sourceutils.csvASTs.CSVRowAccessors import getCSVRowType, getCSVRowLevel
from sourceutils.pythonCFGs.CFG import CFG, BasicBlock

    
    def handleNodeInFunction(self, row):
        level = int(getCSVRowLevel(row))
        
        if self.isInScope(level):
            self.handleNodeInScope(row, level)
        else:
            self.leaveScope(row, level)
    
    def handleNodeInScope(self, row, level):
        
        if self.isScopeIncreaseNode(row):
            self.handleScopeIncreaseNode(row, level)
        elif self.isControlStatementNode(row):
            self.handleControlStatement(row, level)
        elif self.isLabelNode(row):
            self.handleLabelNode(row, level)
        else:
            self.defaultNodeHandler(row, level)
    
    def isInScope(self, level):
        return level >= self.getCurrentLevel()
    
    def isScopeIncreaseNode(self, row):
        return getCSVRowType(row) in scopeIncreaseNodes

    def isControlStatementNode(self, row):
        return getCSVRowType(row) in controlStatementNodes

    def isLabelNode(self, row):

        return getCSVRowType(row) == labelNode