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