Browse Source

Unhooked the json generation from the GUI generation. Refactorings

pull/90/merge
chriskiehl 10 years ago
parent
commit
6839ff93ed
7 changed files with 93 additions and 67 deletions
  1. 54
      gooey/gui/application.py
  2. 8
      gooey/gui/component_builder.py
  3. 12
      gooey/gui/controller.py
  4. 3
      gooey/gui/widgets/components.py
  5. 21
      gooey/gui/windows/advanced_config.py
  6. 8
      gooey/gui/windows/base_window.py
  7. 54
      gooey/python_bindings/gooey_decorator.py

54
gooey/gui/application.py

@ -0,0 +1,54 @@
import itertools
import wx
import os
import sys
import json
import argparse
from functools import partial
from gooey.gui.lang import i18n
from gooey.gui.windows.base_window import BaseWindow
from gooey.gui.windows.advanced_config import AdvancedConfigPanel
def run(build_spec=None):
if not build_spec:
if len(sys.argv) > 1:
parser = argparse.ArgumentParser(description='Gooey turns your command line programs into beautiful, user friendly GUIs')
parser.add_argument('file', help='Path to the configuration file for Gooey. We need this to run! :) ')
args = parser.parse_args()
gooey_config = args.file
else:
local_files = os.listdir(os.getcwd())
if 'gooey_config.json' not in local_files:
print "Bugger! gooey_config.json not found!"
sys.exit(1)
gooey_config = os.path.join(os.getcwd(), 'gooey_config.json')
if not os.path.exists(gooey_config):
raise IOError('Gooey Config not found')
with open(gooey_config, 'r') as f:
build_spec = json.load(f)
app = wx.App(False)
i18n.load(build_spec['language'])
BodyPanel = partial(AdvancedConfigPanel, build_spec=build_spec)
frame = BaseWindow(BodyPanel, build_spec)
frame.Show(True)
app.MainLoop()
if __name__ == '__main__':
run()

8
gooey/gui/component_builder.py

@ -1,6 +1,6 @@
import itertools import itertools
from gooey.gui.widgets import components2
from gooey.gui.widgets import components
class ComponentBuilder(object): class ComponentBuilder(object):
@ -13,8 +13,8 @@ class ComponentBuilder(object):
optionals = self.build_widget(_optional_specs) if _optional_specs else None optionals = self.build_widget(_optional_specs) if _optional_specs else None
if _optional_specs: if _optional_specs:
self.flags = [widget for widget in optionals if isinstance(widget, components2.CheckBox)]
self.general_options = [widget for widget in optionals if not isinstance(widget, components2.CheckBox)]
self.flags = [widget for widget in optionals if isinstance(widget, components.CheckBox)]
self.general_options = [widget for widget in optionals if not isinstance(widget, components.CheckBox)]
else: else:
self.flags = [] self.flags = []
self.general_options = [] self.general_options = []
@ -25,7 +25,7 @@ class ComponentBuilder(object):
widget_type = spec['type'] widget_type = spec['type']
properties = spec['data'] properties = spec['data']
Component = getattr(components2, widget_type)
Component = getattr(components, widget_type)
assembled_widgets.append(Component(data=properties)) assembled_widgets.append(Component(data=properties))
return assembled_widgets return assembled_widgets

12
gooey/gui/controller.py

@ -4,13 +4,11 @@ Created on Dec 22, 2013
@author: Chris @author: Chris
''' '''
import subprocess
import sys
from multiprocessing.dummy import Pool, Process
import time
import platform
import wx import wx
import sys
import subprocess
from multiprocessing.dummy import Pool
from gooey.gui.lang import i18n from gooey.gui.lang import i18n
@ -28,8 +26,8 @@ class Controller(object):
def __init__(self, base_frame, build_spec): def __init__(self, base_frame, build_spec):
''' '''
:param base_frame: BaseWindow
:param build_spec: Json
:type base_frame: BaseWindow
:type build_spec: dict
''' '''
self.core_gui = base_frame self.core_gui = base_frame
self.build_spec = build_spec self.build_spec = build_spec

gooey/gui/widgets/components2.py → gooey/gui/widgets/components.py

