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