-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgraph_orbids.py
More file actions
176 lines (142 loc) · 5.1 KB
/
graph_orbids.py
File metadata and controls
176 lines (142 loc) · 5.1 KB
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
import sys
class GraphObject:
def __init__(self):
self.properties = {}
def set_property(self, name, value):
self.properties[name] = value
def save_dgml_properties(self, f):
for id in self.properties:
f.write(' {}="{}"'.format(id, self.properties[id]))
def save_dot_properties(self, f):
for id in self.properties:
f.write(' {}="{}"'.format(id.lower(), self.properties[id]))
class Node(GraphObject):
def __init__(self, id):
super(Node, self).__init__()
self.id = id
self.properties = {}
def save_dgml(self, f):
f.write('<Node Id="{}"'.format(self.id))
self.save_dgml_properties(f)
f.write("/>\n")
def save_dot(self, f):
f.write(' "{}" ['.format(self.id))
self.save_dgml_properties(f)
f.write("];\n")
class Link(GraphObject):
def __init__(self, source, target):
super(Link, self).__init__()
self.source = source
self.target = target
def save_dgml(self, f):
f.write('<Link Source="{}" Target="{}"'.format(self.source.id, self.target.id))
self.save_dgml_properties(f)
f.write("/>\n")
def save_dot(self, f):
f.write(' "{}" -> "{}" ['.format(self.source.id, self.target.id))
self.save_dot_properties(f)
f.write("];\n")
class Graph:
def __init__(self):
self.nodes = {}
self.links = {}
def get_or_create_node(self, id):
if id not in self.nodes:
node = Node(id)
self.nodes[id] = node
else:
node = self.nodes[id]
return node
def get_or_create_link(self, source, target):
id = source.id + "->" + target.id
if id not in self.links:
link = Link(source, target)
self.links[id] = link
else:
link = self.links[id]
return link
def save_dgml(self, filename):
with open(filename, "w") as f:
f.write('<DirectedGraph xmlns="http://schemas.microsoft.com/vs/2009/dgml">\n')
f.write('<Nodes>\n')
for id in self.nodes:
node = self.nodes[id]
node.save_dgml(f)
f.write('</Nodes>\n')
f.write('<Links>\n')
for id in self.links:
link = self.links[id]
link.save_dgml(f)
f.write('</Links>\n')
f.write('</DirectedGraph>\n')
def save_dot(self, filename):
with open(filename, "w") as f:
f.write('digraph {\n')
for id in self.nodes:
node = self.nodes[id]
node.save_dot(f)
for id in self.links:
link = self.links[id]
link.save_dot(f)
f.write('}\n')
class Parser:
def parse(self, filename):
graph = Graph()
with open(filename, "r") as f:
lines = f.readlines()
for line in lines:
if "(" not in line:
print("Ignoring line: {}".format(line))
continue
i = line.index("(")
file = line[:i].strip()
id = self.get_node_id(file)
node = graph.get_or_create_node(id)
node.set_property("File", file)
node.set_property("Category", "Class")
orbid = None
r = self.parse_line(line)
if r:
type, orbid = r
topic = graph.get_or_create_node(orbid)
topic.set_property("Category", "Topic")
if type == "publish":
graph.get_or_create_link(node, topic)
else:
graph.get_or_create_link(topic, node)
return graph
def parse_line(self, line):
pub_prefixes = ["orb_publish(ORB_ID(", "orb_publish_auto(ORB_ID("]
for prefix in pub_prefixes:
if prefix in line:
i = line.index(prefix)
tail = line[i + len(prefix):]
i = tail.index(")")
return ("publish", tail[:i])
sub_prefixes = ["orb_subscribe(ORB_ID(", "orb_subscription(ORB_ID(",
"orb_copy(ORB_ID(", "copy_if_updated(ORB_ID("]
for prefix in sub_prefixes:
if prefix in line:
i = line.index(prefix)
tail = line[i + len(prefix):]
i = tail.index(")")
return ("subscribe", tail[:i])
if "orb_subscribe_multi" in line:
return None # ignore these for now
if "orb_advertise" in line:
return None # ignore this setup code.
if "== ORB_ID" in line or "= ORB_ID" in line:
return None
print("What is this about? {}".format(line))
return None
def get_node_id(self, filename):
parts = filename.split("\\")
if len(parts) > 2:
# return the directory name, not the file name
return parts[len(parts) - 2]
if __name__ == "__main__":
filename = sys.argv[1]
p = Parser()
graph = p.parse(filename)
graph.save_dgml("graph.dgml")
graph.save_dot("graph.dot")