You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
3.8 KiB

9 years ago
  1. from itertools import chain
  2. from gooey.gui.widgets import components
  3. is_required = lambda widget: widget['required']
  4. is_checkbox = lambda widget: widget['type'] == 'CheckBox'
  5. class ViewModel(object):
  6. def __init__(self, build_spec):
  7. self.config = build_spec
  8. self.fields = []
  9. self.required_fields = []
  10. self.optional_fields = []
  11. required_args, optional_args = partition(build_spec['widgets'], is_required)
  12. checkbox_args, general_args = partition(optional_args, is_checkbox)
  13. self.required_fields = map(self.field_dispatch, required_args)
  14. self.optional_fields = map(self.field_dispatch, general_args + checkbox_args)
  15. print self.required_fields
  16. print self.optional_fields
  17. def field_dispatch(self, data):
  18. if data['type'] == 'RadioGroup':
  19. return MultiField(data)
  20. return Field(data)
  21. def required_section_complete(self):
  22. required_args_present = map(lambda x: x.value, self.fields)
  23. return bool(required_args_present and all(required_args_present))
  24. def skipping_config(self):
  25. return self.config['manual_start']
  26. def build_cmd_string(self):
  27. _f = lambda lst: [x for x in lst if x is not None]
  28. optional_args = _f([c.GetValue() for c in self.optional_fields])
  29. required_args = _f([c.GetValue() for c in self.required_fields if c.HasOptionString()])
  30. position_args = _f([c.GetValue() for c in self.required_fields if not c.HasOptionString()])
  31. if position_args: position_args.insert(0, "--")
  32. return ' '.join(chain(required_args, optional_args, position_args))
  33. def validate(self):
  34. errors = []
  35. for field in self.fields:
  36. if not field.is_valid():
  37. errors.append({field.name: field.errors})
  38. class MultiField(object):
  39. def __init__(self, widget):
  40. self.required = widget['required']
  41. self.wxwidget = self._prime(widget)
  42. self.type = widget['type']
  43. self.nargs = self._unwrap_fields('nargs', widget['data'])
  44. self.command = self._unwrap_cmds(widget['data'])
  45. self.display_name = self._unwrap_fields('display_name', widget['data'])
  46. self.help = self._unwrap_fields('help', widget['data'])
  47. self.choices = self._unwrap_fields('choices', widget['data'])
  48. def _prime(self, widget):
  49. # pre-builds the widget
  50. widget_class = getattr(components, widget['type'])
  51. return widget_class(data=widget['data'])
  52. def _unwrap_fields(self, key, collection):
  53. return [data[key] for data in collection]
  54. def _unwrap_cmds(self, collection):
  55. return [x['commands'][0] if x['commands'] else ''
  56. for x in collection]
  57. def build(self, parent):
  58. return self.wxwidget.build(parent)
  59. class Field(object):
  60. def __init__(self, widget):
  61. data = widget['data']
  62. self.required = widget['required']
  63. self.wxwidget = self._prime(widget)
  64. self.type = widget['type']
  65. self.nargs = data['nargs']
  66. self.command = data['commands'][0] if data['commands'] else ''
  67. self.display_name = data['display_name']
  68. self.help = data['help']
  69. self.choices = data['choices']
  70. def __str__(self):
  71. return '{0}: {1}'.format(self.display_name, self.type)
  72. def _prime(self, widget):
  73. # pre-builds the widget
  74. widget_class = getattr(components, widget['type'])
  75. return widget_class(data=widget['data'])
  76. @property
  77. def value(self):
  78. return self.wxwidget.GetValue()
  79. @value.setter
  80. def value(self, val):
  81. self.wxwidget.setValue(val)
  82. def validate(self):
  83. pass
  84. def build(self, parent):
  85. return self.wxwidget.build(parent)
  86. def build_widget(widget_info):
  87. widget_class = getattr(components, widget_info['type'])
  88. return widget_class(data=widget_info['data'])
  89. def partition(collection, condition):
  90. return filter(condition, collection), filter(lambda x: not condition(x), collection)