X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=src%2Ftools%2Fvppapigen%2Fvppapigen.py;h=80097a10a62766b4eca06e87ec02cbe1a0f92a90;hb=2cd3cc8a605a062c7900dbcc3fad53d04723c298;hp=fd87b18792b9ed85b99cdaeb82491d85961fb27e;hpb=85465588b18fef9c4712f864f512e00741e2d4f2;p=vpp.git diff --git a/src/tools/vppapigen/vppapigen.py b/src/tools/vppapigen/vppapigen.py index fd87b18792b..80097a10a62 100755 --- a/src/tools/vppapigen/vppapigen.py +++ b/src/tools/vppapigen/vppapigen.py @@ -1,14 +1,16 @@ #!/usr/bin/python3 -from __future__ import print_function import ply.lex as lex import ply.yacc as yacc import sys import argparse +import keyword import logging import binascii import os +log = logging.getLogger('vppapigen') + # Ensure we don't leave temporary files around sys.dont_write_bytecode = True @@ -23,8 +25,6 @@ global_types = {} def global_type_add(name, obj): '''Add new type to the dictionary of types ''' type_name = 'vl_api_' + name + '_t' - if type_name in global_types: - raise KeyError('Type is already defined: {}'.format(name)) global_types[type_name] = obj @@ -79,9 +79,12 @@ class VPPAPILexer(object): t_ignore_LINE_COMMENT = '//.*' def t_NUM(self, t): - r'0[xX][0-9a-fA-F]+|\d+' + r'0[xX][0-9a-fA-F]+|-?\d+\.?\d*' base = 16 if t.value.startswith('0x') else 10 - t.value = int(t.value, base) + if '.' in t.value: + t.value = float(t.value) + else: + t.value = int(t.value, base) return t def t_ID(self, t): @@ -125,6 +128,7 @@ def crc_block_combine(block, crc): s = str(block).encode() return binascii.crc32(s, crc) & 0xffffffff + class Service(): def __init__(self, caller, reply, events=None, stream=False): self.caller = caller @@ -247,12 +251,9 @@ class Import(): f = os.path.join(dir, filename) if os.path.exists(f): break - if sys.version[0] == '2': - with open(f) as fd: - self.result = parser.parse_file(fd, None) - else: - with open(f, encoding='utf-8') as fd: - self.result = parser.parse_file(fd, None) + + with open(f, encoding='utf-8') as fd: + self.result = parser.parse_file(fd, None) def __repr__(self): return self.filename @@ -291,6 +292,9 @@ class Field(): def __init__(self, fieldtype, name, limit=None): self.type = 'Field' self.fieldtype = fieldtype + if name in keyword.kwlist: + raise ValueError("Fieldname {!r} is a python keyword and is not " + "accessible via the python API. ".format(name)) self.fieldname = name self.limit = limit @@ -764,6 +768,7 @@ def dirlist_add(dirs): def dirlist_get(): return dirlist + def foldup_blocks(block, crc): for b in block: # Look up CRC in user defined types @@ -777,32 +782,32 @@ def foldup_blocks(block, crc): pass return crc + def foldup_crcs(s): for f in s: f.crc = foldup_blocks(f.block, binascii.crc32(f.crc)) + # # Main # def main(): + if sys.version_info < (3, 5,): + log.exception('vppapigen requires a supported version of python. ' + 'Please use version 3.5 or greater. ' + 'Using {}'.format(sys.version)) + return 1 + cliparser = argparse.ArgumentParser(description='VPP API generator') cliparser.add_argument('--pluginpath', default=""), cliparser.add_argument('--includedir', action='append'), - if sys.version[0] == '2': - cliparser.add_argument('--input', type=argparse.FileType('r'), - default=sys.stdin) - cliparser.add_argument('--output', nargs='?', - type=argparse.FileType('w'), - default=sys.stdout) - - else: - cliparser.add_argument('--input', - type=argparse.FileType('r', encoding='UTF-8'), - default=sys.stdin) - cliparser.add_argument('--output', nargs='?', - type=argparse.FileType('w', encoding='UTF-8'), - default=sys.stdout) + cliparser.add_argument('--input', + type=argparse.FileType('r', encoding='UTF-8'), + default=sys.stdin) + cliparser.add_argument('--output', nargs='?', + type=argparse.FileType('w', encoding='UTF-8'), + default=sys.stdout) cliparser.add_argument('output_module', nargs='?', default='C') cliparser.add_argument('--debug', action='store_true') @@ -825,7 +830,6 @@ def main(): logging.basicConfig(stream=sys.stdout, level=logging.WARNING) else: logging.basicConfig() - log = logging.getLogger('vppapigen') parser = VPPAPI(debug=args.debug, filename=filename, logger=log) parsed_objects = parser.parse_file(args.input, log) @@ -872,7 +876,8 @@ def main(): else: pluginpath = args.pluginpath + '/' if pluginpath == '': - raise Exception('Output plugin not found') + log.exception('Output plugin not found') + return 1 module_path = '{}vppapigen_{}.py'.format(pluginpath, args.output_module.lower()) @@ -880,16 +885,19 @@ def main(): plugin = SourceFileLoader(args.output_module, module_path).load_module() except Exception as err: - raise Exception('Error importing output plugin: {}, {}' - .format(module_path, err)) + log.exception('Error importing output plugin: {}, {}' + .format(module_path, err)) + return 1 result = plugin.run(filename, s) if result: print(result, file=args.output) else: - raise Exception('Running plugin failed: {} {}' - .format(filename, result)) + log.exception('Running plugin failed: {} {}' + .format(filename, result)) + return 1 + return 0 if __name__ == '__main__': - main() + sys.exit(main())