blob: 695b8cc7aa2868c6d0ef0cb0756efc93155b03c3 [file] [log] [blame]
Ole Troan9d420872017-10-12 13:06:35 +02001# JSON generation
2import json
3
Paul Vinciguerra9046e442020-11-20 23:10:09 -05004process_imports = True
5
Ole Troan148c7b72020-10-07 18:05:37 +02006
Ondrej Fabryeec5d482020-02-03 23:25:23 +01007def walk_imports(s):
8 r = []
9 for e in s:
10 r.append(str(e))
11 return r
12
Ole Troan9d420872017-10-12 13:06:35 +020013
Ole Troan148c7b72020-10-07 18:05:37 +020014def walk_counters(s, pathset):
15 r = []
16 for e in s:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020017 r2 = {"name": e.name, "elements": e.block}
Ole Troan148c7b72020-10-07 18:05:37 +020018 r.append(r2)
19
20 r3 = []
21 for p in pathset:
22 r3.append(p.paths)
23
24 return r, r3
25
26
Ole Troan9d420872017-10-12 13:06:35 +020027def walk_enums(s):
28 r = []
29 for e in s:
30 d = []
31 d.append(e.name)
32 for b in e.block:
33 d.append(b)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020034 d.append({"enumtype": e.enumtype})
Ole Troan9d420872017-10-12 13:06:35 +020035 r.append(d)
36 return r
37
38
39def walk_services(s):
Marek Gradzkid05b9262018-02-09 13:39:22 +010040 r = {}
Ole Troan9d420872017-10-12 13:06:35 +020041 for e in s:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020042 d = {"reply": e.reply}
Ole Troan9d420872017-10-12 13:06:35 +020043 if e.stream:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020044 d["stream"] = True
Ole Troanf5db3712020-05-20 15:47:06 +020045 if e.stream_message:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020046 d["stream_msg"] = e.stream_message
Ole Troan9d420872017-10-12 13:06:35 +020047 if e.events:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020048 d["events"] = e.events
Marek Gradzkid05b9262018-02-09 13:39:22 +010049 r[e.caller] = d
Ole Troan9d420872017-10-12 13:06:35 +020050 return r
51
52
Ole Troan33a58172019-09-04 09:12:29 +020053def walk_defs(s, is_message=False):
Ole Troan9d420872017-10-12 13:06:35 +020054 r = []
55 for t in s:
56 d = []
57 d.append(t.name)
58 for b in t.block:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020059 if b.type == "Option":
Ole Troan33a58172019-09-04 09:12:29 +020060 continue
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020061 if b.type == "Field":
Ole Troan9ac11382019-04-23 17:11:01 +020062 if b.limit:
63 d.append([b.fieldtype, b.fieldname, b.limit])
64 else:
65 d.append([b.fieldtype, b.fieldname])
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020066 elif b.type == "Array":
Ole Troan9d420872017-10-12 13:06:35 +020067 if b.lengthfield:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020068 d.append([b.fieldtype, b.fieldname, b.length, b.lengthfield])
Ole Troan9d420872017-10-12 13:06:35 +020069 else:
Ole Troan413f4a52018-11-28 11:36:05 +010070 d.append([b.fieldtype, b.fieldname, b.length])
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020071 elif b.type == "Union":
Ole Troan413f4a52018-11-28 11:36:05 +010072 pass
Ole Troan9d420872017-10-12 13:06:35 +020073 else:
74 raise ValueError("Error in processing array type %s" % b)
Ole Troan413f4a52018-11-28 11:36:05 +010075
Ole Troan8dbfb432019-04-24 14:31:18 +020076 if is_message and t.crc:
Ole Troan9d420872017-10-12 13:06:35 +020077 c = {}
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020078 c["crc"] = "{0:#0{1}x}".format(t.crc, 10)
79 c["options"] = t.options
Ole Troan9d420872017-10-12 13:06:35 +020080 d.append(c)
81
82 r.append(d)
83 return r
84
Ole Troan148c7b72020-10-07 18:05:37 +020085
Ole Troan9d420872017-10-12 13:06:35 +020086#
87# Plugin entry point
88#
Ole Troan2a1ca782019-09-19 01:08:30 +020089def run(args, filename, s):
Ole Troan9d420872017-10-12 13:06:35 +020090 j = {}
91
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020092 j["types"] = walk_defs([o for o in s["types"] if o.__class__.__name__ == "Typedef"])
93 j["messages"] = walk_defs(s["Define"], True)
94 j["unions"] = walk_defs([o for o in s["types"] if o.__class__.__name__ == "Union"])
95 j["enums"] = walk_enums([o for o in s["types"] if o.__class__.__name__ == "Enum"])
96 j["enumflags"] = walk_enums(
97 [o for o in s["types"] if o.__class__.__name__ == "EnumFlag"]
98 )
99 j["services"] = walk_services(s["Service"])
100 j["options"] = s["Option"]
101 j["aliases"] = {
102 o.name: o.alias for o in s["types"] if o.__class__.__name__ == "Using"
103 }
104 j["vl_api_version"] = hex(s["file_crc"])
105 j["imports"] = walk_imports(i for i in s["Import"])
106 j["counters"], j["paths"] = walk_counters(s["Counters"], s["Paths"])
107 return json.dumps(j, indent=4, separators=(",", ": "))