fb8212f096621542f7ee72c50dc12482bb7c5116
[nepi.git] / src / nepi / util / plot.py
1 """
2     NEPI, a framework to manage network experiments
3     Copyright (C) 2013 INRIA
4
5     This program is free software: you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation, either version 3 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 """
19
20 import networkx
21 import tempfile
22
23 class Plotter(object):
24     def __init__(self, box):
25         self._graph = networkx.Graph(graph = dict(overlap = "false"))
26
27         traversed = set()
28         self._traverse_boxes(traversed, box)
29
30     def _traverse_boxes(self, traversed, box):
31         traversed.add(box.guid)
32
33         self._graph.add_node(box.label, 
34                 width = 50/72.0, # 1 inch = 72 points
35                 height = 50/72.0, 
36                 shape = "circle")
37
38         for b in box.connections:
39             self._graph.add_edge(box.label, b.label)
40             if b.guid not in traversed:
41                 self._traverse_boxes(traversed, b)
42
43     def plot(self):
44         f = tempfile.NamedTemporaryFile(delete=False)
45         networkx.draw_graphviz(self._graph)
46         networkx.write_dot(self._graph, f.name)
47         f.close()
48         return f.name
49