blob: 6e7aaa2e6f53c4f58c0af1ec903ea89d8498334b [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
Ole Troanf5db3712020-05-20 15:47:06 +020029 if e.stream_message:
30 d['stream_msg'] = e.stream_message
Ole Troan9d420872017-10-12 13:06:35 +020031 if e.events:
32 d['events'] = e.events
Marek Gradzkid05b9262018-02-09 13:39:22 +010033 r[e.caller] = d
Ole Troan9d420872017-10-12 13:06:35 +020034 return r
35
36
Ole Troan33a58172019-09-04 09:12:29 +020037def walk_defs(s, is_message=False):
Ole Troan9d420872017-10-12 13:06:35 +020038 r = []
39 for t in s:
40 d = []
41 d.append(t.name)
42 for b in t.block:
Ole Troan33a58172019-09-04 09:12:29 +020043 if b.type == 'Option':
44 continue
Ole Troan9d420872017-10-12 13:06:35 +020045 if b.type == 'Field':
Ole Troan9ac11382019-04-23 17:11:01 +020046 if b.limit:
47 d.append([b.fieldtype, b.fieldname, b.limit])
48 else:
49 d.append([b.fieldtype, b.fieldname])
Ole Troan9d420872017-10-12 13:06:35 +020050 elif b.type == 'Array':
51 if b.lengthfield:
Ole Troan33a58172019-09-04 09:12:29 +020052 d.append([b.fieldtype, b.fieldname,
53 b.length, b.lengthfield])
Ole Troan9d420872017-10-12 13:06:35 +020054 else:
Ole Troan413f4a52018-11-28 11:36:05 +010055 d.append([b.fieldtype, b.fieldname, b.length])
Ole Troan2c2feab2018-04-24 00:02:37 -040056 elif b.type == 'Union':
Ole Troan413f4a52018-11-28 11:36:05 +010057 pass
Ole Troan9d420872017-10-12 13:06:35 +020058 else:
59 raise ValueError("Error in processing array type %s" % b)
Ole Troan413f4a52018-11-28 11:36:05 +010060
Ole Troan8dbfb432019-04-24 14:31:18 +020061 if is_message and t.crc:
Ole Troan9d420872017-10-12 13:06:35 +020062 c = {}
63 c['crc'] = "{0:#0{1}x}".format(t.crc, 10)
64 d.append(c)
65
66 r.append(d)
67 return r
68
Ole Troan9d420872017-10-12 13:06:35 +020069#
70# Plugin entry point
71#
Ole Troan2a1ca782019-09-19 01:08:30 +020072def run(args, filename, s):
Ole Troan9d420872017-10-12 13:06:35 +020073 j = {}
74
Ole Troan33a58172019-09-04 09:12:29 +020075 j['types'] = (walk_defs([o for o in s['types']
76 if o.__class__.__name__ == 'Typedef']))
Ole Troan8dbfb432019-04-24 14:31:18 +020077 j['messages'] = walk_defs(s['Define'], True)
Ole Troan33a58172019-09-04 09:12:29 +020078 j['unions'] = (walk_defs([o for o in s['types']
79 if o.__class__.__name__ == 'Union']))
80 j['enums'] = (walk_enums([o for o in s['types']
81 if o.__class__.__name__ == 'Enum']))
Ole Troan2c2feab2018-04-24 00:02:37 -040082 j['services'] = walk_services(s['Service'])
Ole Troanca80dcc2019-02-26 18:03:16 +010083 j['options'] = s['Option']
Ole Troan75761b92019-09-11 17:49:08 +020084 j['aliases'] = {o.name:o.alias for o in s['types'] if o.__class__.__name__ == 'Using'}
Ole Troan8dbfb432019-04-24 14:31:18 +020085 j['vl_api_version'] = hex(s['file_crc'])
Ondrej Fabryeec5d482020-02-03 23:25:23 +010086 j['imports'] = walk_imports(i for i in s['Import'])
Ole Troan9d420872017-10-12 13:06:35 +020087 return json.dumps(j, indent=4, separators=(',', ': '))