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 | |
| 29 | def walk_defs(s): |
| 30 | r = [] |
| 31 | for t in s: |
| 32 | d = [] |
| 33 | d.append(t.name) |
| 34 | for b in t.block: |
| 35 | f = [] |
| 36 | if b.type == 'Field': |
| 37 | f = [b.fieldtype, b.fieldname] |
| 38 | elif b.type == 'Array': |
| 39 | if b.lengthfield: |
| 40 | f = [b.fieldtype, b.fieldname, b.length, b.lengthfield] |
| 41 | else: |
| 42 | f = [b.fieldtype, b.fieldname, b.length] |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame^] | 43 | elif b.type == 'Union': |
| 44 | print('UNION') |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 45 | else: |
| 46 | raise ValueError("Error in processing array type %s" % b) |
| 47 | d.append(f) |
| 48 | if t.crc: |
| 49 | c = {} |
| 50 | c['crc'] = "{0:#0{1}x}".format(t.crc, 10) |
| 51 | d.append(c) |
| 52 | |
| 53 | r.append(d) |
| 54 | return r |
| 55 | |
| 56 | |
| 57 | # |
| 58 | # Plugin entry point |
| 59 | # |
| 60 | def run(filename, s, file_crc): |
| 61 | j = {} |
| 62 | |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame^] | 63 | j['types'] = walk_defs([o for o in s['types'] if o.__class__.__name__ == 'Typedef']) |
| 64 | j['messages'] = walk_defs(s['Define']) |
| 65 | j['unions'] = walk_defs([o for o in s['types'] if o.__class__.__name__ == 'Union']) |
| 66 | j['enums'] = walk_enums([o for o in s['types'] if o.__class__.__name__ == 'Enum']) |
| 67 | j['services'] = walk_services(s['Service']) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 68 | j['vl_api_version'] = hex(file_crc) |
| 69 | return json.dumps(j, indent=4, separators=(',', ': ')) |