def getEdgeList(fileName): ''' Function that gets the edge list and defines edge connectivity and returns it as a list. ''' ## TODO: Extend the SNCF with data also returns the edge list. edgeList=[] basName=os.path.basename(fileName) extName=basName.split('.') if extName[1]=='sncf' or extName=='SNCF': try: fileHandler=open(fileName) allLines=fileHandler.readlines() numLines=sncfheaders.countLines(fileName) strtNum=filesanity.findHeader('@start_elist_(edgName,frmId,toId)',fileName) endNum=filesanity.findHeader('@end_elist',fileName) # The reason why the interpreter prints the '@end_elist' line is because, # there is a \n character (new line) in the end. # So, endNum-1 should do the trick. for itr in range (strtNum,endNum-1): if allLines[itr]=='\n': sncfheaders.readIgnore() else: edgeList.append(allLines[itr]) return edgeList except IOError,WindowsError: print 'Error. Filename %s does not exist. Please check the name of the file and try again.' %(fileName)
def betweenHeaders(startHeaderName,endHeaderName,fileName): ''' Returns the lines in between start and end header(s). ''' fileHandler=open(fileName) allLines=fileHandler.readlines() # Count the number of lines in the file noLines=sncfheaders.countLines(fileName) # Computer the indices of the search-terms. sInd=filesanity.findHeader(startHeaderName,fileName) eInd=filesanity.findHeader(endHeaderName,fileName) # print sInd,eInd Uncomment for diagnostics # Initiate linebuffer lineBuff=[] # Loop through the lines and store the contents in the buffer. for itr in range(sInd,eInd-1): if allLines[itr]=='\n': sncfheaders.readIgnore() else: lineBuff.append(allLines[itr]) return lineBuff
def getCoordinates (fileName): ''' Function that writes the co-ordinate date to a write buffer. ''' basName = os.path.basename (fileName) extName = basName.split ('.') retCoords = [] if extName [1] == 'sncf' or extName == 'SNCF': try: fileHandler=open(fileName) allLines=fileHandler.readlines() numLines = sncfheaders.countLines(fileName) strtNum = filesanity.findHeader('@start_coord_node_id_(x,y)',fileName)# Returns the <line_number+1> endNum = filesanity.findHeader('@end_coord',fileName) # The reason why the interpreter prints the '@end_sncf' line is because, there is a \n character (new line) in the end. # So, endNum-1 should do the trick. for itr in range (strtNum,endNum-1): if allLines [itr] == '\n': sncfheaders.readIgnore() else: retCoords.append(allLines[itr]) return retCoords except IOError,WindowsError: print 'Error. Filename %s does not exist. Please check the name of the file and try again.'%(fileName)