blob: 124c0d3a0bdaf9229f333bbaed9f3030a46f062d [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
Ole Troan8dbfb432019-04-24 14:31:18 +020029def walk_defs(s, is_message = False):
Ole Troan9d420872017-10-12 13:06:35 +020030 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 Troan9ac11382019-04-23 17:11:01 +020036 if b.limit:
37 d.append([b.fieldtype, b.fieldname, b.limit])
38 else:
39 d.append([b.fieldtype, b.fieldname])
Ole Troan9d420872017-10-12 13:06:35 +020040 elif b.type == 'Array':
41 if b.lengthfield:
Ole Troan413f4a52018-11-28 11:36:05 +010042 d.append([b.fieldtype, b.fieldname, b.length, b.lengthfield])
Ole Troan9d420872017-10-12 13:06:35 +020043 else:
Ole Troan413f4a52018-11-28 11:36:05 +010044 d.append([b.fieldtype, b.fieldname, b.length])
Ole Troan2c2feab2018-04-24 00:02:37 -040045 elif b.type == 'Union':
Ole Troan413f4a52018-11-28 11:36:05 +010046 pass
Ole Troan9d420872017-10-12 13:06:35 +020047 else:
48 raise ValueError("Error in processing array type %s" % b)
Ole Troan413f4a52018-11-28 11:36:05 +010049
Ole Troan8dbfb432019-04-24 14:31:18 +020050 if is_message and t.crc:
Ole Troan9d420872017-10-12 13:06:35 +020051 c = {}
52 c['crc'] = "{0:#0{1}x}".format(t.crc, 10)
53 d.append(c)
54
55 r.append(d)
56 return r
57
58
59#
60# Plugin entry point
61#
Ole Troan8dbfb432019-04-24 14:31:18 +020062def run(filename, s):
Ole Troan9d420872017-10-12 13:06:35 +020063 j = {}
64
Ole Troan2c2feab2018-04-24 00:02:37 -040065 j['types'] = walk_defs([o for o in s['types'] if o.__class__.__name__ == 'Typedef'])
Ole Troan8dbfb432019-04-24 14:31:18 +020066 j['messages'] = walk_defs(s['Define'], True)
Ole Troan2c2feab2018-04-24 00:02:37 -040067 j['unions'] = walk_defs([o for o in s['types'] if o.__class__.__name__ == 'Union'])
68 j['enums'] = walk_enums([o for o in s['types'] if o.__class__.__name__ == 'Enum'])
69 j['services'] = walk_services(s['Service'])
Ole Troanca80dcc2019-02-26 18:03:16 +010070 j['options'] = s['Option']
Ole Troan53fffa12018-11-13 12:36:56 +010071 j['aliases'] = s['Alias']
Ole Troan8dbfb432019-04-24 14:31:18 +020072 j['vl_api_version'] = hex(s['file_crc'])
Ole Troan9d420872017-10-12 13:06:35 +020073 return json.dumps(j, indent=4, separators=(',', ': '))