@ -1,4 +1,4 @@
import time
from gooey.gui.widgets import widget_pack from gooey.gui.widgets import widget_pack
__author__ = 'Chris' __author__ = 'Chris'
@ -23,7 +23,6 @@ class BaseGuiComponent(object):
# used to throttle resizing (to avoid widget jiggle) # used to throttle resizing (to avoid widget jiggle)
# TODO: figure out anti-jiggle technology # TODO: figure out anti-jiggle technology
# self.last_update = time.time()
# self.event_stack = [] # self.event_stack = []
def build(self, parent): def build(self, parent):

21
gooey/gui/windows/advanced_config.py

@ -4,6 +4,7 @@ Created on Dec 28, 2013
@author: Chris @author: Chris
""" """
from itertools import chain from itertools import chain
import itertools
import wx import wx
from wx.lib.scrolledpanel import ScrolledPanel from wx.lib.scrolledpanel import ScrolledPanel
@ -15,6 +16,7 @@ from gooey.gui import styling
PADDING = 10 PADDING = 10
class AdvancedConfigPanel(ScrolledPanel, OptionReader): class AdvancedConfigPanel(ScrolledPanel, OptionReader):
""" """
Abstract class for the Footer panels. Abstract class for the Footer panels.
@ -26,7 +28,7 @@ class AdvancedConfigPanel(ScrolledPanel, OptionReader):
self.SetDoubleBuffered(True) self.SetDoubleBuffered(True)
self._action_groups = build_spec
self._build_spec = build_spec
self._positionals = build_spec.get('required', None) self._positionals = build_spec.get('required', None)
self.components = component_builder.ComponentBuilder(build_spec) self.components = component_builder.ComponentBuilder(build_spec)
@ -67,11 +69,8 @@ class AdvancedConfigPanel(ScrolledPanel, OptionReader):
container.Add(styling.HorizontalRule(self), *STD_LAYOUT) container.Add(styling.HorizontalRule(self), *STD_LAYOUT)
container.AddSpacer(20) container.AddSpacer(20)
self.CreateComponentGrid(container, self.components.general_options, cols=2)
self.CreateComponentGrid(container, self.components.flags, cols=3)
# container.Add(general_opts_grid, *STD_LAYOUT)
# container.AddSpacer(30)
# container.Add(flag_grids, 0, wx.LEFT | wx.RIGHT | wx.EXPAND, PADDING)
self.CreateComponentGrid(container, self.components.general_options, cols=self._build_spec['optionals_cols'])
self.CreateComponentGrid(container, self.components.flags, cols=self._build_spec['optionals_cols'])
self.SetSizer(container) self.SetSizer(container)
@ -83,10 +82,9 @@ class AdvancedConfigPanel(ScrolledPanel, OptionReader):
sizer.AddSpacer(8) sizer.AddSpacer(8)
def CreateComponentGrid(self, parent_sizer, components, cols=2): def CreateComponentGrid(self, parent_sizer, components, cols=2):
rows = [components[i:i+cols] for i in range(0, len(components), cols)]
for row in rows:
for row in self.chunk(components, cols):
hsizer = wx.BoxSizer(wx.HORIZONTAL) hsizer = wx.BoxSizer(wx.HORIZONTAL)
for widget in row:
for widget in filter(None, row):
hsizer.Add(widget.build(self), 1, wx.EXPAND | wx.LEFT | wx.RIGHT, 10) hsizer.Add(widget.build(self), 1, wx.EXPAND | wx.LEFT | wx.RIGHT, 10)
# hsizer.FitInside(parent_sizer) # hsizer.FitInside(parent_sizer)
parent_sizer.Add(hsizer, 0, wx.EXPAND) parent_sizer.Add(hsizer, 0, wx.EXPAND)
@ -120,6 +118,11 @@ class AdvancedConfigPanel(ScrolledPanel, OptionReader):
# Not used anywhere. Keep for debugging? # Not used anywhere. Keep for debugging?
return filter(None, [arg.GetValue() for arg in chain(self.components.general_options, self.components.flags)]) return filter(None, [arg.GetValue() for arg in chain(self.components.general_options, self.components.flags)])
def chunk(self, iterable, n, fillvalue=None):
"Collect data into fixed-length chunks or blocks"
# grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx
args = [iter(iterable)] * n
return itertools.izip_longest(fillvalue=fillvalue, *args)
if __name__ == '__main__': if __name__ == '__main__':
pass pass

8
gooey/gui/windows/base_window.py

