Browse Source

Better method for getting Argparse - (SylvainDe's suggestion)

pull/90/merge
chriskiehl 9 years ago
parent
commit
5220b4a139
3 changed files with 43 additions and 22 deletions
  1. 2
      gooey/gui/application.py
  2. 9
      gooey/python_bindings/config_generator.py
  3. 54
      gooey/python_bindings/gooey_decorator.py

2
gooey/gui/application.py

@ -49,7 +49,7 @@ def do_build_script(module_path):
if not source_parser.has_argparse(f.read()):
raise AssertionError('Argparse not found in module. Unable to continue')
gooey_config = config_generator.create_from_module(module_path, show_config=True)
gooey_config = config_generator.create_from_parser(module_path, show_config=True)
outfile = os.path.join(os.getcwd(), 'gooey_config.json')
print 'Writing config file to: {}'.format(outfile)

9
gooey/python_bindings/config_generator.py

@ -4,15 +4,15 @@ from gooey.gui.windows import layouts
from gooey.python_bindings import source_parser
def create_from_module(module_path, payload_name, **kwargs):
def create_from_parser(parser, source_path, **kwargs):
show_config = kwargs.get('show_config', False)
run_cmd = 'python {}'.format(module_path)
a = os.path.basename(module_path).replace('.py', '')
run_cmd = 'python {}'.format(source_path)
build_spec = {
'language': kwargs.get('language', 'english'),
'target': run_cmd,
'program_name': kwargs.get('program_name') or os.path.basename(module_path).replace('.py', ''),
'program_name': kwargs.get('program_name') or os.path.basename(source_path).replace('.py', ''),
'program_description': kwargs.get('program_description', ''),
'show_config': show_config,
'show_advanced': kwargs.get('show_advanced', True),
@ -23,7 +23,6 @@ def create_from_module(module_path, payload_name, **kwargs):
}
if show_config:
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()

54
gooey/python_bindings/gooey_decorator.py

@ -44,6 +44,7 @@ to us. No more complicated ast stuff. Just a little bit of string parsing and we
done.
'''
from argparse import ArgumentParser
import json
import os
@ -58,6 +59,33 @@ from gooey.gui.windows import layouts
from gooey.python_bindings import argparse_to_json
def decorator(func):
def graphical_parse_args(self, args=None, namespace=None):
values = [raw_input("Enter %s (%s):" % (a.dest, a.help)) for a in self._actions]
raw_input('Press enter to start')
# update new args with what you get from the graphical widgets
return self.original_parse_args(arg_lst, namespace)
def inner(*args, **kwargs):
ArgumentParser.original_parse_args = ArgumentParser.parse_args
ArgumentParser.parse_args = graphical_parse_args
return func(*args, **kwargs)
return inner
def store_executable_copy():
main_module_path = get_caller_path()
_, filename = os.path.split(main_module_path)
cleaned_source = clean_source(main_module_path)
descriptor, tmp_filepath = tempfile.mkstemp(suffix='.py')
atexit.register(cleanup, descriptor, tmp_filepath)
with open(tmp_filepath, 'w') as f:
f.write(cleaned_source)
return tmp_filepath
def Gooey(f=None,
advanced=True,
language='english',
@ -76,21 +104,10 @@ def Gooey(f=None,
params = locals()
def build(payload):
def inner():
main_module_path = get_caller_path()
_, filename = os.path.split(main_module_path)
cleaned_source = clean_source(main_module_path)
descriptor, tmp_filepath = tempfile.mkstemp(suffix='.py')
atexit.register(cleanup, descriptor, tmp_filepath)
with open(tmp_filepath, 'w') as f:
f.write(cleaned_source)
def run_gooey(self, args=None, namespace=None):
source_path = store_executable_copy()
if not source_parser.has_argparse(cleaned_source):
show_config = False
build_spec = config_generator.create_from_module(tmp_filepath, payload_name=payload.__name__, **params)
build_spec = config_generator.create_from_parser(self, source_path, payload_name=payload.__name__, **params)
if dump_build_config:
config_path = os.path.join(os.getcwd(), 'gooey_config.json')
@ -100,8 +117,13 @@ def Gooey(f=None,
application.run(build_spec)
inner.__name__ = payload.__name__
return inner
def inner2(*args, **kwargs):
ArgumentParser.original_parse_args = ArgumentParser.parse_args
ArgumentParser.parse_args = run_gooey
return f(*args, **kwargs)
inner2.__name__ = payload.__name__
return inner2
if callable(f):
return build(f)

Loading…
Cancel
Save