pydotfactory.py
Go to the documentation of this file.
1 #
2 # License: Yujin
3 #
4 ##############################################################################
5 # Description
6 ##############################################################################
7 
8 """
9 .. module:: pydotfactory
10  :platform: Unix
11  :synopsis: Repackaging of the limiting ROS qt_dotgraph.pydotfactory module.
12 
13 Oh my spaghettified magnificence,
14 Bless my noggin with a tickle from your noodly appendages!
15 
16 """
17 
18 ##############################################################################
19 # Imports
20 ##############################################################################
21 
22 from distutils.version import LooseVersion
23 import urllib
24 
25 import pydot
26 # work around for https://bugs.launchpad.net/ubuntu/+source/pydot/+bug/1321135
27 import pyparsing
28 pyparsing._noncomma = "".join([c for c in pyparsing.printables if c != ","])
29 
30 ##############################################################################
31 # Classes
32 ##############################################################################
33 
34 
35 # Reference implementation for a dotcode factory
36 class PydotFactory():
37 
38  def __init__(self):
39  pass
40 
41  def escape_label(self, name):
42  if name in ['graph', 'subgraph', 'node', 'edge']:
43  ret = "%s_" % name
44  else:
45  ret = name
46  return ret
47 
48  def escape_name(self, name):
49  ret = urllib.quote(name.strip())
50  ret = ret.replace('/', '_')
51  ret = ret.replace('%', '_')
52  ret = ret.replace('-', '_')
53  return self.escape_label(ret)
54 
55  def get_graph(self, graph_type='digraph', rank='same', simplify=True, rankdir='TB', ranksep=0.2, compound=True):
56  # Lucid version of pydot bugs with certain settings, not sure which version exactly fixes those
57  if LooseVersion(pydot.__version__) > LooseVersion('1.0.10'):
58  graph = pydot.Dot('graphname',
59  graph_type=graph_type,
60  rank=rank,
61  rankdir=rankdir,
62  simplify=simplify
63  )
64  graph.set_ranksep(ranksep)
65  graph.set_compound(compound)
66  else:
67  graph = pydot.Dot('graphname',
68  graph_type=graph_type,
69  rank=rank,
70  rankdir=rankdir)
71  return graph
72 
73  def add_node_to_graph(self,
74  graph,
75  nodename,
76  nodelabel=None,
77  shape='box',
78  color=None,
79  url=None,
80  tooltip=None):
81  """
82  creates a node item for this factory, adds it to the graph.
83  Node name can vary from label but must always be same for the same node label
84  """
85  if nodename is None or nodename == '':
86  raise ValueError('Empty Node name')
87  if nodelabel is None:
88  nodelabel = nodename
89  node = pydot.Node(self.escape_name(nodename))
90  node.set_shape(shape)
91  node.set_label(self.escape_label(nodelabel))
92  if tooltip is not None:
93  node.set_tooltip(tooltip)
94  if url is not None:
95  node.set_URL(self.escape_name(url))
96  if color is not None:
97  node.set_color(color)
98  graph.add_node(node)
99 
100  def add_subgraph_to_graph(self,
101  graph,
102  subgraphname,
103  rank='same',
104  simplify=True,
105  rankdir='TB',
106  ranksep=0.2,
107  compound=True,
108  color=None,
109  shape='box',
110  style='bold',
111  subgraphlabel=None):
112  """
113  creates a cluster subgraph item for this factory, adds it to the graph.
114  cluster name can vary from label but must always be same for the same node label.
115  Most layouters require cluster names to start with cluster.
116  """
117  if subgraphname is None or subgraphname == '':
118  raise ValueError('Empty subgraph name')
119  g = pydot.Cluster(self.escape_name(subgraphname), rank=rank, rankdir=rankdir, simplify=simplify, color=color)
120  if 'set_style' in g.__dict__:
121  g.set_style(style)
122  if 'set_shape' in g.__dict__:
123  g.set_shape(shape)
124  if LooseVersion(pydot.__version__) > LooseVersion('1.0.10'):
125  g.set_compound(compound)
126  g.set_ranksep(ranksep)
127  subgraphlabel = subgraphname if subgraphlabel is None else subgraphlabel
128  subgraphlabel = self.escape_label(subgraphlabel)
129  if subgraphlabel:
130  g.set_label(subgraphlabel)
131  if 'set_color' in g.__dict__:
132  if color is not None:
133  g.set_color(color)
134  graph.add_subgraph(g)
135  return g
136 
137  def add_edge_to_graph(self, graph, nodename1, nodename2, label=None, url=None, simplify=True, style=None, penwidth=1, color=None):
138  if simplify and LooseVersion(pydot.__version__) < LooseVersion('1.0.10'):
139  if graph.get_edge(self.escape_name(nodename1), self.escape_name(nodename2)) != []:
140  return
141  edge = pydot.Edge(self.escape_name(nodename1), self.escape_name(nodename2))
142  if label is not None and label != '':
143  edge.set_label(label)
144  if url is not None:
145  edge.set_URL(self.escape_name(url))
146  if style is not None:
147  edge.set_style(style)
148  edge.obj_dict['attributes']['penwidth'] = str(penwidth)
149  if color is not None:
150  edge.obj_dict['attributes']['colorR'] = str(color[0])
151  edge.obj_dict['attributes']['colorG'] = str(color[1])
152  edge.obj_dict['attributes']['colorB'] = str(color[2])
153  graph.add_edge(edge)
154 
155  def create_dot(self, graph):
156  dot = graph.create_dot()
157  # sadly pydot generates line wraps cutting between numbers
158  return dot.replace("\\\n", "")
def add_subgraph_to_graph(self, graph, subgraphname, rank='same', simplify=True, rankdir='TB', ranksep=0.2, compound=True, color=None, shape='box', style='bold', subgraphlabel=None)
def add_edge_to_graph(self, graph, nodename1, nodename2, label=None, url=None, simplify=True, style=None, penwidth=1, color=None)
def get_graph(self, graph_type='digraph', rank='same', simplify=True, rankdir='TB', ranksep=0.2, compound=True)
Definition: pydotfactory.py:55
def add_node_to_graph(self, graph, nodename, nodelabel=None, shape='box', color=None, url=None, tooltip=None)
Definition: pydotfactory.py:80


rqt_py_trees
Author(s): Thibault Kruse, Michal Staniaszek, Daniel Stonier, Naveed Usmani
autogenerated on Mon Jun 10 2019 14:55:56