data.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #
  2. # SPDX-License-Identifier: GPL-2.0-only
  3. #
  4. import json
  5. import oe.maketype
  6. def typed_value(key, d):
  7. """Construct a value for the specified metadata variable, using its flags
  8. to determine the type and parameters for construction."""
  9. var_type = d.getVarFlag(key, 'type')
  10. flags = d.getVarFlags(key)
  11. if flags is not None:
  12. flags = dict((flag, d.expand(value))
  13. for flag, value in list(flags.items()))
  14. else:
  15. flags = {}
  16. try:
  17. return oe.maketype.create(d.getVar(key) or '', var_type, **flags)
  18. except (TypeError, ValueError) as exc:
  19. bb.msg.fatal("Data", "%s: %s" % (key, str(exc)))
  20. def export2json(d, json_file, expand=True, searchString="",replaceString=""):
  21. data2export = {}
  22. keys2export = []
  23. for key in d.keys():
  24. if key.startswith("_"):
  25. continue
  26. elif key.startswith("BB"):
  27. continue
  28. elif key.startswith("B_pn"):
  29. continue
  30. elif key.startswith("do_"):
  31. continue
  32. elif d.getVarFlag(key, "func"):
  33. continue
  34. keys2export.append(key)
  35. for key in keys2export:
  36. try:
  37. data2export[key] = d.getVar(key, expand).replace(searchString,replaceString)
  38. except bb.data_smart.ExpansionError:
  39. data2export[key] = ''
  40. except AttributeError:
  41. pass
  42. with open(json_file, "w") as f:
  43. json.dump(data2export, f, skipkeys=True, indent=4, sort_keys=True)