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