@ -16,10 +16,9 @@ from gooey.gui.windows import footer, header
class BaseWindow(wx.Frame): class BaseWindow(wx.Frame):
def __init__(self, BodyPanel, build_spec, params):
def __init__(self, BodyPanel, build_spec):
wx.Frame.__init__(self, parent=None, id=-1) wx.Frame.__init__(self, parent=None, id=-1)
self._params = params
self.build_spec = build_spec self.build_spec = build_spec
self._controller = None self._controller = None
@ -40,10 +39,7 @@ class BaseWindow(wx.Frame):
self.Bind(wx.EVT_SIZE, self.onResize) self.Bind(wx.EVT_SIZE, self.onResize)
def _init_properties(self): def _init_properties(self):
if not self._params['program_name']:
title = os.path.basename(sys.argv[0].replace('.py', ''))
else:
title = self._params['program_name']
title = self.build_spec['program_name']
self.SetTitle(title) self.SetTitle(title)
self.SetSize(self.build_spec['default_size']) self.SetSize(self.build_spec['default_size'])
# self.SetMinSize((400, 300)) # self.SetMinSize((400, 300))

54
gooey/python_bindings/gooey_decorator.py

@ -45,18 +45,18 @@ done.
''' '''
import wx
import os import os
import sys
import atexit
import tempfile import tempfile
import wx
import source_parser import source_parser
import atexit
from functools import partial
from gooey.gui.lang import i18n
from gooey.gui import application
from gooey.gui.windows import layouts from gooey.gui.windows import layouts
from gooey.python_bindings import argparse_to_json from gooey.python_bindings import argparse_to_json
def Gooey(f=None, advanced=True, def Gooey(f=None, advanced=True,
language='english', show_config=True, language='english', show_config=True,
program_name=None, program_description=None): program_name=None, program_description=None):
@ -93,54 +93,30 @@ def Gooey(f=None, advanced=True,
# Must be called before anything else # Must be called before anything else
app = wx.App(False) app = wx.App(False)
i18n.load(language)
# load gui components after loading the language pack
from gooey.gui.client_app import ClientApp
from gooey.gui.client_app import EmptyClientApp
from gooey.gui.windows.base_window import BaseWindow
from gooey.gui.windows.advanced_config import AdvancedConfigPanel
from gooey.gui.windows.basic_config_panel import BasicConfigPanel
meta = {
build_spec = {
'language': language,
'target': run_cmd, 'target': run_cmd,
'program_name': program_name,
'program_name': program_name or os.path.basename(sys.argv[0].replace('.py', '')),
'program_description': program_description or '', 'program_description': program_description or '',
'show_config': show_config, 'show_config': show_config,
'show_advanced': advanced, 'show_advanced': advanced,
'default_size': (610, 530), 'default_size': (610, 530),
'requireds_cols': 1, 'requireds_cols': 1,
'optionals_cols': 2,
'optionals_cols': 4,
'manual_start': False 'manual_start': False
} }
if show_config: if show_config:
parser = get_parser(main_module_path) parser = get_parser(main_module_path)
meta['program_description'] = parser.description or program_description
client_app = ClientApp(parser, payload)
if advanced:
build_spec = dict(meta.items() + argparse_to_json.convert(parser).items())
BodyPanel = partial(AdvancedConfigPanel, build_spec=build_spec)
else:
build_spec = dict(meta.items() + layouts.basic_config.items())
BodyPanel = partial(AdvancedConfigPanel, build_spec=build_spec)
# User doesn't want to display configuration screen
# Just jump straight to the run panel
else:
build_spec = dict(meta.items() + layouts.basic_config.items())
build_spec['manual_start'] = True
BodyPanel = partial(AdvancedConfigPanel, build_spec=build_spec)
client_app = EmptyClientApp(payload)
build_spec['program_description'] = parser.description or program_description
layout_data = argparse_to_json.convert(parser) if advanced else layouts.basic_config.items()
build_spec.update(layout_data)
frame = BaseWindow(BodyPanel, build_spec, params)
else:
build_spec['manual_start'] = True
if not show_config:
frame.ManualStart()
frame.Show(True)
app.MainLoop()
application.run(build_spec)
inner.__name__ = payload.__name__ inner.__name__ = payload.__name__
return inner return inner

Loading…
Cancel
Save