combine_dsn_list 1005 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #!/usr/bin/env python
  2. from __future__ import annotations
  3. import json
  4. from io import TextIOWrapper
  5. import click
  6. @click.command()
  7. @click.argument("old", type=click.File("rb"))
  8. @click.argument("new", type=click.File("rb"))
  9. @click.argument("out", type=click.File("wb"))
  10. def combine_json(old, new, out):
  11. old_data = json.load(old)
  12. new_data = json.load(new)
  13. combined_data = {}
  14. for key in old_data:
  15. if key in new_data:
  16. inner_combined = {}
  17. for inner_key in old_data[key]:
  18. if inner_key in new_data[key]:
  19. inner_combined[inner_key] = {
  20. "old": old_data[key][inner_key],
  21. "new": new_data[key][inner_key],
  22. }
  23. if inner_combined:
  24. combined_data[key] = inner_combined
  25. dest = TextIOWrapper(out, encoding="utf-8", newline="")
  26. json.dump(combined_data, dest, indent=2)
  27. dest.detach()
  28. if __name__ == "__main__":
  29. combine_json()