blob: f41bfb08c58eeb3283aad308d3d8bb44a0a368e1 [file] [log] [blame]
Ole Troan9d420872017-10-12 13:06:35 +02001# JSON generation
2import json
3
Ole Troan148c7b72020-10-07 18:05:37 +02004
Ondrej Fabryeec5d482020-02-03 23:25:23 +01005def walk_imports(s):
6 r = []
7 for e in s:
8 r.append(str(e))
9 return r
10
Ole Troan9d420872017-10-12 13:06:35 +020011
Ole Troan148c7b72020-10-07 18:05:37 +020012def walk_counters(s, pathset):
13 r = []
14 for e in s:
15 r2 = {'name': e.name, 'elements': e.block}
16 r.append(r2)
17
18 r3 = []
19 for p in pathset:
20 r3.append(p.paths)
21
22 return r, r3
23
24
Ole Troan9d420872017-10-12 13:06:35 +020025def walk_enums(s):
26 r = []
27 for e in s:
28 d = []
29 d.append(e.name)
30 for b in e.block:
31 d.append(b)
32 d.append({'enumtype': e.enumtype})
33 r.append(d)
34 return r
35
36
37def walk_services(s):
Marek Gradzkid05b9262018-02-09 13:39:22 +010038 r = {}
Ole Troan9d420872017-10-12 13:06:35 +020039 for e in s:
40 d = {'reply': e.reply}
41 if e.stream:
42 d['stream'] = True
Ole Troanf5db3712020-05-20 15:47:06 +020043 if e.stream_message:
44 d['stream_msg'] = e.stream_message
Ole Troan9d420872017-10-12 13:06:35 +020045 if e.events:
46 d['events'] = e.events
Marek Gradzkid05b9262018-02-09 13:39:22 +010047 r[e.caller] = d
Ole Troan9d420872017-10-12 13:06:35 +020048 return r
49
50
Ole Troan33a58172019-09-04 09:12:29 +020051def walk_defs(s, is_message=False):
Ole Troan9d420872017-10-12 13:06:35 +020052 r = []
53 for t in s:
54 d = []
55 d.append(t.name)
56 for b in t.block:
Ole Troan33a58172019-09-04 09:12:29 +020057 if b.type == 'Option':
58 continue
Ole Troan9d420872017-10-12 13:06:35 +020059 if b.type == 'Field':
Ole Troan9ac11382019-04-23 17:11:01 +020060 if b.limit:
61 d.append([b.fieldtype, b.fieldname, b.limit])
62 else:
63 d.append([b.fieldtype, b.fieldname])
Ole Troan9d420872017-10-12 13:06:35 +020064 elif b.type == 'Array':
65 if b.lengthfield:
Ole Troan33a58172019-09-04 09:12:29 +020066 d.append([b.fieldtype, b.fieldname,
67 b.length, b.lengthfield])
Ole Troan9d420872017-10-12 13:06:35 +020068 else:
Ole Troan413f4a52018-11-28 11:36:05 +010069 d.append([b.fieldtype, b.fieldname, b.length])
Ole Troan2c2feab2018-04-24 00:02:37 -040070 elif b.type == 'Union':
Ole Troan413f4a52018-11-28 11:36:05 +010071 pass
Ole Troan9d420872017-10-12 13:06:35 +020072 else:
73 raise ValueError("Error in processing array type %s" % b)
Ole Troan413f4a52018-11-28 11:36:05 +010074
Ole Troan8dbfb432019-04-24 14:31:18 +020075 if is_message and t.crc:
Ole Troan9d420872017-10-12 13:06:35 +020076 c = {}
77 c['crc'] = "{0:#0{1}x}".format(t.crc, 10)
78 d.append(c)
79
80 r.append(d)
81 return r
82
Ole Troan148c7b72020-10-07 18:05:37 +020083
Ole Troan9d420872017-10-12 13:06:35 +020084#
85# Plugin entry point
86#
Ole Troan2a1ca782019-09-19 01:08:30 +020087def run(args, filename, s):
Ole Troan9d420872017-10-12 13:06:35 +020088 j = {}
89
Ole Troan33a58172019-09-04 09:12:29 +020090 j['types'] = (walk_defs([o for o in s['types']
91 if o.__class__.__name__ == 'Typedef']))
Ole Troan8dbfb432019-04-24 14:31:18 +020092 j['messages'] = walk_defs(s['Define'], True)
Ole Troan33a58172019-09-04 09:12:29 +020093 j['unions'] = (walk_defs([o for o in s['types']
94 if o.__class__.__name__ == 'Union']))
95 j['enums'] = (walk_enums([o for o in s['types']
96 if o.__class__.__name__ == 'Enum']))
Ole Troan2c2feab2018-04-24 00:02:37 -040097 j['services'] = walk_services(s['Service'])
Ole Troanca80dcc2019-02-26 18:03:16 +010098 j['options'] = s['Option']
Ole Troan75761b92019-09-11 17:49:08 +020099 j['aliases'] = {o.name:o.alias for o in s['types'] if o.__class__.__name__ == 'Using'}
Ole Troan8dbfb432019-04-24 14:31:18 +0200100 j['vl_api_version'] = hex(s['file_crc'])
Ondrej Fabryeec5d482020-02-03 23:25:23 +0100101 j['imports'] = walk_imports(i for i in s['Import'])
Ole Troan148c7b72020-10-07 18:05:37 +0200102 j['counters'], j['paths'] = walk_counters(s['Counters'], s['Paths'])
Ole Troan9d420872017-10-12 13:06:35 +0200103 return json.dumps(j, indent=4, separators=(',', ': '))