Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1 | # JSON generation |
| 2 | import json |
| 3 | |
Paul Vinciguerra | 9046e44 | 2020-11-20 23:10:09 -0500 | [diff] [blame^] | 4 | process_imports = True |
| 5 | |
Ole Troan | 148c7b7 | 2020-10-07 18:05:37 +0200 | [diff] [blame] | 6 | |
Ondrej Fabry | eec5d48 | 2020-02-03 23:25:23 +0100 | [diff] [blame] | 7 | def walk_imports(s): |
| 8 | r = [] |
| 9 | for e in s: |
| 10 | r.append(str(e)) |
| 11 | return r |
| 12 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 13 | |
Ole Troan | 148c7b7 | 2020-10-07 18:05:37 +0200 | [diff] [blame] | 14 | def 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 Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 27 | def 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 | |
| 39 | def walk_services(s): |
Marek Gradzki | d05b926 | 2018-02-09 13:39:22 +0100 | [diff] [blame] | 40 | r = {} |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 41 | for e in s: |
| 42 | d = {'reply': e.reply} |
| 43 | if e.stream: |
| 44 | d['stream'] = True |
Ole Troan | f5db371 | 2020-05-20 15:47:06 +0200 | [diff] [blame] | 45 | if e.stream_message: |
| 46 | d['stream_msg'] = e.stream_message |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 47 | if e.events: |
| 48 | d['events'] = e.events |
Marek Gradzki | d05b926 | 2018-02-09 13:39:22 +0100 | [diff] [blame] | 49 | r[e.caller] = d |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 50 | return r |
| 51 | |
| 52 | |
Ole Troan | 33a5817 | 2019-09-04 09:12:29 +0200 | [diff] [blame] | 53 | def walk_defs(s, is_message=False): |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 54 | r = [] |
| 55 | for t in s: |
| 56 | d = [] |
| 57 | d.append(t.name) |
| 58 | for b in t.block: |
Ole Troan | 33a5817 | 2019-09-04 09:12:29 +0200 | [diff] [blame] | 59 | if b.type == 'Option': |
| 60 | continue |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 61 | if b.type == 'Field': |
Ole Troan | 9ac1138 | 2019-04-23 17:11:01 +0200 | [diff] [blame] | 62 | if b.limit: |
| 63 | d.append([b.fieldtype, b.fieldname, b.limit]) |
| 64 | else: |
| 65 | d.append([b.fieldtype, b.fieldname]) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 66 | elif b.type == 'Array': |
| 67 | if b.lengthfield: |
Ole Troan | 33a5817 | 2019-09-04 09:12:29 +0200 | [diff] [blame] | 68 | d.append([b.fieldtype, b.fieldname, |
| 69 | b.length, b.lengthfield]) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 70 | else: |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame] | 71 | d.append([b.fieldtype, b.fieldname, b.length]) |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 72 | elif b.type == 'Union': |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame] | 73 | pass |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 74 | else: |
| 75 | raise ValueError("Error in processing array type %s" % b) |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame] | 76 | |
Ole Troan | 8dbfb43 | 2019-04-24 14:31:18 +0200 | [diff] [blame] | 77 | if is_message and t.crc: |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 78 | 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 Troan | 148c7b7 | 2020-10-07 18:05:37 +0200 | [diff] [blame] | 85 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 86 | # |
| 87 | # Plugin entry point |
| 88 | # |
Ole Troan | 2a1ca78 | 2019-09-19 01:08:30 +0200 | [diff] [blame] | 89 | def run(args, filename, s): |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 90 | j = {} |
| 91 | |
Ole Troan | 33a5817 | 2019-09-04 09:12:29 +0200 | [diff] [blame] | 92 | j['types'] = (walk_defs([o for o in s['types'] |
| 93 | if o.__class__.__name__ == 'Typedef'])) |
Ole Troan | 8dbfb43 | 2019-04-24 14:31:18 +0200 | [diff] [blame] | 94 | j['messages'] = walk_defs(s['Define'], True) |
Ole Troan | 33a5817 | 2019-09-04 09:12:29 +0200 | [diff] [blame] | 95 | 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 Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 99 | j['services'] = walk_services(s['Service']) |
Ole Troan | ca80dcc | 2019-02-26 18:03:16 +0100 | [diff] [blame] | 100 | j['options'] = s['Option'] |
Ole Troan | 75761b9 | 2019-09-11 17:49:08 +0200 | [diff] [blame] | 101 | j['aliases'] = {o.name:o.alias for o in s['types'] if o.__class__.__name__ == 'Using'} |
Ole Troan | 8dbfb43 | 2019-04-24 14:31:18 +0200 | [diff] [blame] | 102 | j['vl_api_version'] = hex(s['file_crc']) |
Ondrej Fabry | eec5d48 | 2020-02-03 23:25:23 +0100 | [diff] [blame] | 103 | j['imports'] = walk_imports(i for i in s['Import']) |
Ole Troan | 148c7b7 | 2020-10-07 18:05:37 +0200 | [diff] [blame] | 104 | j['counters'], j['paths'] = walk_counters(s['Counters'], s['Paths']) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 105 | return json.dumps(j, indent=4, separators=(',', ': ')) |