summaryrefslogtreecommitdiffstats
path: root/meta/lib/oe/data.py
blob: 80bba2b9d2451954dd11999593e152fbcd2566ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import json
import oe.maketype

def typed_value(key, d):
    """Construct a value for the specified metadata variable, using its flags
    to determine the type and parameters for construction."""
    var_type = d.getVarFlag(key, 'type')
    flags = d.getVarFlags(key)
    if flags is not None:
        flags = dict((flag, d.expand(value))
                     for flag, value in list(flags.items()))
    else:
        flags = {}

    try:
        return oe.maketype.create(d.getVar(key) or '', var_type, **flags)
    except (TypeError, ValueError) as exc:
        bb.msg.fatal("Data", "%s: %s" % (key, str(exc)))

def export2json(d, json_file, expand=True):
    data2export = {}
    keys2export = []

    for key in d.keys():
        if key.startswith("_"):
            continue
        elif key.startswith("BB"):
            continue
        elif key.startswith("B_pn"):
            continue
        elif key.startswith("do_"):
            continue
        elif d.getVarFlag(key, "func"):
            continue

        keys2export.append(key)

    for key in keys2export:
        try:
            data2export[key] = d.getVar(key, expand)
        except bb.data_smart.ExpansionError:
            data2export[key] = ''

    with open(json_file, "w") as f:
        json.dump(data2export, f, skipkeys=True, indent=4, sort_keys=True)