blob: c563a0899490176ba679e0ca6d507d39db30a48f [file] [log] [blame]
Ole Troan9d420872017-10-12 13:06:35 +02001# JSON generation
2import json
3
4
5def walk_enums(s):
6 r = []
7 for e in s:
8 d = []
9 d.append(e.name)
10 for b in e.block:
11 d.append(b)
12 d.append({'enumtype': e.enumtype})
13 r.append(d)
14 return r
15
16
17def walk_services(s):
Marek Gradzkid05b9262018-02-09 13:39:22 +010018 r = {}
Ole Troan9d420872017-10-12 13:06:35 +020019 for e in s:
20 d = {'reply': e.reply}
21 if e.stream:
22 d['stream'] = True
23 if e.events:
24 d['events'] = e.events
Marek Gradzkid05b9262018-02-09 13:39:22 +010025 r[e.caller] = d
Ole Troan9d420872017-10-12 13:06:35 +020026 return r
27
28
29def walk_defs(s):
30 r = []
31 for t in s:
32 d = []
33 d.append(t.name)
34 for b in t.block:
Ole Troan9d420872017-10-12 13:06:35 +020035 if b.type == 'Field':
Ole Troan413f4a52018-11-28 11:36:05 +010036 d.append([b.fieldtype, b.fieldname])
Ole Troan9d420872017-10-12 13:06:35 +020037 elif b.type == 'Array':
38 if b.lengthfield:
Ole Troan413f4a52018-11-28 11:36:05 +010039 d.append([b.fieldtype, b.fieldname, b.length, b.lengthfield])
Ole Troan9d420872017-10-12 13:06:35 +020040 else:
Ole Troan413f4a52018-11-28 11:36:05 +010041 d.append([b.fieldtype, b.fieldname, b.length])
Ole Troan2c2feab2018-04-24 00:02:37 -040042 elif b.type == 'Union':
Ole Troan413f4a52018-11-28 11:36:05 +010043 pass
Ole Troan9d420872017-10-12 13:06:35 +020044 else:
45 raise ValueError("Error in processing array type %s" % b)
Ole Troan413f4a52018-11-28 11:36:05 +010046
Ole Troan9d420872017-10-12 13:06:35 +020047 if t.crc:
48 c = {}
49 c['crc'] = "{0:#0{1}x}".format(t.crc, 10)
50 d.append(c)
51
52 r.append(d)
53 return r
54
55
56#
57# Plugin entry point
58#
59def run(filename, s, file_crc):
60 j = {}
61
Ole Troan2c2feab2018-04-24 00:02:37 -040062 j['types'] = walk_defs([o for o in s['types'] if o.__class__.__name__ == 'Typedef'])
63 j['messages'] = walk_defs(s['Define'])
64 j['unions'] = walk_defs([o for o in s['types'] if o.__class__.__name__ == 'Union'])
65 j['enums'] = walk_enums([o for o in s['types'] if o.__class__.__name__ == 'Enum'])
66 j['services'] = walk_services(s['Service'])
Ole Troanca80dcc2019-02-26 18:03:16 +010067 j['options'] = s['Option']
Ole Troan53fffa12018-11-13 12:36:56 +010068 j['aliases'] = s['Alias']
Ole Troan9d420872017-10-12 13:06:35 +020069 j['vl_api_version'] = hex(file_crc)
70 return json.dumps(j, indent=4, separators=(',', ': '))