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