def mark(): ok = False n = gv.firstnode(gr) while gv.ok(n): if gv.getv(n, 'color') == 'green': nh = gv.firsthead(n) while gv.ok(nh): if gv.getv(nh, 'color') != 'green': gv.setv(nh, 'color', 'green') ok = True nh = gv.nexthead(n, nh) n = gv.nextnode(gr, n) return ok
def __iter__(self): """ Iterates over item attributes. """ attr = gv.firstattr(self.handle) while gv.ok(attr): yield gv.nameof(attr), \ decode_page(gv.getv(self.handle, attr)) attr = gv.nextattr(self.handle, attr)
def __iter__(self): cur = gv.firstedge(self.parent.handle) while gv.ok(cur): yield (decode_page(gv.nameof(gv.tailof(cur))), decode_page(gv.nameof(gv.headof(cur)))), \ dict(self.graph._iterattrs(cur)) cur = gv.nextedge(self.parent.handle, cur)
def __iter__(self): """ Iterate over all items, yielding Graphviz.<type> items """ handle = self.parent.handle cur = getattr(gv, "first%s" % self.type)(handle) nextitem = getattr(gv, "next%s" % self.type) while gv.ok(cur): yield self.get(gv.nameof(cur)) cur = nextitem(handle, cur)
def initialise_nodes(self): # Bake in the node attributes from 'dot' layout gv.layout(self.gvo, 'dot') gv.render(self.gvo) # iterate over node attributes to get/set node positions # see gv.3python.pdf for more info # as well as https://mailman.research.att.com/pipermail/graphviz-interest/2006q1/003182.html n = gv.firstnode(self.gvo) #store min and max x and y minx = 0 miny = 0 maxx = None maxy = None # store the node label and position as reported by Dot layout nodepos = {} # {<node object>:(x,y)} while gv.ok(n) : # check that the iterator returned by firstnode is ok label = gv.nameof(n) spos = gv.getv(n,'pos').split(',') # list of strings (xpos,ypos) = [float(i) for i in spos] # convert to float node = self.dag.get_node_from_label(label) pos = node.get_position() if pos != None: # Set xpos and ypos if they are already defined in node.get_position() (xpos,ypos) = pos print xpos, ypos # set min and max values if minx > xpos: minx = xpos if maxx < xpos: maxx = xpos if miny > ypos: miny = ypos if maxy < ypos: maxy = ypos nodepos[node] = (xpos, ypos) #change node before iteration n = gv.nextnode(self.gvo, n) print "min", minx, miny print "max", maxx, maxy # Set the position in all nodes for node, pos in nodepos.iteritems(): node.set_position(pos)
def _iterattrs(self, handle=""): """ Iterate over the attributes of a graph item. If no handle attribute is given, iterates over the attributes of the root graph """ if not handle: handle = self.handle attr = gv.firstattr(handle) while gv.ok(attr): yield gv.nameof(attr), decode_page(gv.getv(handle, attr)) attr = gv.nextattr(handle, attr)
def _getNodesFromDAG(self): # Get the dotfile from the DAG dot = self.dag.get_dot() gvo = gv.readstring(dot) # Bake in the node attributes from 'dot' layout gv.layout(gvo, 'dot') gv.render(gvo) # iterate over node attributes to get/set node positions # see gv.3python.pdf for more info # as well as https://mailman.research.att.com/pipermail/graphviz-interest/2006q1/003182.html n = gv.firstnode(gvo) #store min and max x and y minx = 0 miny = 0 maxx = None maxy = None # store the node label and position as reported by Dot layout nodepos = {} # {<node object>:(x,y)} while gv.ok(n) : # check that the iterator returned by firstnode is ok label = gv.nameof(n) spos = gv.getv(n,'pos').split(',') # list of strings (xpos,ypos) = [float(i) for i in spos] # convert to float node = self.dag.get_node_from_label(label) pos = node.get_position() if pos != None: # Set xpos and ypos if they are already defined in node.get_position() (xpos,ypos) = pos # set min and max values if minx > xpos: minx = xpos if maxx < xpos: maxx = xpos if miny > ypos: miny = ypos if maxy < ypos: maxy = ypos nodepos[node] = (xpos, ypos) #change node before iteration n = gv.nextnode(gvo, n) # Set the position in all nodes and add them to the graph for node, pos in nodepos.iteritems(): node.set_position(pos) label = self.dag.get_label_from_node(node) v_node = v_Node(label) v_node.setPos(*pos) self.graphview.add(v_node) bounding = self.graphview.scene().itemsBoundingRect() #self.graphview.fitInView(bounding, QtCore.Qt.IgnoreAspectRatio) self.graphview.centerOn(bounding.center())
def get_edge_list(graph_h): """Generator to iterate over all edges of a graph""" handle = gv.firstedge(graph_h) while gv.ok(handle): yield handle handle = gv.nextedge(graph_h, handle)
n = gv.nextnode(gr, n) return ok if __name__ == "__main__": name = sys.argv[1] if name[-4:] != '.dot': print "wrong name", name exit name = name[:-4] gr = gv.read(name + '.dot') m = gv.findnode(gr, 'main') gv.setv(m, 'color', 'green') while mark(): pass n = gv.firstnode(gr) while gv.ok(n): if gv.getv(n, 'color') != 'green': gv.setv(n, 'fillcolor', 'red') gv.setv(n, 'style', 'filled') in_degree = 0 e = gv.firstin(n) while gv.ok(e): in_degree += 1 e = gv.nextin(n, e) if in_degree == 1: gv.setv(n, 'shape', 'diamond') n = gv.nextnode(gr, n) gv.write(gr, name + '-new.dot') gv.layout(gr, 'dot') gv.render(gr) gv.render(gr, 'fig', name + '.fig')