dot.py
Go to the documentation of this file.
1 import pygraphviz as pgv
2 
3 def dotencode(string):
4  return string.replace("<","&lt;").replace(">","&gt;")
5 
6 def node_port_string(letter, types, addtypes=False):
7  if not types:
8  return None
9  ports = dict()
10  for ind,typ in enumerate(types):
11  port_name = letter+str(ind)
12  port_label = port_name.upper()
13  ## C++ templated types tend to screw up dot
14  if addtypes:
15  port_label += "(%s)" % dotencode(typ)
16  ports[port_name] = port_label
17  items = sorted(ports.items())
18  inner = '|'.join([ "<%s>%s" % p for p in items ])
19  return "{|%s|}"%inner
20 
21 
22 
23 def nodetype_label(nt, addtypes=False):
24  '''
25  Return a GraphViz node label defined based on given NodeType <nt>.
26  '''
27  lines = list()
28  lines.append(node_port_string("i", nt.input_types, addtypes))
29  lines.append("{%s (cat:%d con:%d)}" % (nt.type, nt.category, nt.concurrency))
30  lines.append(node_port_string("o", nt.output_types, addtypes))
31  label = '|'.join([l for l in lines if l])
32  return "{" + label + "}"
33 
34 
35 
36 def gvgraph_nodetypes(nxgraph, nodetypes):
37  '''Return a GraphViz graph made from the NX graph. The <nodetypes> is
38  a NodeType dictionary and will be used to define the nodes.
39  '''
40  ag = pgv.AGraph(directed=True, strict=False)
41  ag.node_attr['shape'] = 'record'
42 
43  for nn in nxgraph.nodes():
44  typ = str(nn)
45  if ':' in typ:
46  typ = typ.split(':',1)[0]
47  nt = nodetypes[typ]
48  ag.add_node(nn, label = nodetype_label(nt))
49 
50  for nt,nh,nd in nxgraph.edges(data=True):
51  key = ' {tail_port}-{head_port} '.format(**nd)
52  dt = nd.get('data_type')
53  if dt:
54  key += "(%s)" % dt
55  ag.add_edge(nt,nh, key=key, label=key,
56  tailport='o'+str(nd.get('tail_port',0)),
57  headport='i'+str(nd.get('head_port',0)))
58 
59  return ag
60 
61 
62 
63 def edge_port_string(letter, edges):
64  port_key = "tail_port"
65  if letter == "i":
66  port_key = "head_port"
67 
68  ports = dict()
69  for t,h,dat in edges:
70  port_num = dat[port_key]
71  port_name = letter+str(port_num)
72  port_label = port_name.upper()
73  dt = dat.get('data_type')
74  if (dt):
75  port_label += "(%s)" % dt
76  ports[port_name] = port_label
77 
78  items = sorted(ports.items())
79  inner = '|'.join([ "<%s>%s" % p for p in items ])
80  return "{|%s|}"%inner
81 
82 def edgetype_label(nxnode, inedges, outedges):
83  '''
84  Return a GraphViz node label defined based on its collection of input and output edges.
85  '''
86  lines = list()
87  lines.append(edge_port_string('i',inedges))
88  lines.append(nxnode)
89  lines.append(edge_port_string('o',outedges))
90  label = '|'.join([l for l in lines if l])
91  return label
92 
93 
94 def gvgraph(nxgraph):
95  '''Return a GraphViz graph made from the NX graph.'''
96 
97  ag = pgv.AGraph(directed=True, strict=False, overlap='false', splines='true')
98  ag.node_attr['shape'] = 'record'
99 
100  for nn in nxgraph.nodes():
101  nodestring = edgetype_label(nn, nxgraph.in_edges(nn,data=True), nxgraph.out_edges(nn,data=True))
102  label = "{" + nodestring + "}"
103  ag.add_node(str(nn), label=label)
104  for nt,nh,nd in nxgraph.edges(data=True):
105  key = ' {tail_port}-{head_port} '.format(**nd)
106  dt = nd.get('data_type')
107  if dt:
108  key += "(%s)" % dt
109  ag.add_edge(nt,nh, key=key, label=key,
110  tailport='o'+str(nd.get('tail_port',0)),
111  headport='i'+str(nd.get('head_port',0)))
112 
113  return ag
114 
static bool format(QChar::Decomposition tag, QString &str, int index, int len)
Definition: qstring.cpp:11496
auto enumerate(Iterables &&...iterables)
Range-for loop helper tracking the number of iteration.
Definition: enumerate.h:69
def edge_port_string(letter, edges)
Definition: dot.py:63
def node_port_string(letter, types, addtypes=False)
Definition: dot.py:6
def edgetype_label(nxnode, inedges, outedges)
Definition: dot.py:82
def gvgraph_nodetypes(nxgraph, nodetypes)
Definition: dot.py:36
def dotencode(string)
Definition: dot.py:3
def nodetype_label(nt, addtypes=False)
Definition: dot.py:23
def gvgraph(nxgraph)
Definition: dot.py:94
static QCString str