Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/local/bin/python3
# Created : 2020-07-16 13:06:10
import sys
import os
from graphviz import Digraph
fmt = os.getenv('GRAPH_FMT', 'dot')
dot = Digraph(comment='alembic', format=fmt)
def split_line(line):
bases, _, name_and_target = line.partition('->')
id_and_stuff, _, desc = name_and_target.strip().partition(',')
desc = desc.strip()
id_, _, type_ = id_and_stuff.partition(' ')
id_ = id_.replace('<', '').replace('>', '')
type_ = type_[1:-1]
lowers = []
dependencies = []
for base in bases.split(' '):
base = base.strip().replace(',', '')
if not base:
continue
if base.startswith('(') and base.endswith(')'):
dependencies.append(base.replace('(', '').replace(')', ''))
if base.startswith('<') and base.endswith('>'):
dependencies.append(base.replace('<', '').replace('>', ''))
else:
lowers.append(base)
return id_, lowers, dependencies, desc, type_
for line in sys.stdin.readlines():
id_, lowers, dependencies, desc, type_ = split_line(line)
if not id_:
continue
dot.node(id_, id_ + ":\n" + desc)
for lower in lowers:
dot.edge(lower, id_)
for dep in dependencies:
dot.edge(id_, dep, label='depends on', _attributes=dict(style='dotted'))
if fmt=='dot':
print(dot.pipe().decode())
else:
dot.render('/tmp/alembic.gv', view=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment