Created
September 27, 2013 13:07
-
-
Save fritz0705/6728328 to your computer and use it in GitHub Desktop.
Simple gist to visualize a database
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# coding: utf-8 | |
import hashlib | |
import argparse | |
import lglass.database.file | |
import lglass.rpsl | |
def spec_id(spec): | |
return "x" + hashlib.md5(":".join(spec).encode()).hexdigest() | |
def main(): | |
argparser = argparse.ArgumentParser() | |
argparser.add_argument("database") | |
args = argparser.parse_args() | |
db = lglass.database.file.FileDatabase(args.database) | |
nodes = set() | |
edges = set() | |
for type, primary_key in db.list(): | |
obj = db.get(type, primary_key) | |
if obj in nodes: | |
continue | |
nodes.add(obj) | |
for inverse in obj.inverses(db): | |
edges.add((obj.spec, inverse.spec)) | |
print('digraph {') | |
print('overlap="scale";') | |
print('outputMode=edgesfirst;') | |
print('edge [color="#0000007f"];') | |
print('node [shape=box];') | |
for node in nodes: | |
print('{id} [label="{label}"];'.format( | |
id=spec_id(node.spec), | |
label=": ".join(node.spec))) | |
for edge in edges: | |
print("{left} -> {right};".format( | |
left=spec_id(edge[0]), | |
right=spec_id(edge[1]))) | |
print("}") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment