forked from pfalcon/xdot.py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filtered-viewer.py
executable file
·183 lines (151 loc) · 5.49 KB
/
filtered-viewer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/env python
#
# Copyright 2013 Paul Sokolovsky
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#
# Please note that unlike xdot module, this application is licensed
# under GPLv3, not LGPLv3.
#
import sys
from pprint import pprint
import gtk
import xdot
def is_hidden(graph, node):
return graph.get(node, {}).get("hide", False)
def graph2dot(graph):
res = "digraph G {\n"
for n, meta in graph.iteritems():
if meta.get("hide", False):
continue
res += n + " " + meta.get("attrs", "") + "\n"
for dst in meta.get("edges_to", []):
if not is_hidden(graph, dst):
res += "%s -> %s\n" % (n, dst)
return res + "}\n"
def dot2graph(dot):
graph = {}
first = True
for l in dot:
l = l.strip()
if not l:
continue
if first:
first = False
continue
l = l.strip()
if l[-1] == ";":
l = l[:-1]
if l == "}":
break
if "->" in l:
src, dst = [x.strip() for x in l.split("->", 1)]
graph.setdefault(src, {}).setdefault("edges_to", []).append(dst)
graph.setdefault(dst, {})
else:
n = l
attrs = ""
if "[" in l:
n, attrs = l.split("[", 1)
attrs = "[" + attrs
graph.setdefault(n, {})["attrs"] = attrs
return graph
def subgraph(graph, node, newgraph={}):
newgraph[node] = graph[node]
for n in graph[node].get("edges_to", []):
subgraph(graph, n, newgraph)
class ViewerDotWidget(xdot.DotWidget):
def __init__(self):
xdot.DotWidget.__init__(self)
self.main_graph = None
def set_graph(self, graph):
if self.main_graph is None:
self.main_graph = graph
self.view_graph = graph
self.set_dotcode(graph2dot(graph))
def re_render(self):
self.set_graph(self.view_graph)
@staticmethod
def make_menu(items, obj, handler):
menu = gtk.Menu()
for i in items:
menu_item = gtk.MenuItem(i)
menu.append(menu_item)
menu_item.show()
menu_item.connect("activate", handler, (i, obj))
return menu
def on_click(self, element, event):
if event.button == 3:
print "right clicked:", element
if not element:
def menu_handler(widget, data):
print widget, data
action, el = data
if action == "Unhide all":
for n in self.view_graph.itervalues():
n["hide"] = False
self.re_render()
if action == "Back to whole":
self.set_graph(self.main_graph)
menu = self.make_menu(["Unhide all", "Back to whole"], None, menu_handler)
else:
def menu_handler(widget, data):
print widget, data
action, el = data
if action == "Hide":
self.view_graph[el.id]["hide"] = True
self.re_render()
elif action == "Collapse":
n = self.view_graph[el.id]
n["edges_to.hide"] = n["edges_to"]
n["edges_to"] = []
self.re_render()
elif action == "Uncollapse":
n = self.view_graph[el.id]
n["edges_to"] = n["edges_to.hide"]
del n["edges_to.hide"]
self.re_render()
elif action == "Subgraph":
new = {}
subgraph(self.view_graph, el.id, new)
self.set_graph(new)
node = self.view_graph[element.id]
items = ["Hide"]
if "edges_to.hide" in node:
items.append("Uncollapse")
else:
items.append("Collapse")
items.append("Subgraph")
menu = self.make_menu(items, element, menu_handler)
menu.popup(None, None, None, event.button, event.time)
return True
class MyDotWindow(xdot.DotWindow):
def __init__(self):
xdot.DotWindow.__init__(self, widget=ViewerDotWidget())
# Example of graph representation
graph = {
"Hello": {"attrs": '[URL="http://en.wikipedia.org/wiki/Hello"]', "edges_to": ["World"]},
"World": {"attrs": '[URL="http://en.wikipedia.org/wiki/World"]', "edges_to": ["foo"], "hide": False},
"foo": {"attrs": "[foo=1]", "hide": False},
"baz": {"attrs": "[foo=1]"},
}
def main():
window = MyDotWindow()
window.widget.set_graph(dot2graph(open(sys.argv[1])))
window.widget.zoom_to_fit()
window.connect('destroy', gtk.main_quit)
gtk.main()
if __name__ == '__main__':
main()