Browse Source

replaced lazy class with module methods

subparsing
chriskiehl 9 years ago
parent
commit
e6d72cf89a
1 changed files with 27 additions and 36 deletions
  1. 63
      gooey/gui/component_builder.py

63
gooey/gui/component_builder.py

@ -1,40 +1,31 @@
import itertools
from collections import namedtuple
from gooey.gui.widgets import components
class ComponentBuilder(object):
def __init__(self, build_spec):
self.build_spec = build_spec
_required_specs = self.build_spec.get('required', None)
_optional_specs = self.build_spec.get('optional', None)
self.required_args = self.build_widget(_required_specs) if _required_specs else []
optionals = self.build_widget(_optional_specs) if _optional_specs else None
if _optional_specs:
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:
self.flags = []
self.general_options = []
def build_widget(self, build_spec):
assembled_widgets = []
for spec in build_spec:
widget_type = spec['type']
properties = spec['data']
Component = getattr(components, widget_type)
assembled_widgets.append(Component(data=properties))
return assembled_widgets
def __iter__(self):
'''
return an iterator for all of the contained gui
'''
return itertools.chain(self.required_args or [],
self.flags or [],
self.general_options or [])
is_required = lambda widget: widget['required']
is_checkbox = lambda widget: isinstance(widget, components.CheckBox)
ComponentList = namedtuple('ComponentList', 'required_args optional_args')
def build_components(widget_list):
'''
:param widget_list: list of dicts containing widget info (name, type, etc..)
:return: ComponentList
Converts the Json widget information into concrete wx Widget types
'''
required_args, optional_args = partition(widget_list, is_required)
checkbox_args, general_args = partition(optional_args, is_checkbox)
required_args = map(build_widget, required_args)
optional_args = map(build_widget, general_args) + map(build_widget, checkbox_args)
return ComponentList(required_args, optional_args)
def build_widget(widget_info):
widget_class = getattr(components, widget_info['type'])
return widget_class(data=widget_info['data'])
def partition(collection, condition):
return filter(condition, collection), filter(lambda x: not condition(x), collection)
Loading…
Cancel
Save