Ole Troan | 1b1ccad | 2019-10-25 18:30:40 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 2 | |
| 3 | import json |
| 4 | |
| 5 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 6 | class ParseError(Exception): |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 7 | pass |
| 8 | |
| 9 | |
| 10 | magic_prefix = "vl_api_" |
| 11 | magic_suffix = "_t" |
| 12 | |
| 13 | |
| 14 | def remove_magic(what): |
| 15 | if what.startswith(magic_prefix) and what.endswith(magic_suffix): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 16 | return what[len(magic_prefix) : -len(magic_suffix)] |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 17 | return what |
| 18 | |
| 19 | |
Klement Sekera | 958b750 | 2017-09-28 06:31:53 +0200 | [diff] [blame] | 20 | class Field(object): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 21 | def __init__(self, field_name, field_type, array_len=None, nelem_field=None): |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 22 | self.name = field_name |
| 23 | self.type = field_type |
| 24 | self.len = array_len |
| 25 | self.nelem_field = nelem_field |
| 26 | |
| 27 | def __str__(self): |
| 28 | if self.len is None: |
Klement Sekera | 34a962b | 2018-09-06 19:31:36 +0200 | [diff] [blame] | 29 | return "Field(name: %s, type: %s)" % (self.name, self.type) |
Paul Vinciguerra | d7a32eb | 2020-05-01 10:09:58 -0400 | [diff] [blame] | 30 | elif type(self.len) == dict: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 31 | return "Field(name: %s, type: %s, length: %s)" % ( |
| 32 | self.name, |
| 33 | self.type, |
| 34 | self.len, |
| 35 | ) |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 36 | elif self.len > 0: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 37 | return "Field(name: %s, type: %s, length: %s)" % ( |
| 38 | self.name, |
| 39 | self.type, |
| 40 | self.len, |
| 41 | ) |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 42 | else: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 43 | return "Field(name: %s, type: %s, variable length stored in: %s)" % ( |
| 44 | self.name, |
| 45 | self.type, |
| 46 | self.nelem_field, |
| 47 | ) |
Klement Sekera | 34a962b | 2018-09-06 19:31:36 +0200 | [diff] [blame] | 48 | |
| 49 | def is_vla(self): |
| 50 | return self.nelem_field is not None |
| 51 | |
| 52 | def has_vla(self): |
| 53 | return self.is_vla() or self.type.has_vla() |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 54 | |
| 55 | |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 56 | class Alias(Field): |
| 57 | pass |
| 58 | |
| 59 | |
Klement Sekera | 958b750 | 2017-09-28 06:31:53 +0200 | [diff] [blame] | 60 | class Type(object): |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 61 | def __init__(self, name): |
| 62 | self.name = name |
| 63 | |
Klement Sekera | 32a9d7b | 2017-12-10 05:15:41 +0100 | [diff] [blame] | 64 | def __str__(self): |
| 65 | return self.name |
| 66 | |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 67 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 68 | class SimpleType(Type): |
Klement Sekera | 34a962b | 2018-09-06 19:31:36 +0200 | [diff] [blame] | 69 | def has_vla(self): |
| 70 | return False |
| 71 | |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 72 | |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 73 | def get_msg_header_defs(struct_type_class, field_class, json_parser, logger): |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 74 | return [ |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 75 | struct_type_class( |
| 76 | [ |
| 77 | "msg_header1_t", |
| 78 | ["u16", "_vl_msg_id"], |
| 79 | ["u32", "context"], |
| 80 | ], |
| 81 | json_parser, |
| 82 | field_class, |
| 83 | logger, |
| 84 | ), |
| 85 | struct_type_class( |
| 86 | [ |
| 87 | "msg_header2_t", |
| 88 | ["u16", "_vl_msg_id"], |
| 89 | ["u32", "client_index"], |
| 90 | ["u32", "context"], |
| 91 | ], |
| 92 | json_parser, |
| 93 | field_class, |
| 94 | logger, |
| 95 | ), |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 96 | ] |
| 97 | |
| 98 | |
Klement Sekera | 958b750 | 2017-09-28 06:31:53 +0200 | [diff] [blame] | 99 | class Struct(object): |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 100 | def __init__(self, name, fields): |
| 101 | self.name = name |
| 102 | self.fields = fields |
| 103 | self.field_names = [n.name for n in self.fields] |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 104 | self.depends = [f.type for f in self.fields] |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 105 | |
Klement Sekera | 32a9d7b | 2017-12-10 05:15:41 +0100 | [diff] [blame] | 106 | def __str__(self): |
| 107 | return "[%s]" % "], [".join([str(f) for f in self.fields]) |
| 108 | |
Klement Sekera | 34a962b | 2018-09-06 19:31:36 +0200 | [diff] [blame] | 109 | def has_vla(self): |
| 110 | for f in self.fields: |
| 111 | if f.has_vla(): |
| 112 | return True |
| 113 | return False |
| 114 | |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 115 | |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 116 | class Enum(SimpleType): |
| 117 | def __init__(self, name, value_pairs, enumtype): |
| 118 | super(Enum, self).__init__(name) |
| 119 | self.type = enumtype |
| 120 | self.value_pairs = value_pairs |
| 121 | |
| 122 | def __str__(self): |
| 123 | return "Enum(%s, [%s])" % ( |
| 124 | self.name, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 125 | "], [".join(["%s => %s" % (i, j) for i, j in self.value_pairs]), |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 126 | ) |
| 127 | |
| 128 | |
| 129 | class Union(Type): |
| 130 | def __init__(self, name, type_pairs, crc): |
| 131 | Type.__init__(self, name) |
| 132 | self.crc = crc |
| 133 | self.type_pairs = type_pairs |
| 134 | self.depends = [t for t, _ in self.type_pairs] |
| 135 | |
| 136 | def __str__(self): |
| 137 | return "Union(%s, [%s])" % ( |
| 138 | self.name, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 139 | "], [".join(["%s %s" % (i, j) for i, j in self.type_pairs]), |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 140 | ) |
| 141 | |
Klement Sekera | 34a962b | 2018-09-06 19:31:36 +0200 | [diff] [blame] | 142 | def has_vla(self): |
| 143 | return False |
| 144 | |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 145 | |
Klement Sekera | 958b750 | 2017-09-28 06:31:53 +0200 | [diff] [blame] | 146 | class Message(object): |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 147 | def __init__(self, logger, definition, json_parser): |
| 148 | struct_type_class = json_parser.struct_type_class |
| 149 | field_class = json_parser.field_class |
Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 150 | self.request = None |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 151 | self.logger = logger |
| 152 | m = definition |
| 153 | logger.debug("Parsing message definition `%s'" % m) |
| 154 | name = m[0] |
| 155 | self.name = name |
| 156 | logger.debug("Message name is `%s'" % name) |
| 157 | ignore = True |
| 158 | self.header = None |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 159 | self.is_reply = json_parser.is_reply(self.name) |
| 160 | self.is_event = json_parser.is_event(self.name) |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 161 | fields = [] |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 162 | for header in get_msg_header_defs( |
| 163 | struct_type_class, field_class, json_parser, logger |
| 164 | ): |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 165 | logger.debug("Probing header `%s'" % header.name) |
| 166 | if header.is_part_of_def(m[1:]): |
| 167 | self.header = header |
| 168 | logger.debug("Found header `%s'" % header.name) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 169 | fields.append(field_class(field_name="header", field_type=self.header)) |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 170 | ignore = False |
| 171 | break |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 172 | if ignore and not self.is_event and not self.is_reply: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 173 | raise ParseError( |
| 174 | "While parsing message `%s': could not find all " |
| 175 | "common header fields" % name |
| 176 | ) |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 177 | for field in m[1:]: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 178 | if isinstance(field, dict) and "crc" in field: |
| 179 | self.crc = field["crc"] |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 180 | logger.debug("Found CRC `%s'" % self.crc) |
| 181 | continue |
| 182 | else: |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 183 | field_type = json_parser.lookup_type_like_id(field[0]) |
Klement Sekera | 8b6b5ab | 2018-05-03 14:27:42 +0200 | [diff] [blame] | 184 | logger.debug("Parsing message field `%s'" % field) |
Ole Troan | 7adaa22 | 2019-08-27 15:05:27 +0200 | [diff] [blame] | 185 | l = len(field) |
| 186 | if any(type(n) is dict for n in field): |
| 187 | l -= 1 |
| 188 | if l == 2: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 189 | if self.header is not None and self.header.has_field(field[1]): |
Ole Troan | 7adaa22 | 2019-08-27 15:05:27 +0200 | [diff] [blame] | 190 | continue |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 191 | p = field_class(field_name=field[1], field_type=field_type) |
Ole Troan | 7adaa22 | 2019-08-27 15:05:27 +0200 | [diff] [blame] | 192 | elif l == 3: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 193 | if field[2] == 0 and field[0] != "string": |
Ole Troan | 7adaa22 | 2019-08-27 15:05:27 +0200 | [diff] [blame] | 194 | raise ParseError( |
| 195 | "While parsing message `%s': variable length " |
| 196 | "array `%s' doesn't have reference to member " |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 197 | "containing the actual length" % (name, field[1]) |
| 198 | ) |
| 199 | if field[0] == "string" and field[2] > 0: |
| 200 | field_type = json_parser.lookup_type_like_id("u8") |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 201 | |
Ole Troan | 7adaa22 | 2019-08-27 15:05:27 +0200 | [diff] [blame] | 202 | p = field_class( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 203 | field_name=field[1], field_type=field_type, array_len=field[2] |
| 204 | ) |
Ole Troan | 7adaa22 | 2019-08-27 15:05:27 +0200 | [diff] [blame] | 205 | elif l == 4: |
| 206 | nelem_field = None |
| 207 | for f in fields: |
| 208 | if f.name == field[3]: |
| 209 | nelem_field = f |
| 210 | if nelem_field is None: |
| 211 | raise ParseError( |
| 212 | "While parsing message `%s': couldn't find " |
| 213 | "variable length array `%s' member containing " |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 214 | "the actual length `%s'" % (name, field[1], field[3]) |
| 215 | ) |
Ole Troan | 7adaa22 | 2019-08-27 15:05:27 +0200 | [diff] [blame] | 216 | p = field_class( |
| 217 | field_name=field[1], |
| 218 | field_type=field_type, |
| 219 | array_len=field[2], |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 220 | nelem_field=nelem_field, |
| 221 | ) |
Ole Troan | 7adaa22 | 2019-08-27 15:05:27 +0200 | [diff] [blame] | 222 | else: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 223 | raise Exception( |
| 224 | "Don't know how to parse message " |
| 225 | "definition for message `%s': `%s'" % (m, m[1:]) |
| 226 | ) |
Ole Troan | 7adaa22 | 2019-08-27 15:05:27 +0200 | [diff] [blame] | 227 | logger.debug("Parsed field `%s'" % p) |
| 228 | fields.append(p) |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 229 | self.fields = fields |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 230 | self.depends = [f.type for f in self.fields] |
Klement Sekera | 34a962b | 2018-09-06 19:31:36 +0200 | [diff] [blame] | 231 | logger.debug("Parsed message: %s" % self) |
| 232 | |
| 233 | def __str__(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 234 | return "Message(%s, [%s], {crc: %s}" % ( |
| 235 | self.name, |
| 236 | "], [".join([str(f) for f in self.fields]), |
| 237 | self.crc, |
| 238 | ) |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 239 | |
| 240 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 241 | class StructType(Type, Struct): |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 242 | def __init__(self, definition, json_parser, field_class, logger): |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 243 | t = definition |
Klement Sekera | 8b6b5ab | 2018-05-03 14:27:42 +0200 | [diff] [blame] | 244 | logger.debug("Parsing struct definition `%s'" % t) |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 245 | name = t[0] |
| 246 | fields = [] |
| 247 | for field in t[1:]: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 248 | if len(field) == 1 and "crc" in field: |
| 249 | self.crc = field["crc"] |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 250 | continue |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 251 | field_type = json_parser.lookup_type_like_id(field[0]) |
Klement Sekera | 8b6b5ab | 2018-05-03 14:27:42 +0200 | [diff] [blame] | 252 | logger.debug("Parsing type field `%s'" % field) |
Ole Troan | 7adaa22 | 2019-08-27 15:05:27 +0200 | [diff] [blame] | 253 | if len(field) == 2: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 254 | p = field_class(field_name=field[1], field_type=field_type) |
Ole Troan | 7adaa22 | 2019-08-27 15:05:27 +0200 | [diff] [blame] | 255 | elif len(field) == 3: |
| 256 | if field[2] == 0: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 257 | raise ParseError( |
| 258 | "While parsing type `%s': array `%s' has " |
| 259 | "variable length" % (name, field[1]) |
| 260 | ) |
| 261 | p = field_class( |
| 262 | field_name=field[1], field_type=field_type, array_len=field[2] |
| 263 | ) |
Ole Troan | 7adaa22 | 2019-08-27 15:05:27 +0200 | [diff] [blame] | 264 | elif len(field) == 4: |
| 265 | nelem_field = None |
| 266 | for f in fields: |
| 267 | if f.name == field[3]: |
| 268 | nelem_field = f |
| 269 | if nelem_field is None: |
| 270 | raise ParseError( |
| 271 | "While parsing message `%s': couldn't find " |
| 272 | "variable length array `%s' member containing " |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 273 | "the actual length `%s'" % (name, field[1], field[3]) |
| 274 | ) |
| 275 | p = field_class( |
| 276 | field_name=field[1], |
| 277 | field_type=field_type, |
| 278 | array_len=field[2], |
| 279 | nelem_field=nelem_field, |
| 280 | ) |
Ole Troan | 7adaa22 | 2019-08-27 15:05:27 +0200 | [diff] [blame] | 281 | else: |
| 282 | raise ParseError( |
| 283 | "Don't know how to parse field `%s' of type definition " |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 284 | "for type `%s'" % (field, t) |
| 285 | ) |
Ole Troan | 7adaa22 | 2019-08-27 15:05:27 +0200 | [diff] [blame] | 286 | fields.append(p) |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 287 | Type.__init__(self, name) |
| 288 | Struct.__init__(self, name, fields) |
| 289 | |
Klement Sekera | 32a9d7b | 2017-12-10 05:15:41 +0100 | [diff] [blame] | 290 | def __str__(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 291 | return "StructType(%s, %s)" % (Type.__str__(self), Struct.__str__(self)) |
Klement Sekera | 32a9d7b | 2017-12-10 05:15:41 +0100 | [diff] [blame] | 292 | |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 293 | def has_field(self, name): |
| 294 | return name in self.field_names |
| 295 | |
| 296 | def is_part_of_def(self, definition): |
| 297 | for idx in range(len(self.fields)): |
| 298 | field = definition[idx] |
| 299 | p = self.fields[idx] |
| 300 | if field[1] != p.name: |
| 301 | return False |
| 302 | if field[0] != p.type.name: |
| 303 | raise ParseError( |
| 304 | "Unexpected field type `%s' (should be `%s'), " |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 305 | "while parsing msg/def/field `%s/%s/%s'" |
| 306 | % (field[0], p.type, p.name, definition, field) |
| 307 | ) |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 308 | return True |
| 309 | |
| 310 | |
Klement Sekera | 958b750 | 2017-09-28 06:31:53 +0200 | [diff] [blame] | 311 | class JsonParser(object): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 312 | def __init__( |
| 313 | self, |
| 314 | logger, |
| 315 | files, |
| 316 | simple_type_class=SimpleType, |
| 317 | enum_class=Enum, |
| 318 | union_class=Union, |
| 319 | struct_type_class=StructType, |
| 320 | field_class=Field, |
| 321 | message_class=Message, |
| 322 | alias_class=Alias, |
| 323 | ): |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 324 | self.services = {} |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 325 | self.messages = {} |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 326 | self.enums = {} |
Matthew Smith | d1c7506 | 2022-11-09 17:45:19 +0000 | [diff] [blame] | 327 | self.enumflags = {} |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 328 | self.unions = {} |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 329 | self.aliases = {} |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 330 | self.types = { |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 331 | x: simple_type_class(x) |
| 332 | for x in [ |
| 333 | "i8", |
| 334 | "i16", |
| 335 | "i32", |
| 336 | "i64", |
| 337 | "u8", |
| 338 | "u16", |
| 339 | "u32", |
| 340 | "u64", |
| 341 | "f64", |
| 342 | "bool", |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 343 | ] |
| 344 | } |
| 345 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 346 | self.types["string"] = simple_type_class("vl_api_string_t") |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 347 | self.replies = set() |
| 348 | self.events = set() |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 349 | self.simple_type_class = simple_type_class |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 350 | self.enum_class = enum_class |
| 351 | self.union_class = union_class |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 352 | self.struct_type_class = struct_type_class |
| 353 | self.field_class = field_class |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 354 | self.alias_class = alias_class |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 355 | self.message_class = message_class |
| 356 | |
| 357 | self.exceptions = [] |
| 358 | self.json_files = [] |
| 359 | self.types_by_json = {} |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 360 | self.enums_by_json = {} |
| 361 | self.unions_by_json = {} |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 362 | self.aliases_by_json = {} |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 363 | self.messages_by_json = {} |
| 364 | self.logger = logger |
| 365 | for f in files: |
| 366 | self.parse_json_file(f) |
| 367 | self.finalize_parsing() |
| 368 | |
| 369 | def parse_json_file(self, path): |
| 370 | self.logger.info("Parsing json api file: `%s'" % path) |
| 371 | self.json_files.append(path) |
Ole Troan | 52ca756 | 2018-03-06 17:45:32 +0100 | [diff] [blame] | 372 | self.types_by_json[path] = [] |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 373 | self.enums_by_json[path] = [] |
| 374 | self.unions_by_json[path] = [] |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 375 | self.aliases_by_json[path] = [] |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 376 | self.messages_by_json[path] = {} |
| 377 | with open(path) as f: |
| 378 | j = json.load(f) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 379 | for k in j["services"]: |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 380 | if k in self.services: |
| 381 | raise ParseError("Duplicate service `%s'" % k) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 382 | self.services[k] = j["services"][k] |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 383 | self.replies.add(self.services[k]["reply"]) |
| 384 | if "events" in self.services[k]: |
| 385 | for x in self.services[k]["events"]: |
| 386 | self.events.add(x) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 387 | for e in j["enums"]: |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 388 | name = e[0] |
| 389 | value_pairs = e[1:-1] |
| 390 | enumtype = self.types[e[-1]["enumtype"]] |
| 391 | enum = self.enum_class(name, value_pairs, enumtype) |
| 392 | self.enums[enum.name] = enum |
| 393 | self.logger.debug("Parsed enum: %s" % enum) |
| 394 | self.enums_by_json[path].append(enum) |
Matthew Smith | d1c7506 | 2022-11-09 17:45:19 +0000 | [diff] [blame] | 395 | for e in j["enumflags"]: |
| 396 | name = e[0] |
| 397 | value_pairs = e[1:-1] |
| 398 | enumtype = self.types[e[-1]["enumtype"]] |
| 399 | enum = self.enum_class(name, value_pairs, enumtype) |
| 400 | self.enums[enum.name] = enum |
| 401 | self.logger.debug("Parsed enumflag: %s" % enum) |
| 402 | self.enums_by_json[path].append(enum) |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 403 | exceptions = [] |
| 404 | progress = 0 |
| 405 | last_progress = 0 |
| 406 | while True: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 407 | for u in j["unions"]: |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 408 | name = u[0] |
| 409 | if name in self.unions: |
| 410 | progress = progress + 1 |
| 411 | continue |
| 412 | try: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 413 | type_pairs = [ |
| 414 | [self.lookup_type_like_id(t), n] for t, n in u[1:] |
| 415 | ] |
Ole Troan | 8dbfb43 | 2019-04-24 14:31:18 +0200 | [diff] [blame] | 416 | union = self.union_class(name, type_pairs, 0) |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 417 | progress = progress + 1 |
| 418 | except ParseError as e: |
| 419 | exceptions.append(e) |
| 420 | continue |
| 421 | self.unions[union.name] = union |
| 422 | self.logger.debug("Parsed union: %s" % union) |
| 423 | self.unions_by_json[path].append(union) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 424 | for t in j["types"]: |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 425 | if t[0] in self.types: |
| 426 | progress = progress + 1 |
| 427 | continue |
| 428 | try: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 429 | type_ = self.struct_type_class( |
| 430 | t, self, self.field_class, self.logger |
| 431 | ) |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 432 | if type_.name in self.types: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 433 | raise ParseError("Duplicate type `%s'" % type_.name) |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 434 | progress = progress + 1 |
| 435 | except ParseError as e: |
| 436 | exceptions.append(e) |
| 437 | continue |
| 438 | self.types[type_.name] = type_ |
| 439 | self.types_by_json[path].append(type_) |
| 440 | self.logger.debug("Parsed type: %s" % type_) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 441 | for name, body in j["aliases"].items(): |
Ole Troan | 75761b9 | 2019-09-11 17:49:08 +0200 | [diff] [blame] | 442 | if name in self.aliases: |
| 443 | progress = progress + 1 |
| 444 | continue |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 445 | if "length" in body: |
| 446 | array_len = body["length"] |
Ole Troan | 75761b9 | 2019-09-11 17:49:08 +0200 | [diff] [blame] | 447 | else: |
| 448 | array_len = None |
| 449 | try: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 450 | t = self.lookup_type_like_id(body["type"]) |
Ole Troan | 75761b9 | 2019-09-11 17:49:08 +0200 | [diff] [blame] | 451 | except ParseError as e: |
| 452 | exceptions.append(e) |
| 453 | continue |
| 454 | alias = self.alias_class(name, t, array_len) |
| 455 | self.aliases[name] = alias |
| 456 | self.logger.debug("Parsed alias: %s" % alias) |
| 457 | self.aliases_by_json[path].append(alias) |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 458 | if not exceptions: |
| 459 | # finished parsing |
| 460 | break |
| 461 | if progress <= last_progress: |
| 462 | # cannot make forward progress |
| 463 | self.exceptions.extend(exceptions) |
Klement Sekera | ab11ec9 | 2018-11-26 15:37:28 +0100 | [diff] [blame] | 464 | break |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 465 | exceptions = [] |
| 466 | last_progress = progress |
| 467 | progress = 0 |
Klement Sekera | 8b6b5ab | 2018-05-03 14:27:42 +0200 | [diff] [blame] | 468 | prev_length = len(self.messages) |
| 469 | processed = [] |
| 470 | while True: |
| 471 | exceptions = [] |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 472 | for m in j["messages"]: |
Klement Sekera | 8b6b5ab | 2018-05-03 14:27:42 +0200 | [diff] [blame] | 473 | if m in processed: |
| 474 | continue |
| 475 | try: |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 476 | msg = self.message_class(self.logger, m, self) |
Klement Sekera | 8b6b5ab | 2018-05-03 14:27:42 +0200 | [diff] [blame] | 477 | if msg.name in self.messages: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 478 | raise ParseError("Duplicate message `%s'" % msg.name) |
Klement Sekera | 8b6b5ab | 2018-05-03 14:27:42 +0200 | [diff] [blame] | 479 | except ParseError as e: |
| 480 | exceptions.append(e) |
| 481 | continue |
| 482 | self.messages[msg.name] = msg |
| 483 | self.messages_by_json[path][msg.name] = msg |
| 484 | processed.append(m) |
| 485 | if prev_length == len(self.messages): |
| 486 | # cannot make forward progress ... |
| 487 | self.exceptions.extend(exceptions) |
| 488 | break |
| 489 | prev_length = len(self.messages) |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 490 | |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 491 | def lookup_type_like_id(self, name): |
| 492 | mundane_name = remove_magic(name) |
| 493 | if name in self.types: |
| 494 | return self.types[name] |
| 495 | elif name in self.enums: |
| 496 | return self.enums[name] |
Matthew Smith | d1c7506 | 2022-11-09 17:45:19 +0000 | [diff] [blame] | 497 | elif name in self.enumflags: |
| 498 | return self.enumflags[name] |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 499 | elif name in self.unions: |
| 500 | return self.unions[name] |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 501 | elif name in self.aliases: |
| 502 | return self.aliases[name] |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 503 | elif mundane_name in self.types: |
| 504 | return self.types[mundane_name] |
| 505 | elif mundane_name in self.enums: |
| 506 | return self.enums[mundane_name] |
Matthew Smith | d1c7506 | 2022-11-09 17:45:19 +0000 | [diff] [blame] | 507 | elif mundane_name in self.enumflags: |
| 508 | return self.enumflags[mundane_name] |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 509 | elif mundane_name in self.unions: |
| 510 | return self.unions[mundane_name] |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 511 | elif mundane_name in self.aliases: |
| 512 | return self.aliases[mundane_name] |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 513 | raise ParseError( |
| 514 | "Could not find type, enum or union by magic name `%s' nor by " |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 515 | "mundane name `%s'" % (name, mundane_name) |
| 516 | ) |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 517 | |
| 518 | def is_reply(self, message): |
| 519 | return message in self.replies |
| 520 | |
| 521 | def is_event(self, message): |
| 522 | return message in self.events |
| 523 | |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 524 | def get_reply(self, message): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 525 | return self.messages[self.services[message]["reply"]] |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 526 | |
| 527 | def finalize_parsing(self): |
| 528 | if len(self.messages) == 0: |
| 529 | for e in self.exceptions: |
Damjan Marion | 4c64b6e | 2018-08-26 18:14:46 +0200 | [diff] [blame] | 530 | self.logger.warning(e) |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 531 | for jn, j in self.messages_by_json.items(): |
| 532 | remove = [] |
| 533 | for n, m in j.items(): |
| 534 | try: |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 535 | if not m.is_reply and not m.is_event: |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 536 | try: |
| 537 | m.reply = self.get_reply(n) |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 538 | if "stream" in self.services[m.name]: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 539 | m.reply_is_stream = self.services[m.name]["stream"] |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 540 | else: |
| 541 | m.reply_is_stream = False |
Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 542 | m.reply.request = m |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 543 | except: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 544 | raise ParseError("Cannot find reply to message `%s'" % n) |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 545 | except ParseError as e: |
| 546 | self.exceptions.append(e) |
| 547 | remove.append(n) |
| 548 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 549 | self.messages_by_json[jn] = {k: v for k, v in j.items() if k not in remove} |