Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1 | # JSON generation |
| 2 | import json |
| 3 | |
Paul Vinciguerra | 9046e44 | 2020-11-20 23:10:09 -0500 | [diff] [blame] | 4 | process_imports = True |
| 5 | |
Ole Troan | 148c7b7 | 2020-10-07 18:05:37 +0200 | [diff] [blame] | 6 | |
Ondrej Fabry | eec5d48 | 2020-02-03 23:25:23 +0100 | [diff] [blame] | 7 | def walk_imports(s): |
| 8 | r = [] |
| 9 | for e in s: |
| 10 | r.append(str(e)) |
| 11 | return r |
| 12 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 13 | |
Ole Troan | 148c7b7 | 2020-10-07 18:05:37 +0200 | [diff] [blame] | 14 | def walk_counters(s, pathset): |
| 15 | r = [] |
| 16 | for e in s: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 17 | r2 = {"name": e.name, "elements": e.block} |
Ole Troan | 148c7b7 | 2020-10-07 18:05:37 +0200 | [diff] [blame] | 18 | r.append(r2) |
| 19 | |
| 20 | r3 = [] |
| 21 | for p in pathset: |
| 22 | r3.append(p.paths) |
| 23 | |
| 24 | return r, r3 |
| 25 | |
| 26 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 27 | def 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 34 | d.append({"enumtype": e.enumtype}) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 35 | r.append(d) |
| 36 | return r |
| 37 | |
| 38 | |
| 39 | def walk_services(s): |
Marek Gradzki | d05b926 | 2018-02-09 13:39:22 +0100 | [diff] [blame] | 40 | r = {} |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 41 | for e in s: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 42 | d = {"reply": e.reply} |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 43 | if e.stream: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 44 | d["stream"] = True |
Ole Troan | f5db371 | 2020-05-20 15:47:06 +0200 | [diff] [blame] | 45 | if e.stream_message: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 46 | d["stream_msg"] = e.stream_message |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 47 | if e.events: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 48 | d["events"] = e.events |
Marek Gradzki | d05b926 | 2018-02-09 13:39:22 +0100 | [diff] [blame] | 49 | r[e.caller] = d |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 50 | return r |
| 51 | |
| 52 | |
Ole Troan | 33a5817 | 2019-09-04 09:12:29 +0200 | [diff] [blame] | 53 | def walk_defs(s, is_message=False): |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 54 | r = [] |
| 55 | for t in s: |
| 56 | d = [] |
| 57 | d.append(t.name) |
| 58 | for b in t.block: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 59 | if b.type == "Option": |
Ole Troan | 33a5817 | 2019-09-04 09:12:29 +0200 | [diff] [blame] | 60 | continue |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 61 | if b.type == "Field": |
Ole Troan | 9ac1138 | 2019-04-23 17:11:01 +0200 | [diff] [blame] | 62 | if b.limit: |
| 63 | d.append([b.fieldtype, b.fieldname, b.limit]) |
| 64 | else: |
| 65 | d.append([b.fieldtype, b.fieldname]) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 66 | elif b.type == "Array": |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 67 | if b.lengthfield: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 68 | d.append([b.fieldtype, b.fieldname, b.length, b.lengthfield]) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 69 | else: |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame] | 70 | d.append([b.fieldtype, b.fieldname, b.length]) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 71 | elif b.type == "Union": |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame] | 72 | pass |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 73 | else: |
| 74 | raise ValueError("Error in processing array type %s" % b) |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame] | 75 | |
Ole Troan | 8dbfb43 | 2019-04-24 14:31:18 +0200 | [diff] [blame] | 76 | if is_message and t.crc: |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 77 | c = {} |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 78 | c["crc"] = "{0:#0{1}x}".format(t.crc, 10) |
| 79 | c["options"] = t.options |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 80 | d.append(c) |
| 81 | |
| 82 | r.append(d) |
| 83 | return r |
| 84 | |
Ole Troan | 148c7b7 | 2020-10-07 18:05:37 +0200 | [diff] [blame] | 85 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 86 | # |
| 87 | # Plugin entry point |
| 88 | # |
Nathan Skrzypczak | 1b299fa | 2022-06-16 17:00:02 +0200 | [diff] [blame^] | 89 | def run(output_dir, filename, s): |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 90 | j = {} |
| 91 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 92 | 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=(",", ": ")) |