blob: 19f7d65b8c58505ef63d7e630c142fec3a22a7b4 [file] [log] [blame]
Ole Troan9d420872017-10-12 13:06:35 +02001# JSON generation
2import json
3
Paul Vinciguerra9046e442020-11-20 23:10:09 -05004process_imports = True
5
Ole Troan148c7b72020-10-07 18:05:37 +02006
Ondrej Fabryeec5d482020-02-03 23:25:23 +01007def walk_imports(s):
8 r = []
9 for e in s:
10 r.append(str(e))
11 return r
12
Ole Troan9d420872017-10-12 13:06:35 +020013
Ole Troan148c7b72020-10-07 18:05:37 +020014def walk_counters(s, pathset):
15 r = []
16 for e in s:
17 r2 = {'name': e.name, 'elements': e.block}
18 r.append(r2)
19
20 r3 = []
21 for p in pathset:
22 r3.append(p.paths)
23
24 return r, r3
25
26
Ole Troan9d420872017-10-12 13:06:35 +020027def walk_enums(s):
28 r = []
29 for e in s:
30 d = []
31 d.append(e.name)
32 for b in e.block:
33 d.append(b)
34 d.append({'enumtype': e.enumtype})
35 r.append(d)
36 return r
37
38
39def walk_services(s):
Marek Gradzkid05b9262018-02-09 13:39:22 +010040 r = {}
Ole Troan9d420872017-10-12 13:06:35 +020041 for e in s:
42 d = {'reply': e.reply}
43 if e.stream:
44 d['stream'] = True
Ole Troanf5db3712020-05-20 15:47:06 +020045 if e.stream_message:
46 d['stream_msg'] = e.stream_message
Ole Troan9d420872017-10-12 13:06:35 +020047 if e.events:
48 d['events'] = e.events
Marek Gradzkid05b9262018-02-09 13:39:22 +010049 r[e.caller] = d
Ole Troan9d420872017-10-12 13:06:35 +020050 return r
51
52
Ole Troan33a58172019-09-04 09:12:29 +020053def walk_defs(s, is_message=False):
Ole Troan9d420872017-10-12 13:06:35 +020054 r = []
55 for t in s:
56 d = []
57 d.append(t.name)
58 for b in t.block:
Ole Troan33a58172019-09-04 09:12:29 +020059 if b.type == 'Option':
60 continue
Ole Troan9d420872017-10-12 13:06:35 +020061 if b.type == 'Field':
Ole Troan9ac11382019-04-23 17:11:01 +020062 if b.limit:
63 d.append([b.fieldtype, b.fieldname, b.limit])
64 else:
65 d.append([b.fieldtype, b.fieldname])
Ole Troan9d420872017-10-12 13:06:35 +020066 elif b.type == 'Array':
67 if b.lengthfield:
Ole Troan33a58172019-09-04 09:12:29 +020068 d.append([b.fieldtype, b.fieldname,
69 b.length, b.lengthfield])
Ole Troan9d420872017-10-12 13:06:35 +020070 else:
Ole Troan413f4a52018-11-28 11:36:05 +010071 d.append([b.fieldtype, b.fieldname, b.length])
Ole Troan2c2feab2018-04-24 00:02:37 -040072 elif b.type == 'Union':
Ole Troan413f4a52018-11-28 11:36:05 +010073 pass
Ole Troan9d420872017-10-12 13:06:35 +020074 else:
75 raise ValueError("Error in processing array type %s" % b)
Ole Troan413f4a52018-11-28 11:36:05 +010076
Ole Troan8dbfb432019-04-24 14:31:18 +020077 if is_message and t.crc:
Ole Troan9d420872017-10-12 13:06:35 +020078 c = {}
79 c['crc'] = "{0:#0{1}x}".format(t.crc, 10)
80 d.append(c)
81
82 r.append(d)
83 return r
84
Ole Troan148c7b72020-10-07 18:05:37 +020085
Ole Troan9d420872017-10-12 13:06:35 +020086#
87# Plugin entry point
88#
Ole Troan2a1ca782019-09-19 01:08:30 +020089def run(args, filename, s):
Ole Troan9d420872017-10-12 13:06:35 +020090 j = {}
91
Ole Troan33a58172019-09-04 09:12:29 +020092 j['types'] = (walk_defs([o for o in s['types']
93 if o.__class__.__name__ == 'Typedef']))
Ole Troan8dbfb432019-04-24 14:31:18 +020094 j['messages'] = walk_defs(s['Define'], True)
Ole Troan33a58172019-09-04 09:12:29 +020095 j['unions'] = (walk_defs([o for o in s['types']
96 if o.__class__.__name__ == 'Union']))
97 j['enums'] = (walk_enums([o for o in s['types']
98 if o.__class__.__name__ == 'Enum']))
Ole Troan2c2feab2018-04-24 00:02:37 -040099 j['services'] = walk_services(s['Service'])
Ole Troanca80dcc2019-02-26 18:03:16 +0100100 j['options'] = s['Option']
Ole Troan75761b92019-09-11 17:49:08 +0200101 j['aliases'] = {o.name:o.alias for o in s['types'] if o.__class__.__name__ == 'Using'}
Ole Troan8dbfb432019-04-24 14:31:18 +0200102 j['vl_api_version'] = hex(s['file_crc'])
Ondrej Fabryeec5d482020-02-03 23:25:23 +0100103 j['imports'] = walk_imports(i for i in s['Import'])
Ole Troan148c7b72020-10-07 18:05:37 +0200104 j['counters'], j['paths'] = walk_counters(s['Counters'], s['Paths'])
Ole Troan9d420872017-10-12 13:06:35 +0200105 return json.dumps(j, indent=4, separators=(',', ': '))