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