api: add new stream message convention
[vpp.git] / src / tools / vppapigen / vppapigen_json.py
1 # JSON generation
2 import json
3
4 def walk_imports(s):
5     r = []
6     for e in s:
7         r.append(str(e))
8     return r
9
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):
24     r = {}
25     for e in s:
26         d = {'reply': e.reply}
27         if e.stream:
28             d['stream'] = True
29         if e.stream_message:
30             d['stream_msg'] = e.stream_message
31         if e.events:
32             d['events'] = e.events
33         r[e.caller] = d
34     return r
35
36
37 def walk_defs(s, is_message=False):
38     r = []
39     for t in s:
40         d = []
41         d.append(t.name)
42         for b in t.block:
43             if b.type == 'Option':
44                 continue
45             if b.type == 'Field':
46                 if b.limit:
47                     d.append([b.fieldtype, b.fieldname, b.limit])
48                 else:
49                     d.append([b.fieldtype, b.fieldname])
50             elif b.type == 'Array':
51                 if b.lengthfield:
52                     d.append([b.fieldtype, b.fieldname,
53                               b.length, b.lengthfield])
54                 else:
55                     d.append([b.fieldtype, b.fieldname, b.length])
56             elif b.type == 'Union':
57                 pass
58             else:
59                 raise ValueError("Error in processing array type %s" % b)
60
61         if is_message and t.crc:
62             c = {}
63             c['crc'] = "{0:#0{1}x}".format(t.crc, 10)
64             d.append(c)
65
66         r.append(d)
67     return r
68
69 #
70 # Plugin entry point
71 #
72 def run(args, filename, s):
73     j = {}
74
75     j['types'] = (walk_defs([o for o in s['types']
76                              if o.__class__.__name__ == 'Typedef']))
77     j['messages'] = walk_defs(s['Define'], True)
78     j['unions'] = (walk_defs([o for o in s['types']
79                               if o.__class__.__name__ == 'Union']))
80     j['enums'] = (walk_enums([o for o in s['types']
81                               if o.__class__.__name__ == 'Enum']))
82     j['services'] = walk_services(s['Service'])
83     j['options'] = s['Option']
84     j['aliases'] = {o.name:o.alias for o in s['types'] if o.__class__.__name__ == 'Using'}
85     j['vl_api_version'] = hex(s['file_crc'])
86     j['imports'] = walk_imports(i for i in s['Import'])
87     return json.dumps(j, indent=4, separators=(',', ': '))