datamodel_to_constants.py 997 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #!/usr/bin/env python3
  2. """
  3. Auxiliary script to generate constant definitiosn from a JSON FILE
  4. """
  5. import json
  6. import sys
  7. def process(json_object, keys, values):
  8. if type(json_object) == dict:
  9. for key, value in json_object.items():
  10. keys.append(key)
  11. process(value, keys, values)
  12. elif type(json_object) == list:
  13. for v in json_object:
  14. process(v, keys, values)
  15. else:
  16. values.append(str(json_object))
  17. def print_as_const(lst, prefix):
  18. for s in lst:
  19. if not s.isidentifier():
  20. continue
  21. print("const char* const " + prefix + s.upper() + " = \"" + s + "\";")
  22. if __name__ == '__main__':
  23. with open(sys.argv[1], "r") as f:
  24. json_object = json.load(f)
  25. # using list so that we get related keys and values close to each other
  26. keys = []
  27. values = []
  28. process(json_object, keys, values)
  29. print_as_const(keys, "KEY_")
  30. print_as_const(values, "VALUE_")