Skip to content

Instantly share code, notes, and snippets.

@weaming
Forked from lad1337/alembic-graph
Created July 16, 2020 06:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weaming/2463b0ec3f6ee79f5d38dcfb8b7a0b94 to your computer and use it in GitHub Desktop.
Save weaming/2463b0ec3f6ee79f5d38dcfb8b7a0b94 to your computer and use it in GitHub Desktop.
#!/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