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