#!/usr/bin/env python

import json

def read_file(filename):
    with open(filename, 'r') as f:
        for line in f:
            yield line.strip()
            
class json_encoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, set):
            return list(obj)
        return json.JSONEncoder.default(self, obj)
         
def main():
    graph = dict()

    for line in read_file("connections.txt"):
        tokens = line.split(" ")
        tokens[0] = int(tokens[0])
        tokens[1] = int(tokens[1])

        # add vertex 1
        if tokens[0] not in graph:
            graph[tokens[0]] = {
                'siblings': [],
                'transport': set()
            }
        graph[tokens[0]]['siblings'].append({
            'vertex': tokens[1],
            'edge': tokens[2]
        })
        graph[tokens[0]]['transport'].add(tokens[2])
        # add vertex 2
        if tokens[1] not in graph:
            graph[tokens[1]] = {
                'siblings': [],
                'transport': set()
            }
        graph[tokens[1]]['siblings'].append({
            'vertex': tokens[0],
            'edge': tokens[2]
        })
        graph[tokens[1]]['transport'].add(tokens[2])

    print(json.dumps(graph, sort_keys=True, indent=2, cls=json_encoder))
if __name__ == "__main__":
    main()


