From 7d62d0920c438ea36819005c642d76668c49cdc7 Mon Sep 17 00:00:00 2001 From: Benjamin Tissoires Date: Tue, 5 Nov 2019 11:22:02 +0100 Subject: generate_openapi: add a little bit more verbosity when we get an Error --- openapi/generate_openapi.py | 50 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 10 deletions(-) diff --git a/openapi/generate_openapi.py b/openapi/generate_openapi.py index b1d7ab84..7c152f3c 100644 --- a/openapi/generate_openapi.py +++ b/openapi/generate_openapi.py @@ -677,6 +677,27 @@ class Schemas(object): print(' - {}'.format(f)) +class Context(object): + def __init__(self, path): + self.path = path + + with open(path) as f: + self._txt = f.readlines() + + data = ''.join(self._txt) + self.program = esprima.parseModule(data, + options={ + 'comment': True, + 'loc': True + }) + + def txt_for(self, statement): + return self.text_at(statement.loc.start.line, statement.loc.end.line) + + def text_at(self, begin, end): + return ''.join(self._txt[begin - 1:end]) + + def parse_schemas(schemas_dir): schemas = {} @@ -686,17 +707,19 @@ def parse_schemas(schemas_dir): files.sort() for filename in files: path = os.path.join(root, filename) - with open(path) as f: - data = ''.join(f.readlines()) - try: - # if the file failed, it's likely it doesn't contain a schema - program = esprima.parseModule(data, options={'comment': True, 'loc': True}) - except: - continue + try: + # if the file failed, it's likely it doesn't contain a schema + context = Context(path) + except: + continue - current_schema = None - jsdocs = [c for c in program.comments - if c.type == 'Block' and c.value.startswith('*\n')] + program = context.program + + current_schema = None + jsdocs = [c for c in program.comments + if c.type == 'Block' and c.value.startswith('*\n')] + + try: for statement in program.body: @@ -742,6 +765,13 @@ def parse_schemas(schemas_dir): if j.loc.end.line + 1 == operation.loc.start.line] if bool(jsdoc): entry_point.doc = jsdoc[0] + except TypeError: + logger.warning(context.txt_for(statement)) + logger.error('{}:{}-{} can not parse {}'.format(path, + statement.loc.start.line, + statement.loc.end.line, + statement.type)) + raise return schemas, entry_points -- cgit v1.2.3-1-g7c22