From 00a42517e7ac375dbb85eed108cc511f8f004f2a Mon Sep 17 00:00:00 2001 From: chriskiehl Date: Wed, 15 Apr 2015 20:47:44 -0400 Subject: [PATCH] Enhancement: Lookup argparse outside of main - issue #84 --- gooey/__init__.py | 2 +- gooey/__main__.py | 24 ++++++------- gooey/examples/gooey_config.json | 41 +++++++++++++++++++++++ gooey/examples/mock_argparse_example.py | 2 +- gooey/examples/widget_demo.py | 17 ++++------ gooey/python_bindings/config_generator.py | 4 +-- gooey/python_bindings/gooey_decorator.py | 2 +- gooey/python_bindings/source_parser.py | 7 ++-- 8 files changed, 69 insertions(+), 30 deletions(-) create mode 100644 gooey/examples/gooey_config.json diff --git a/gooey/__init__.py b/gooey/__init__.py index fc4822f..e57cadc 100644 --- a/gooey/__init__.py +++ b/gooey/__init__.py @@ -1,7 +1,7 @@ import os from gooey.python_bindings.gooey_decorator import Gooey from gooey.python_bindings.gooey_parser import GooeyParser -from gooey.gui import application +# from gooey.gui import application version_file = os.path.join(os.path.dirname(__file__), 'version') diff --git a/gooey/__main__.py b/gooey/__main__.py index 3e819af..70877e9 100644 --- a/gooey/__main__.py +++ b/gooey/__main__.py @@ -1,12 +1,12 @@ -''' -Delegates arguments to the main Gooey runner - -For use when run directly from command line with the -m (module) flag: - - e.g. $ python -m gooey - -''' - -from gooey import application - -application.main() +# ''' +# Delegates arguments to the main Gooey runner +# +# For use when run directly from command line with the -m (module) flag: +# +# e.g. $ python -m gooey +# +# ''' +# +# from gooey import application +# +# application.main() diff --git a/gooey/examples/gooey_config.json b/gooey/examples/gooey_config.json new file mode 100644 index 0000000..5e56610 --- /dev/null +++ b/gooey/examples/gooey_config.json @@ -0,0 +1,41 @@ +{ + "show_advanced": true, + "language": "english", + "manual_start": false, + "optionals_cols": 3, + "required": [ + { + "data": { + "nargs": "+", + "commands": [], + "display_name": "integers", + "help": "an integer for the accumulator", + "choices": [] + }, + "type": "TextField" + } + ], + "requireds_cols": 1, + "show_config": true, + "default_size": [ + 610, + 530 + ], + "optional": [ + { + "data": { + "nargs": "", + "commands": [ + "--sum" + ], + "display_name": "accumulate", + "help": "sum the integers (default: find the max)", + "choices": [] + }, + "type": "CheckBox" + } + ], + "program_name": "tmpewuheq", + "program_description": "Process some integers.", + "target": "python c:\\users\\chris\\appdata\\local\\temp\\tmpewuheq.py" +} \ No newline at end of file diff --git a/gooey/examples/mock_argparse_example.py b/gooey/examples/mock_argparse_example.py index 00d6485..b2a205d 100644 --- a/gooey/examples/mock_argparse_example.py +++ b/gooey/examples/mock_argparse_example.py @@ -1,7 +1,7 @@ import argparse from gooey import Gooey -@Gooey +@Gooey(dump_build_config=True) def main(): parser = argparse.ArgumentParser(description='Process some integers.') parser.add_argument('integers', metavar='N', type=int, nargs='+', diff --git a/gooey/examples/widget_demo.py b/gooey/examples/widget_demo.py index 3679dec..e0e8e5e 100644 --- a/gooey/examples/widget_demo.py +++ b/gooey/examples/widget_demo.py @@ -12,8 +12,9 @@ from gooey import Gooey from gooey import GooeyParser + @Gooey -def main(): +def arbitrary_function(): desc = "Example application to show Gooey's various widgets" file_help_msg = "Name of the file you want to process" my_cool_parser = GooeyParser(description=desc) @@ -39,6 +40,10 @@ def main(): print 'inside of main(), my_cool_parser =', my_cool_parser args = my_cool_parser.parse_args() + main(args) + + +def main(args): print sys.argv print args.countdown print args.showtime @@ -58,12 +63,4 @@ def here_is_smore(): if __name__ == '__main__': - main() - # import inspect - # import dis - # # print dir(main.__code__) - # # for i in dir(main.__code__): - # # print i, getattr(main.__code__, i) - # print dis.dis(main.__code__) - # # for i in inspect.getmembers(main): - # # print i + arbitrary_function() diff --git a/gooey/python_bindings/config_generator.py b/gooey/python_bindings/config_generator.py index 5a5cd45..4d0907f 100644 --- a/gooey/python_bindings/config_generator.py +++ b/gooey/python_bindings/config_generator.py @@ -4,7 +4,7 @@ from gooey.gui.windows import layouts from gooey.python_bindings import source_parser -def create_from_module(module_path, **kwargs): +def create_from_module(module_path, payload_name, **kwargs): show_config = kwargs.get('show_config', False) run_cmd = 'python {}'.format(module_path) @@ -23,7 +23,7 @@ def create_from_module(module_path, **kwargs): } if show_config: - parser = source_parser.extract_parser(module_path) + parser = source_parser.extract_parser(module_path, payload_name) build_spec['program_description'] = parser.description or build_spec['program_description'] layout_data = argparse_to_json.convert(parser) if build_spec['show_advanced'] else layouts.basic_config.items() diff --git a/gooey/python_bindings/gooey_decorator.py b/gooey/python_bindings/gooey_decorator.py index a106c80..8feb91c 100644 --- a/gooey/python_bindings/gooey_decorator.py +++ b/gooey/python_bindings/gooey_decorator.py @@ -87,7 +87,7 @@ def Gooey(f=None, if not source_parser.has_argparse(cleaned_source): show_config = False - build_spec = config_generator.create_from_module(tmp_filepath, **params) + build_spec = config_generator.create_from_module(tmp_filepath, payload_name=payload.__name__, **params) if dump_build_config: config_path = os.path.join(os.getcwd(), 'gooey_config.json') diff --git a/gooey/python_bindings/source_parser.py b/gooey/python_bindings/source_parser.py index 4870dfc..351378f 100644 --- a/gooey/python_bindings/source_parser.py +++ b/gooey/python_bindings/source_parser.py @@ -125,14 +125,14 @@ def format_source_to_return_parser(source, cutoff_line, restart_line, col_offset return ''.join(new_source) -def extract_parser(modulepath): +def extract_parser(modulepath, func_with_argparse): source = read_client_module(modulepath) nodes = ast.parse(''.join(source)) funcs = get_nodes_by_instance_type(nodes, _ast.FunctionDef) assignment_objs = get_nodes_by_instance_type(nodes, _ast.Assign) - main_func = get_nodes_by_containing_attr(funcs, 'main')[0] + main_func = get_nodes_by_containing_attr(funcs, func_with_argparse)[0] parse_args_assignment = get_nodes_by_containing_attr(main_func.body, 'parse_args')[0] # ast reports the line no of a block structure as the start of the structure, @@ -151,7 +151,8 @@ def extract_parser(modulepath): parser_name=parse_args_assignment.value.func.value.id ) client_module = modules.load(module_source) - return client_module.main() + return getattr(client_module, func_with_argparse)() + def has_argparse(source): return any(['.parse_args(' in line.lower() for line in source.split('\n')])