polygraph/polygraph.py

61 lines
2.0 KiB
Python
Executable File

#!/usr/bin/python3
import pygraphviz as pgv
import json
import argparse
def parse_args():
parser = argparse.ArgumentParser(description='Generate graphs of polyamorous family networks from data files')
parser.add_argument('--input', '-i', default='network.json', help='Input file in json format')
parser.add_argument('--output', '-o', default='network.png', help='Output image file. Format auto-detected from file extension')
parser.add_argument('--color', '-c', action='store_true', help='Include node colors in output graph')
return parser.parse_args()
def main():
settings = parse_args()
try:
data = json.load(open(settings.input, 'r'))
except Exception as e:
print("Error parsing data file: " + str(e))
return
graph = pgv.AGraph()
graph.graph_attr['overlap'] = 'prism'
graph.node_attr['style'] = 'filled'
graph.edge_attr['arrowsize'] = 0.5
# Add nodes to graph
for user in data['members']:
node_color = 'white'
if settings.color and 'color' in user:
node_color = user['color']
graph.add_node(user['name'], fillcolor=node_color)
# if user.has_key('cloud') and user['cloud'] = true:
# graph.add_node()
# graph.add_edge()
# Add edges to graph
for relationship in data['relationships']:
edge_style = 'solid'
if 'style' in relationship: edge_style = relationship['style']
edge_color = 'black'
if 'color' in relationship: edge_color = relationship['color']
edge_label = None
if 'label' in relationship: edge_label = relationship['label']
member0 = relationship['members'][0]
member1 = relationship['members'][1]
graph.add_edge(member0, member1, style=edge_style, color=edge_color)
if edge_label is not None:
graph.get_edge(member0, member1).attr['label'] = edge_label
graph.layout()
graph.draw(settings.output)
if __name__ == '__main__': main()