From 6e0c77d73c5b30453012b4248bed527159cb6b5a Mon Sep 17 00:00:00 2001 From: Seth M Morton Date: Tue, 17 Feb 2015 20:49:28 -0800 Subject: [PATCH] Generalized main function parsing. Code has been updated to understand block structures in the main() function. --- gooey/python_bindings/source_parser.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/gooey/python_bindings/source_parser.py b/gooey/python_bindings/source_parser.py index a979089..d3174d6 100644 --- a/gooey/python_bindings/source_parser.py +++ b/gooey/python_bindings/source_parser.py @@ -135,10 +135,13 @@ def extract_parser(modulepath): main_func = get_nodes_by_containing_attr(funcs, 'main')[0] parse_args_assignment = get_nodes_by_containing_attr(main_func.body, 'parse_args')[0] - # ast seemingly incorrectly reports the line no of ast.If objects (bug?) - # thus, if we end on an IF statement, decriment the line number by one. - final_line = main_func.body[-1] - restart_line = final_line.lineno if not isinstance(final_line, ast.If) else final_line.lineno - 1 + # ast reports the line no of a block structure as the start of the structure, + # not the end, so we look for the line no of the next node after main() + # and use that as the end of the main() function. + try: + restart_line = nodes.body[nodes.body.index(main_func)+1].lineno - 1 + except IndexError: + restart_line = len(source) module_source = format_source_to_return_parser( source,