merge-db.py 545 B

1234567891011121314151617181920212223242526272829
  1. from sys import argv
  2. import json
  3. def read(f: str) -> dict[str, dict]:
  4. with open(f) as infile:
  5. next(infile)
  6. return {
  7. (d := json.loads(s))["name"]: d
  8. for line in infile
  9. if (s := line.strip().strip(",")) != "]"
  10. }
  11. _, new, old = argv
  12. db = read(old)
  13. db.update(read(new))
  14. output = [
  15. f" {json.dumps(s)},\n"
  16. for s in sorted(
  17. db.values(),
  18. key=lambda x: (x["num"], x["name"])
  19. )
  20. ]
  21. with open(old, "w") as outfile:
  22. outfile.write("const database = [\n")
  23. outfile.writelines(output)
  24. outfile.write("]\n")