blob: ba32e284d814dc5364851817de759d03067c8b3d [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 Troan5d234682021-05-05 23:00:58 +020080 c["comment"] = t.comment
Ole Troan9d420872017-10-12 13:06:35 +020081 d.append(c)
82
83 r.append(d)
84 return r
85
Ole Troan148c7b72020-10-07 18:05:37 +020086
Ole Troan9d420872017-10-12 13:06:35 +020087#
88# Plugin entry point
89#
Nathan Skrzypczak1b299fa2022-06-16 17:00:02 +020090def run(output_dir, filename, s):
Ole Troan9d420872017-10-12 13:06:35 +020091 j = {}
92
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020093 j["types"] = walk_defs([o for o in s["types"] if o.__class__.__name__ == "Typedef"])
94 j["messages"] = walk_defs(s["Define"], True)
95 j["unions"] = walk_defs([o for o in s["types"] if o.__class__.__name__ == "Union"])
96 j["enums"] = walk_enums([o for o in s["types"] if o.__class__.__name__ == "Enum"])
97 j["enumflags"] = walk_enums(
98 [o for o in s["types"] if o.__class__.__name__ == "EnumFlag"]
99 )
100 j["services"] = walk_services(s["Service"])
101 j["options"] = s["Option"]
102 j["aliases"] = {
103 o.name: o.alias for o in s["types"] if o.__class__.__name__ == "Using"
104 }
105 j["vl_api_version"] = hex(s["file_crc"])
106 j["imports"] = walk_imports(i for i in s["Import"])
107 j["counters"], j["paths"] = walk_counters(s["Counters"], s["Paths"])
108 return json.dumps(j, indent=4, separators=(",", ": "))