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