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) |
Ole Troan | 9ac1138 | 2019-04-23 17:11:01 +0200 | [diff] [blame^] | 170 | l = len(field) |
| 171 | if any(type(n) is dict for n in field): |
| 172 | l -= 1 |
| 173 | if l == 2: |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 174 | if self.header is not None and\ |
| 175 | self.header.has_field(field[1]): |
| 176 | continue |
| 177 | p = field_class(field_name=field[1], |
| 178 | field_type=field_type) |
Ole Troan | 9ac1138 | 2019-04-23 17:11:01 +0200 | [diff] [blame^] | 179 | elif l == 3: |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 180 | if field[2] == 0: |
| 181 | raise ParseError( |
| 182 | "While parsing message `%s': variable length " |
| 183 | "array `%s' doesn't have reference to member " |
| 184 | "containing the actual length" % ( |
| 185 | name, field[1])) |
| 186 | p = field_class( |
| 187 | field_name=field[1], |
| 188 | field_type=field_type, |
| 189 | array_len=field[2]) |
Ole Troan | 9ac1138 | 2019-04-23 17:11:01 +0200 | [diff] [blame^] | 190 | elif l == 4: |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 191 | nelem_field = None |
| 192 | for f in fields: |
| 193 | if f.name == field[3]: |
| 194 | nelem_field = f |
| 195 | if nelem_field is None: |
| 196 | raise ParseError( |
| 197 | "While parsing message `%s': couldn't find " |
| 198 | "variable length array `%s' member containing " |
| 199 | "the actual length `%s'" % ( |
| 200 | name, field[1], field[3])) |
| 201 | p = field_class( |
| 202 | field_name=field[1], |
| 203 | field_type=field_type, |
| 204 | array_len=field[2], |
| 205 | nelem_field=nelem_field) |
| 206 | else: |
| 207 | raise Exception("Don't know how to parse message " |
| 208 | "definition for message `%s': `%s'" % |
| 209 | (m, m[1:])) |
| 210 | logger.debug("Parsed field `%s'" % p) |
| 211 | fields.append(p) |
| 212 | self.fields = fields |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 213 | self.depends = [f.type for f in self.fields] |
Klement Sekera | 34a962b | 2018-09-06 19:31:36 +0200 | [diff] [blame] | 214 | logger.debug("Parsed message: %s" % self) |
| 215 | |
| 216 | def __str__(self): |
| 217 | return "Message(%s, [%s], {crc: %s}" % \ |
| 218 | (self.name, |
| 219 | "], [".join([str(f) for f in self.fields]), |
| 220 | self.crc) |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 221 | |
| 222 | |
| 223 | class StructType (Type, Struct): |
| 224 | |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 225 | def __init__(self, definition, json_parser, field_class, logger): |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 226 | t = definition |
Klement Sekera | 8b6b5ab | 2018-05-03 14:27:42 +0200 | [diff] [blame] | 227 | logger.debug("Parsing struct definition `%s'" % t) |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 228 | name = t[0] |
| 229 | fields = [] |
| 230 | for field in t[1:]: |
| 231 | if len(field) == 1 and 'crc' in field: |
| 232 | self.crc = field['crc'] |
| 233 | continue |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 234 | field_type = json_parser.lookup_type_like_id(field[0]) |
Klement Sekera | 8b6b5ab | 2018-05-03 14:27:42 +0200 | [diff] [blame] | 235 | logger.debug("Parsing type field `%s'" % field) |
Klement Sekera | 329a1c1 | 2018-02-25 18:48:04 +0100 | [diff] [blame] | 236 | if len(field) == 2: |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 237 | p = field_class(field_name=field[1], |
Klement Sekera | 329a1c1 | 2018-02-25 18:48:04 +0100 | [diff] [blame] | 238 | field_type=field_type) |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 239 | elif len(field) == 3: |
| 240 | if field[2] == 0: |
| 241 | raise ParseError("While parsing type `%s': array `%s' has " |
| 242 | "variable length" % (name, field[1])) |
| 243 | p = field_class(field_name=field[1], |
Klement Sekera | 329a1c1 | 2018-02-25 18:48:04 +0100 | [diff] [blame] | 244 | field_type=field_type, |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 245 | array_len=field[2]) |
Klement Sekera | 8b6b5ab | 2018-05-03 14:27:42 +0200 | [diff] [blame] | 246 | elif len(field) == 4: |
| 247 | nelem_field = None |
| 248 | for f in fields: |
| 249 | if f.name == field[3]: |
| 250 | nelem_field = f |
| 251 | if nelem_field is None: |
| 252 | raise ParseError( |
| 253 | "While parsing message `%s': couldn't find " |
| 254 | "variable length array `%s' member containing " |
| 255 | "the actual length `%s'" % ( |
| 256 | name, field[1], field[3])) |
| 257 | p = field_class(field_name=field[1], |
| 258 | field_type=field_type, |
| 259 | array_len=field[2], |
| 260 | nelem_field=nelem_field) |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 261 | else: |
| 262 | raise ParseError( |
Klement Sekera | 8b6b5ab | 2018-05-03 14:27:42 +0200 | [diff] [blame] | 263 | "Don't know how to parse field `%s' of type definition " |
| 264 | "for type `%s'" % (field, t)) |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 265 | fields.append(p) |
| 266 | Type.__init__(self, name) |
| 267 | Struct.__init__(self, name, fields) |
| 268 | |
Klement Sekera | 32a9d7b | 2017-12-10 05:15:41 +0100 | [diff] [blame] | 269 | def __str__(self): |
| 270 | return "StructType(%s, %s)" % (Type.__str__(self), |
| 271 | Struct.__str__(self)) |
| 272 | |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 273 | def has_field(self, name): |
| 274 | return name in self.field_names |
| 275 | |
| 276 | def is_part_of_def(self, definition): |
| 277 | for idx in range(len(self.fields)): |
| 278 | field = definition[idx] |
| 279 | p = self.fields[idx] |
| 280 | if field[1] != p.name: |
| 281 | return False |
| 282 | if field[0] != p.type.name: |
| 283 | raise ParseError( |
| 284 | "Unexpected field type `%s' (should be `%s'), " |
| 285 | "while parsing msg/def/field `%s/%s/%s'" % |
| 286 | (field[0], p.type, p.name, definition, field)) |
| 287 | return True |
| 288 | |
| 289 | |
Klement Sekera | 958b750 | 2017-09-28 06:31:53 +0200 | [diff] [blame] | 290 | class JsonParser(object): |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 291 | def __init__(self, logger, files, simple_type_class=SimpleType, |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 292 | enum_class=Enum, union_class=Union, |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 293 | struct_type_class=StructType, field_class=Field, |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 294 | message_class=Message, alias_class=Alias): |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 295 | self.services = {} |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 296 | self.messages = {} |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 297 | self.enums = {} |
| 298 | self.unions = {} |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 299 | self.aliases = {} |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 300 | self.types = { |
| 301 | x: simple_type_class(x) for x in [ |
| 302 | 'i8', 'i16', 'i32', 'i64', |
| 303 | 'u8', 'u16', 'u32', 'u64', |
Filip Varga | dd1e3e7 | 2019-04-15 18:52:43 +0200 | [diff] [blame] | 304 | 'f64', 'bool' |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 305 | ] |
| 306 | } |
| 307 | |
Ole Troan | 003d5da | 2018-12-18 12:23:13 +0100 | [diff] [blame] | 308 | self.types['string'] = simple_type_class('vl_api_string_t') |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 309 | self.replies = set() |
| 310 | self.events = set() |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 311 | self.simple_type_class = simple_type_class |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 312 | self.enum_class = enum_class |
| 313 | self.union_class = union_class |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 314 | self.struct_type_class = struct_type_class |
| 315 | self.field_class = field_class |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 316 | self.alias_class = alias_class |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 317 | self.message_class = message_class |
| 318 | |
| 319 | self.exceptions = [] |
| 320 | self.json_files = [] |
| 321 | self.types_by_json = {} |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 322 | self.enums_by_json = {} |
| 323 | self.unions_by_json = {} |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 324 | self.aliases_by_json = {} |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 325 | self.messages_by_json = {} |
| 326 | self.logger = logger |
| 327 | for f in files: |
| 328 | self.parse_json_file(f) |
| 329 | self.finalize_parsing() |
| 330 | |
| 331 | def parse_json_file(self, path): |
| 332 | self.logger.info("Parsing json api file: `%s'" % path) |
| 333 | self.json_files.append(path) |
Ole Troan | 52ca756 | 2018-03-06 17:45:32 +0100 | [diff] [blame] | 334 | self.types_by_json[path] = [] |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 335 | self.enums_by_json[path] = [] |
| 336 | self.unions_by_json[path] = [] |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 337 | self.aliases_by_json[path] = [] |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 338 | self.messages_by_json[path] = {} |
| 339 | with open(path) as f: |
| 340 | j = json.load(f) |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 341 | for k in j['services']: |
| 342 | if k in self.services: |
| 343 | raise ParseError("Duplicate service `%s'" % k) |
| 344 | self.services[k] = j['services'][k] |
| 345 | self.replies.add(self.services[k]["reply"]) |
| 346 | if "events" in self.services[k]: |
| 347 | for x in self.services[k]["events"]: |
| 348 | self.events.add(x) |
| 349 | for e in j['enums']: |
| 350 | name = e[0] |
| 351 | value_pairs = e[1:-1] |
| 352 | enumtype = self.types[e[-1]["enumtype"]] |
| 353 | enum = self.enum_class(name, value_pairs, enumtype) |
| 354 | self.enums[enum.name] = enum |
| 355 | self.logger.debug("Parsed enum: %s" % enum) |
| 356 | self.enums_by_json[path].append(enum) |
| 357 | exceptions = [] |
| 358 | progress = 0 |
| 359 | last_progress = 0 |
| 360 | while True: |
| 361 | for u in j['unions']: |
| 362 | name = u[0] |
| 363 | if name in self.unions: |
| 364 | progress = progress + 1 |
| 365 | continue |
| 366 | try: |
| 367 | type_pairs = [[self.lookup_type_like_id(t), n] |
| 368 | for t, n in u[1:-1]] |
| 369 | crc = u[-1]["crc"] |
| 370 | union = self.union_class(name, type_pairs, crc) |
| 371 | progress = progress + 1 |
| 372 | except ParseError as e: |
| 373 | exceptions.append(e) |
| 374 | continue |
| 375 | self.unions[union.name] = union |
| 376 | self.logger.debug("Parsed union: %s" % union) |
| 377 | self.unions_by_json[path].append(union) |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 378 | for name, body in j['aliases'].iteritems(): |
| 379 | if name in self.aliases: |
| 380 | progress = progress + 1 |
| 381 | continue |
| 382 | if 'length' in body: |
| 383 | array_len = body['length'] |
| 384 | else: |
| 385 | array_len = None |
| 386 | t = self.types[body['type']] |
| 387 | alias = self.alias_class(name, t, array_len) |
| 388 | self.aliases[name] = alias |
| 389 | self.logger.debug("Parsed alias: %s" % alias) |
| 390 | self.aliases_by_json[path].append(alias) |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 391 | for t in j['types']: |
| 392 | if t[0] in self.types: |
| 393 | progress = progress + 1 |
| 394 | continue |
| 395 | try: |
| 396 | type_ = self.struct_type_class(t, self, |
| 397 | self.field_class, |
| 398 | self.logger) |
| 399 | if type_.name in self.types: |
| 400 | raise ParseError( |
| 401 | "Duplicate type `%s'" % type_.name) |
| 402 | progress = progress + 1 |
| 403 | except ParseError as e: |
| 404 | exceptions.append(e) |
| 405 | continue |
| 406 | self.types[type_.name] = type_ |
| 407 | self.types_by_json[path].append(type_) |
| 408 | self.logger.debug("Parsed type: %s" % type_) |
| 409 | if not exceptions: |
| 410 | # finished parsing |
| 411 | break |
| 412 | if progress <= last_progress: |
| 413 | # cannot make forward progress |
| 414 | self.exceptions.extend(exceptions) |
Klement Sekera | ab11ec9 | 2018-11-26 15:37:28 +0100 | [diff] [blame] | 415 | break |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 416 | exceptions = [] |
| 417 | last_progress = progress |
| 418 | progress = 0 |
Klement Sekera | 8b6b5ab | 2018-05-03 14:27:42 +0200 | [diff] [blame] | 419 | prev_length = len(self.messages) |
| 420 | processed = [] |
| 421 | while True: |
| 422 | exceptions = [] |
| 423 | for m in j['messages']: |
| 424 | if m in processed: |
| 425 | continue |
| 426 | try: |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 427 | msg = self.message_class(self.logger, m, self) |
Klement Sekera | 8b6b5ab | 2018-05-03 14:27:42 +0200 | [diff] [blame] | 428 | if msg.name in self.messages: |
| 429 | raise ParseError( |
| 430 | "Duplicate message `%s'" % msg.name) |
| 431 | except ParseError as e: |
| 432 | exceptions.append(e) |
| 433 | continue |
| 434 | self.messages[msg.name] = msg |
| 435 | self.messages_by_json[path][msg.name] = msg |
| 436 | processed.append(m) |
| 437 | if prev_length == len(self.messages): |
| 438 | # cannot make forward progress ... |
| 439 | self.exceptions.extend(exceptions) |
| 440 | break |
| 441 | prev_length = len(self.messages) |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 442 | |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 443 | def lookup_type_like_id(self, name): |
| 444 | mundane_name = remove_magic(name) |
| 445 | if name in self.types: |
| 446 | return self.types[name] |
| 447 | elif name in self.enums: |
| 448 | return self.enums[name] |
| 449 | elif name in self.unions: |
| 450 | return self.unions[name] |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 451 | elif name in self.aliases: |
| 452 | return self.aliases[name] |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 453 | elif mundane_name in self.types: |
| 454 | return self.types[mundane_name] |
| 455 | elif mundane_name in self.enums: |
| 456 | return self.enums[mundane_name] |
| 457 | elif mundane_name in self.unions: |
| 458 | return self.unions[mundane_name] |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 459 | elif mundane_name in self.aliases: |
| 460 | return self.aliases[mundane_name] |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 461 | raise ParseError( |
| 462 | "Could not find type, enum or union by magic name `%s' nor by " |
| 463 | "mundane name `%s'" % (name, mundane_name)) |
| 464 | |
| 465 | def is_reply(self, message): |
| 466 | return message in self.replies |
| 467 | |
| 468 | def is_event(self, message): |
| 469 | return message in self.events |
| 470 | |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 471 | def get_reply(self, message): |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 472 | return self.messages[self.services[message]['reply']] |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 473 | |
| 474 | def finalize_parsing(self): |
| 475 | if len(self.messages) == 0: |
| 476 | for e in self.exceptions: |
Damjan Marion | 4c64b6e | 2018-08-26 18:14:46 +0200 | [diff] [blame] | 477 | self.logger.warning(e) |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 478 | for jn, j in self.messages_by_json.items(): |
| 479 | remove = [] |
| 480 | for n, m in j.items(): |
| 481 | try: |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 482 | if not m.is_reply and not m.is_event: |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 483 | try: |
| 484 | m.reply = self.get_reply(n) |
Klement Sekera | 2108c0c | 2018-08-24 11:43:20 +0200 | [diff] [blame] | 485 | if "stream" in self.services[m.name]: |
| 486 | m.reply_is_stream = \ |
| 487 | self.services[m.name]["stream"] |
| 488 | else: |
| 489 | m.reply_is_stream = False |
Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 490 | m.reply.request = m |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 491 | except: |
| 492 | raise ParseError( |
| 493 | "Cannot find reply to message `%s'" % n) |
| 494 | except ParseError as e: |
| 495 | self.exceptions.append(e) |
| 496 | remove.append(n) |
| 497 | |
| 498 | self.messages_by_json[jn] = { |
| 499 | k: v for k, v in j.items() if k not in remove} |