def OpenDotGraph(fileName):
    """ Reads in a graph from file fileName. File-format is supposed
        to be dot (*.dot) used in """
    G = Graph()
    G.directed = 1
    E = VertexLabeling()
    W = EdgeWeight(G)
    L = VertexLabeling()
    VLabel = VertexLabeling()
    ELabel = EdgeLabeling()
    
    import re
    file = open(fileName, 'r')
    lines = file.readlines()
    file.close()
    
    dot2graph = {}
    
    for l in lines[3:]:
        items = string.split(l)
        if len(items) < 2:
            break
        if items[1] != '->':
            v = G.AddVertex()
            dot_v = int(items[0])
            L[v] = "%d" % dot_v
            dot2graph[dot_v] = v
            m = re.search('label=("[^"]+")', l)
            VLabel[v] = m.group(1)[1:-1]
            m = re.search('pos="(\d+),(\d+)"', l)
            x = int(m.group(1))
            y = int(m.group(2))
            E[v] = Point2D(x,y)
        else:
            m = re.search('(\d+) -> (\d+)', l)
            v = dot2graph[int(m.group(1))]
            w = dot2graph[int(m.group(2))]
            m = re.search('label=("[^"]+")', l)
            #print l
            #print v,w,m.group(1)
            G.AddEdge(v,w)
            weight = float(m.group(1)[1:-1])
            W[(v,w)] = weight
            ELabel[(v,w)] = "%0.2f" % weight
            
    G.embedding = E
    G.labeling  = L
    G.nrEdgeWeights = 1
    G.edgeWeights[0] = W
    G.vertexAnnotation = VLabel
    G.edgeAnnotation = ELabel
    return G
def OpenGMLGraph(fileName):
    """ Reads in a graph from file fileName. File-format is supposed
        to be GML (*.gml) """
    G = Graph()
    G.directed = 0
    E = VertexLabeling()
    W = EdgeWeight(G)
    L = VertexLabeling()
    VLabel = VertexLabeling()
    ELabel = EdgeLabeling()
    
    file = open(fileName, 'r')
    g = ParseGML(file)
    file.close()
    
    if g[0][0] != 'graph':
        log.error("Serious format error in %s. first key is not graph" % fileName)
        return
    else:
        l = g[0][1]
        for i in xrange(len(l)):
        
            key   = l[i][0]
            value = l[i][1]
            
            if key == 'node':
            
                d = PairListToDictionary(value)
                v = G.AddVertex()
                
                try:
                    VLabel[v] = eval(d['label'])
                    P = PairListToDictionary(d['graphics'])
                    E[v] = Point2D(eval(P['x']), eval(P['y']))
                    
                except:
                    d = None 
                    P = None
                    
            elif key == 'edge':
            
                d = PairListToDictionary(value)
                
                try:
                    s = eval(d['source'])
                    t = eval(d['target'])
                    G.AddEdge(s,t)
                    ELabel[(s,t)] = eval(d['label'])
                    W[(s,t)] = 0
                except:
                    d = None 
                    
            elif key == 'directed':
                G.directed = 1 
                
    for v in G.vertices:
        L[v] = v
        
    G.embedding = E
    G.labeling  = L
    G.nrEdgeWeights = 1
    G.edgeWeights[0] = W
    G.vertexAnnotation = VLabel
    G.edgeAnnotation = ELabel
    
    return G
def OpenCATBoxGraph(_file):
    """ Reads in a graph from file fileName. File-format is supposed
        to be from old CATBOX++ (*.cat) """
    G = Graph()
    E = VertexLabeling()
    W = EdgeWeight(G)
    L = VertexLabeling()
    
    # get file from name or file object
    graphFile=None
    if type(_file) in types.StringTypes:
        graphFile = open(_file, 'r')
    elif type(_file)==types.FileType or issubclass(_file.__class__,StringIO.StringIO):
        graphFile=_file
    else:
        raise Exception("got wrong argument")
        
    lineNr = 1
    
    firstVertexLineNr = -1    
    lastVertexLineNr  = -1
    firstEdgeLineNr   = -1
    lastEdgeLineNr    = -1
    intWeights        = 0
    
    while 1:
    
        line = graphFile.readline()
        
        if not line:
            break
            
        if lineNr == 2: # Read directed and euclidian
            splitLine = split(line[:-1],';')	    
            G.directed = eval(split(splitLine[0],':')[1])
            G.simple = eval(split(splitLine[1],':')[1])
            G.euclidian = eval(split(splitLine[2],':')[1])
            intWeights = eval(split(splitLine[3],':')[1])
            nrOfEdgeWeights = eval(split(splitLine[4],':')[1])
            nrOfVertexWeights = eval(split(splitLine[5],':')[1])
            for i in xrange(nrOfEdgeWeights):
                G.edgeWeights[i] = EdgeWeight(G)
            for i in xrange(nrOfVertexWeights):
                G.vertexWeights[i] = VertexWeight(G)
                
                
        if lineNr == 5: # Read nr of vertices
            nrOfVertices = eval(split(line[:-2],':')[1]) # Strip of "\n" and ; 
            firstVertexLineNr = lineNr + 1
            lastVertexLineNr  = lineNr + nrOfVertices
            
        if  firstVertexLineNr <= lineNr and lineNr <= lastVertexLineNr: 
            splitLine = split(line[:-1],';')
            v = G.AddVertex()
            x = eval(split(splitLine[1],':')[1])
            y = eval(split(splitLine[2],':')[1])
            for i in xrange(nrOfVertexWeights):
                w = eval(split(splitLine[3+i],':')[1])
                G.vertexWeights[i][v] = w
                
            E[v] = Point2D(x,y)
            
        if lineNr == lastVertexLineNr + 1: # Read Nr of edges
            nrOfEdges = eval(split(line[:-2],':')[1]) # Strip of "\n" and ; 
            firstEdgeLineNr = lineNr + 1
            lastEdgeLineNr  = lineNr + nrOfEdges
            
        if firstEdgeLineNr <= lineNr and lineNr <= lastEdgeLineNr: 
            splitLine = split(line[:-1],';')
            h = eval(split(splitLine[0],':')[1])
            t = eval(split(splitLine[1],':')[1])
            G.AddEdge(t,h)
            for i in xrange(nrOfEdgeWeights):
                G.edgeWeights[i][(t,h)] = eval(split(splitLine[3+i],':')[1])
                
        lineNr = lineNr + 1
        
    graphFile.close()
    
    for v in G.vertices:
        L[v] = v
        
    G.embedding = E
    G.labeling  = L
    if intWeights:
        G.Integerize('all')
        for i in xrange(nrOfVertexWeights):
            G.vertexWeights[i].Integerize()
            
    return G