Browse Source

Enhancement: Lookup argparse outside of main - issue #84

pull/90/merge
chriskiehl 9 years ago
parent
commit
00a42517e7
8 changed files with 69 additions and 30 deletions
  1. 2
      gooey/__init__.py
  2. 24
      gooey/__main__.py
  3. 41
      gooey/examples/gooey_config.json
  4. 2
      gooey/examples/mock_argparse_example.py
  5. 17
      gooey/examples/widget_demo.py
  6. 4
      gooey/python_bindings/config_generator.py
  7. 2
      gooey/python_bindings/gooey_decorator.py
  8. 7
      gooey/python_bindings/source_parser.py

2
gooey/__init__.py

@ -1,7 +1,7 @@
import os import os
from gooey.python_bindings.gooey_decorator import Gooey from gooey.python_bindings.gooey_decorator import Gooey
from gooey.python_bindings.gooey_parser import GooeyParser 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') version_file = os.path.join(os.path.dirname(__file__), 'version')

24
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()

41
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"
}

2
gooey/examples/mock_argparse_example.py

@ -1,7 +1,7 @@
import argparse import argparse
from gooey import Gooey from gooey import Gooey
@Gooey
@Gooey(dump_build_config=True)
def main(): def main():
parser = argparse.ArgumentParser(description='Process some integers.') parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('integers', metavar='N', type=int, nargs='+', parser.add_argument('integers', metavar='N', type=int, nargs='+',

17
gooey/examples/widget_demo.py

@ -12,8 +12,9 @@ from gooey import Gooey
from gooey import GooeyParser from gooey import GooeyParser
@Gooey @Gooey
def main():
def arbitrary_function():
desc = "Example application to show Gooey's various widgets" desc = "Example application to show Gooey's various widgets"
file_help_msg = "Name of the file you want to process" file_help_msg = "Name of the file you want to process"
my_cool_parser = GooeyParser(description=desc) my_cool_parser = GooeyParser(description=desc)
@ -39,6 +40,10 @@ def main():
print 'inside of main(), my_cool_parser =', my_cool_parser print 'inside of main(), my_cool_parser =', my_cool_parser
args = my_cool_parser.parse_args() args = my_cool_parser.parse_args()
main(args)
def main(args):
print sys.argv print sys.argv
print args.countdown print args.countdown
print args.showtime print args.showtime
@ -58,12 +63,4 @@ def here_is_smore():
if __name__ == '__main__': 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()

4
gooey/python_bindings/config_generator.py

@ -4,7 +4,7 @@ from gooey.gui.windows import layouts
from gooey.python_bindings import source_parser 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) show_config = kwargs.get('show_config', False)
run_cmd = 'python {}'.format(module_path) run_cmd = 'python {}'.format(module_path)
@ -23,7 +23,7 @@ def create_from_module(module_path, **kwargs):
} }
if show_config: 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'] 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() layout_data = argparse_to_json.convert(parser) if build_spec['show_advanced'] else layouts.basic_config.items()

2
gooey/python_bindings/gooey_decorator.py

@ -87,7 +87,7 @@ def Gooey(f=None,
if not source_parser.has_argparse(cleaned_source): if not source_parser.has_argparse(cleaned_source):
show_config = False 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: if dump_build_config:
config_path = os.path.join(os.getcwd(), 'gooey_config.json') config_path = os.path.join(os.getcwd(), 'gooey_config.json')

7
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) return ''.join(new_source)
def extract_parser(modulepath):
def extract_parser(modulepath, func_with_argparse):
source = read_client_module(modulepath) source = read_client_module(modulepath)
nodes = ast.parse(''.join(source)) nodes = ast.parse(''.join(source))
funcs = get_nodes_by_instance_type(nodes, _ast.FunctionDef) funcs = get_nodes_by_instance_type(nodes, _ast.FunctionDef)
assignment_objs = get_nodes_by_instance_type(nodes, _ast.Assign) 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] 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, # 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 parser_name=parse_args_assignment.value.func.value.id
) )
client_module = modules.load(module_source) client_module = modules.load(module_source)
return client_module.main()
return getattr(client_module, func_with_argparse)()
def has_argparse(source): def has_argparse(source):
return any(['.parse_args(' in line.lower() for line in source.split('\n')]) return any(['.parse_args(' in line.lower() for line in source.split('\n')])

Loading…
Cancel
Save