2018-07-16 16:19:46 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import sys
|
|
|
|
|
|
|
|
from graphviz import Digraph
|
|
|
|
|
|
|
|
|
|
|
|
class Message(object):
|
|
|
|
def __init__(self, name, attrs):
|
|
|
|
self.name = name
|
2018-07-17 16:33:59 +00:00
|
|
|
t = attrs[0][0]
|
|
|
|
if t in ["start", "end", "auxstart", "auxend", "embed", "ignore"]:
|
|
|
|
self.typ = t
|
|
|
|
attrs = attrs[1:]
|
|
|
|
elif t == "next":
|
|
|
|
self.typ = "normal"
|
|
|
|
attrs = attrs
|
|
|
|
else:
|
|
|
|
raise ValueError("wrong message type in message '%s'" % m.name)
|
|
|
|
self.next = []
|
|
|
|
for a in attrs:
|
|
|
|
if a[0] == "next":
|
|
|
|
self.next.append(a[1])
|
2018-07-16 16:19:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
def generate_messages(files):
|
|
|
|
attrs = []
|
2018-07-17 16:33:59 +00:00
|
|
|
msgs = {}
|
2018-07-16 16:19:46 +00:00
|
|
|
for f in files:
|
|
|
|
for line in open(f, "rt").readlines():
|
|
|
|
line = line.rstrip()
|
|
|
|
if line.startswith(" * @"):
|
|
|
|
attrs.append(line[4:].split(" "))
|
|
|
|
elif line.startswith("message "):
|
|
|
|
name = line[8:-2]
|
2018-07-17 16:33:59 +00:00
|
|
|
msgs[name] = Message(name, attrs)
|
2018-07-16 16:19:46 +00:00
|
|
|
attrs = []
|
|
|
|
return msgs
|
|
|
|
|
|
|
|
|
|
|
|
def generate_graph(msgs, fn):
|
|
|
|
dot = Digraph(format="png")
|
|
|
|
dot.attr(rankdir="LR")
|
2018-07-17 16:33:59 +00:00
|
|
|
for m in msgs.values():
|
|
|
|
if m.typ == "start":
|
2018-07-16 16:19:46 +00:00
|
|
|
dot.node(m.name, shape="box", color="blue")
|
2018-07-17 16:33:59 +00:00
|
|
|
elif m.typ == "end":
|
2018-07-16 16:19:46 +00:00
|
|
|
dot.node(m.name, shape="box", color="green3")
|
2018-07-17 16:33:59 +00:00
|
|
|
elif m.typ == "auxstart":
|
2018-07-16 16:19:46 +00:00
|
|
|
dot.node(m.name, shape="diamond", color="blue")
|
2018-07-17 16:33:59 +00:00
|
|
|
elif m.typ == "auxend":
|
2018-07-16 16:19:46 +00:00
|
|
|
dot.node(m.name, shape="diamond", color="green3")
|
2018-07-17 16:33:59 +00:00
|
|
|
elif m.typ == "normal":
|
|
|
|
dot.node(m.name)
|
|
|
|
|
|
|
|
for m in msgs.values():
|
|
|
|
for n in m.next:
|
|
|
|
dot.edge(m.name, n)
|
2018-07-16 16:19:46 +00:00
|
|
|
dot.render(fn)
|
|
|
|
|
|
|
|
|
|
|
|
msgs = generate_messages(sys.argv)
|
|
|
|
generate_graph(msgs, "graph.gv")
|