blob: 35dcbcafbbdb82a6302db81161f0724d5294e326 [file] [log] [blame]
Ole Troan9d420872017-10-12 13:06:35 +02001# JSON generation
2import json
3
Ondrej Fabryeec5d482020-02-03 23:25:23 +01004def walk_imports(s):
5 r = []
6 for e in s:
7 r.append(str(e))
8 return r
9
Ole Troan9d420872017-10-12 13:06:35 +020010
11def walk_enums(s):
12 r = []
13 for e in s:
14 d = []
15 d.append(e.name)
16 for b in e.block:
17 d.append(b)
18 d.append({'enumtype': e.enumtype})
19 r.append(d)
20 return r
21
22
23def walk_services(s):
Marek Gradzkid05b9262018-02-09 13:39:22 +010024 r = {}
Ole Troan9d420872017-10-12 13:06:35 +020025 for e in s:
26 d = {'reply': e.reply}
27 if e.stream:
28 d['stream'] = True
29 if e.events:
30 d['events'] = e.events
Marek Gradzkid05b9262018-02-09 13:39:22 +010031 r[e.caller] = d
Ole Troan9d420872017-10-12 13:06:35 +020032 return r
33
34
Ole Troan33a58172019-09-04 09:12:29 +020035def walk_defs(s, is_message=False):
Ole Troan9d420872017-10-12 13:06:35 +020036 r = []
37 for t in s:
38 d = []
39 d.append(t.name)
40 for b in t.block:
Ole Troan33a58172019-09-04 09:12:29 +020041 if b.type == 'Option':
42 continue
Ole Troan9d420872017-10-12 13:06:35 +020043 if b.type == 'Field':
Ole Troan9ac11382019-04-23 17:11:01 +020044 if b.limit:
45 d.append([b.fieldtype, b.fieldname, b.limit])
46 else:
47 d.append([b.fieldtype, b.fieldname])
Ole Troan9d420872017-10-12 13:06:35 +020048 elif b.type == 'Array':
49 if b.lengthfield:
Ole Troan33a58172019-09-04 09:12:29 +020050 d.append([b.fieldtype, b.fieldname,
51 b.length, b.lengthfield])
Ole Troan9d420872017-10-12 13:06:35 +020052 else:
Ole Troan413f4a52018-11-28 11:36:05 +010053 d.append([b.fieldtype, b.fieldname, b.length])
Ole Troan2c2feab2018-04-24 00:02:37 -040054 elif b.type == 'Union':
Ole Troan413f4a52018-11-28 11:36:05 +010055 pass
Ole Troan9d420872017-10-12 13:06:35 +020056 else:
57 raise ValueError("Error in processing array type %s" % b)
Ole Troan413f4a52018-11-28 11:36:05 +010058
Ole Troan8dbfb432019-04-24 14:31:18 +020059 if is_message and t.crc:
Ole Troan9d420872017-10-12 13:06:35 +020060 c = {}
61 c['crc'] = "{0:#0{1}x}".format(t.crc, 10)
62 d.append(c)
63
64 r.append(d)
65 return r
66
Ole Troan9d420872017-10-12 13:06:35 +020067#
68# Plugin entry point
69#
Ole Troan2a1ca782019-09-19 01:08:30 +020070def run(args, filename, s):
Ole Troan9d420872017-10-12 13:06:35 +020071 j = {}
72
Ole Troan33a58172019-09-04 09:12:29 +020073 j['types'] = (walk_defs([o for o in s['types']
74 if o.__class__.__name__ == 'Typedef']))
Ole Troan8dbfb432019-04-24 14:31:18 +020075 j['messages'] = walk_defs(s['Define'], True)
Ole Troan33a58172019-09-04 09:12:29 +020076 j['unions'] = (walk_defs([o for o in s['types']
77 if o.__class__.__name__ == 'Union']))
78 j['enums'] = (walk_enums([o for o in s['types']
79 if o.__class__.__name__ == 'Enum']))
Ole Troan2c2feab2018-04-24 00:02:37 -040080 j['services'] = walk_services(s['Service'])
Ole Troanca80dcc2019-02-26 18:03:16 +010081 j['options'] = s['Option']
Ole Troan75761b92019-09-11 17:49:08 +020082 j['aliases'] = {o.name:o.alias for o in s['types'] if o.__class__.__name__ == 'Using'}
Ole Troan8dbfb432019-04-24 14:31:18 +020083 j['vl_api_version'] = hex(s['file_crc'])
Ondrej Fabryeec5d482020-02-03 23:25:23 +010084 j['imports'] = walk_imports(i for i in s['Import'])
Ole Troan9d420872017-10-12 13:06:35 +020085 return json.dumps(j, indent=4, separators=(',', ': '))