X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=src%2Fvpp-api%2Fvapi%2Fvapi_json_parser.py;h=fda3f75d9c1d50de41c796f358e33838ab917306;hb=dd1e3e780;hp=799542491f681cd7b43077fb04c890ef44e0ec6b;hpb=2108c0c6daeb39a0619ace66189cc20e049a3c79;p=vpp.git diff --git a/src/vpp-api/vapi/vapi_json_parser.py b/src/vpp-api/vapi/vapi_json_parser.py index 799542491f6..fda3f75d9c1 100644 --- a/src/vpp-api/vapi/vapi_json_parser.py +++ b/src/vpp-api/vapi/vapi_json_parser.py @@ -28,13 +28,25 @@ class Field(object): def __str__(self): if self.len is None: - return "name: %s, type: %s" % (self.name, self.type) + return "Field(name: %s, type: %s)" % (self.name, self.type) elif self.len > 0: - return "name: %s, type: %s, length: %s" % (self.name, self.type, - self.len) + return "Field(name: %s, type: %s, length: %s)" % (self.name, + self.type, + self.len) else: - return ("name: %s, type: %s, variable length stored in: %s" % - (self.name, self.type, self.nelem_field)) + return ( + "Field(name: %s, type: %s, variable length stored in: %s)" % + (self.name, self.type, self.nelem_field)) + + def is_vla(self): + return self.nelem_field is not None + + def has_vla(self): + return self.is_vla() or self.type.has_vla() + + +class Alias(Field): + pass class Type(object): @@ -47,11 +59,8 @@ class Type(object): class SimpleType (Type): - def __init__(self, name): - super(SimpleType, self).__init__(name) - - def __str__(self): - return self.name + def has_vla(self): + return False def get_msg_header_defs(struct_type_class, field_class, json_parser, logger): @@ -83,6 +92,12 @@ class Struct(object): def __str__(self): return "[%s]" % "], [".join([str(f) for f in self.fields]) + def has_vla(self): + for f in self.fields: + if f.has_vla(): + return True + return False + class Enum(SimpleType): def __init__(self, name, value_pairs, enumtype): @@ -110,6 +125,9 @@ class Union(Type): "], [" .join(["%s %s" % (i, j) for i, j in self.type_pairs]) ) + def has_vla(self): + return False + class Message(object): @@ -190,6 +208,13 @@ class Message(object): fields.append(p) self.fields = fields self.depends = [f.type for f in self.fields] + logger.debug("Parsed message: %s" % self) + + def __str__(self): + return "Message(%s, [%s], {crc: %s}" % \ + (self.name, + "], [".join([str(f) for f in self.fields]), + self.crc) class StructType (Type, Struct): @@ -263,19 +288,21 @@ class JsonParser(object): def __init__(self, logger, files, simple_type_class=SimpleType, enum_class=Enum, union_class=Union, struct_type_class=StructType, field_class=Field, - message_class=Message): + message_class=Message, alias_class=Alias): self.services = {} self.messages = {} self.enums = {} self.unions = {} + self.aliases = {} self.types = { x: simple_type_class(x) for x in [ 'i8', 'i16', 'i32', 'i64', 'u8', 'u16', 'u32', 'u64', - 'f64' + 'f64', 'bool' ] } + self.types['string'] = simple_type_class('vl_api_string_t') self.replies = set() self.events = set() self.simple_type_class = simple_type_class @@ -283,6 +310,7 @@ class JsonParser(object): self.union_class = union_class self.struct_type_class = struct_type_class self.field_class = field_class + self.alias_class = alias_class self.message_class = message_class self.exceptions = [] @@ -290,6 +318,7 @@ class JsonParser(object): self.types_by_json = {} self.enums_by_json = {} self.unions_by_json = {} + self.aliases_by_json = {} self.messages_by_json = {} self.logger = logger for f in files: @@ -302,6 +331,7 @@ class JsonParser(object): self.types_by_json[path] = [] self.enums_by_json[path] = [] self.unions_by_json[path] = [] + self.aliases_by_json[path] = [] self.messages_by_json[path] = {} with open(path) as f: j = json.load(f) @@ -342,6 +372,19 @@ class JsonParser(object): self.unions[union.name] = union self.logger.debug("Parsed union: %s" % union) self.unions_by_json[path].append(union) + for name, body in j['aliases'].iteritems(): + if name in self.aliases: + progress = progress + 1 + continue + if 'length' in body: + array_len = body['length'] + else: + array_len = None + t = self.types[body['type']] + alias = self.alias_class(name, t, array_len) + self.aliases[name] = alias + self.logger.debug("Parsed alias: %s" % alias) + self.aliases_by_json[path].append(alias) for t in j['types']: if t[0] in self.types: progress = progress + 1 @@ -366,6 +409,7 @@ class JsonParser(object): if progress <= last_progress: # cannot make forward progress self.exceptions.extend(exceptions) + break exceptions = [] last_progress = progress progress = 0 @@ -401,12 +445,16 @@ class JsonParser(object): return self.enums[name] elif name in self.unions: return self.unions[name] + elif name in self.aliases: + return self.aliases[name] elif mundane_name in self.types: return self.types[mundane_name] elif mundane_name in self.enums: return self.enums[mundane_name] elif mundane_name in self.unions: return self.unions[mundane_name] + elif mundane_name in self.aliases: + return self.aliases[mundane_name] raise ParseError( "Could not find type, enum or union by magic name `%s' nor by " "mundane name `%s'" % (name, mundane_name))