1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-07-25 07:58:12 +00:00

protob: refactor graph.py

This commit is contained in:
Pavol Rusnak 2018-07-17 18:33:59 +02:00
parent f7df570194
commit b7e76e4e81
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D

View File

@ -7,12 +7,24 @@ from graphviz import Digraph
class Message(object): class Message(object):
def __init__(self, name, attrs): def __init__(self, name, attrs):
self.name = name self.name = name
self.attrs = attrs 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])
def generate_messages(files): def generate_messages(files):
attrs = [] attrs = []
msgs = [] msgs = {}
for f in files: for f in files:
for line in open(f, "rt").readlines(): for line in open(f, "rt").readlines():
line = line.rstrip() line = line.rstrip()
@ -20,7 +32,7 @@ def generate_messages(files):
attrs.append(line[4:].split(" ")) attrs.append(line[4:].split(" "))
elif line.startswith("message "): elif line.startswith("message "):
name = line[8:-2] name = line[8:-2]
msgs.append(Message(name, attrs)) msgs[name] = Message(name, attrs)
attrs = [] attrs = []
return msgs return msgs
@ -28,26 +40,21 @@ def generate_messages(files):
def generate_graph(msgs, fn): def generate_graph(msgs, fn):
dot = Digraph(format="png") dot = Digraph(format="png")
dot.attr(rankdir="LR") dot.attr(rankdir="LR")
for m in msgs: for m in msgs.values():
typ = m.attrs[0][0] if m.typ == "start":
if typ == "start":
dot.node(m.name, shape="box", color="blue") dot.node(m.name, shape="box", color="blue")
elif typ == "end": elif m.typ == "end":
dot.node(m.name, shape="box", color="green3") dot.node(m.name, shape="box", color="green3")
elif typ == "auxstart": elif m.typ == "auxstart":
dot.node(m.name, shape="diamond", color="blue") dot.node(m.name, shape="diamond", color="blue")
elif typ == "auxend": elif m.typ == "auxend":
dot.node(m.name, shape="diamond", color="green3") dot.node(m.name, shape="diamond", color="green3")
elif typ == "next": elif m.typ == "normal":
dot.node(m.name) # no attrs dot.node(m.name)
elif typ in ["embed", "ignore"]:
continue for m in msgs.values():
else: for n in m.next:
raise ValueError("wrong message type in message '%s'" % m.name) dot.edge(m.name, n)
for m in msgs:
for a in m.attrs:
if a[0] == "next":
dot.edge(m.name, a[1])
dot.render(fn) dot.render(fn)