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.

118 lines
3.4 KiB

9 years ago
  1. '''
  2. Created on Dec 22, 2013
  3. @author: Chris
  4. '''
  5. import wx
  6. import sys
  7. import subprocess
  8. from gooey.gui.pubsub import pub
  9. from multiprocessing.dummy import Pool
  10. from gooey.gui import events
  11. from gooey.gui.lang import i18n
  12. from gooey.gui.windows import views
  13. YES = 5103
  14. NO = 5104
  15. class Controller(object):
  16. '''
  17. Main controller for the gui.
  18. All controlls are delegated to this central control point.
  19. '''
  20. def __init__(self, base_frame, build_spec):
  21. '''
  22. :type base_frame: BaseWindow
  23. :type build_spec: dict
  24. '''
  25. self.core_gui = base_frame
  26. self.build_spec = build_spec
  27. # wire up all the observers
  28. pub.subscribe(self.on_cancel, events.WINDOW_CANCEL)
  29. pub.subscribe(self.on_start, events.WINDOW_START)
  30. pub.subscribe(self.on_restart, events.WINDOW_RESTART)
  31. pub.subscribe(self.on_close, events.WINDOW_CLOSE)
  32. pub.subscribe(self.on_edit, events.WINDOW_EDIT)
  33. def on_edit(self):
  34. pub.send_message(events.WINDOW_CHANGE, view_name=views.CONFIG_SCREEN)
  35. def on_close(self):
  36. self.core_gui.Destroy()
  37. sys.exit()
  38. def on_restart(self):
  39. self.on_start()
  40. def manual_restart(self):
  41. self.on_start()
  42. def on_cancel(self):
  43. msg = i18n._('sure_you_want_to_exit')
  44. dlg = wx.MessageDialog(None, msg, i18n._('close_program'), wx.YES_NO)
  45. result = dlg.ShowModal()
  46. if result == YES:
  47. dlg.Destroy()
  48. self.core_gui.Destroy()
  49. sys.exit()
  50. dlg.Destroy()
  51. def on_start(self):
  52. if not self.skipping_config() and not self.required_section_complete():
  53. return self.show_dialog(i18n._('error_title'), i18n._('error_required_fields'), wx.ICON_ERROR)
  54. cmd_line_args = self.core_gui.GetOptions()
  55. command = '{0} --ignore-gooey {1}'.format(self.build_spec['target'], cmd_line_args)
  56. pub.send_message(events.WINDOW_CHANGE, view_name=views.RUNNING_SCREEN)
  57. self.run_client_code(command)
  58. def run_client_code(self, command):
  59. p = subprocess.Popen(command, bufsize=1, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
  60. pool = Pool(1)
  61. pool.apply_async(self.read_stdout, (p, self.process_result))
  62. def read_stdout(self, process, callback):
  63. while True:
  64. line = process.stdout.readline()
  65. if not line:
  66. break
  67. wx.CallAfter(self.core_gui.PublishConsoleMsg, line)
  68. wx.CallAfter(callback, process)
  69. def process_result(self, process):
  70. _stdout, _stderr = process.communicate()
  71. if process.returncode == 0:
  72. pub.send_message(events.WINDOW_CHANGE, view_name=views.SUCCESS_SCREEN)
  73. self.success_dialog()
  74. else:
  75. pub.send_message(events.WINDOW_CHANGE, view_name=views.ERROR_SCREEN)
  76. self.error_dialog(_stderr)
  77. def skipping_config(self):
  78. return self.build_spec['manual_start']
  79. def required_section_complete(self):
  80. required_section = self.core_gui.GetRequiredArgs()
  81. if len(required_section) == 0:
  82. return True # no requirements!
  83. return not any(req == '' for req in required_section)
  84. def success_dialog(self):
  85. self.show_dialog(i18n._("execution_finished"), i18n._('success_message'), wx.ICON_INFORMATION)
  86. def error_dialog(self, error_msg):
  87. self.show_dialog(i18n._('error_title'), i18n._('uh_oh').format(error_msg), wx.ICON_ERROR)
  88. def show_dialog(self, title, content, style):
  89. a = wx.MessageDialog(None, content, title, style)
  90. a.ShowModal()
  91. a.Destroy()