ff_merge_nodes_json 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #!/usr/bin/python
  2. #
  3. # Maximilian Wilhelm <max@rfc2324.org>
  4. # -- Tue 20 Jun 2017 06:40:18 PM CEST
  5. #
  6. import argparse
  7. import json
  8. import os
  9. import sys
  10. parser = argparse.ArgumentParser (description = 'Merge nodes.json files')
  11. parser.add_argument ('files', help = 'Path for nodes.json file(s)', nargs = '+')
  12. parser.add_argument ('--pretty-print', help = 'Pretty-print JSON output', action = 'store_true')
  13. args = parser.parse_args ()
  14. all_nodes = {}
  15. uberdict = {}
  16. # Read all nodes lists into all_nodes dict, thereby dropping any duplicate nodes.
  17. for file_path in args.files:
  18. try:
  19. with open (file_path, 'rb') as fh:
  20. nodes = json.load (fh)
  21. except IOError as (errno, strerror):
  22. print "Error while reading file '%s': %s" % (file_path, strerror)
  23. sys.exit (1)
  24. for node in nodes['nodes']:
  25. all_nodes[node['nodeinfo']['node_id']] = node
  26. for key in nodes.keys ():
  27. if key != 'nodes':
  28. uberdict[key] = nodes[key]
  29. uberdict['nodes'] = all_nodes.values ()
  30. # Print merged nodes.json's to stdout
  31. if args.pretty_print:
  32. print (json.dumps (uberdict, sort_keys = True, indent = 4, separators = (',', ': ')))
  33. else:
  34. print (json.dumps (uberdict))