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.

102 lines
2.8 KiB

  1. '''
  2. Created on Dec 22, 2013
  3. @author: Chris
  4. '''
  5. import wx
  6. import sys
  7. import subprocess
  8. from multiprocessing.dummy import Pool
  9. from gooey.gui.lang import i18n
  10. YES = 5103
  11. NO = 5104
  12. class Controller(object):
  13. '''
  14. Main controller for the gui.
  15. All controlls are delegated to this central control point.
  16. '''
  17. def __init__(self, base_frame, build_spec):
  18. '''
  19. :type base_frame: BaseWindow
  20. :type build_spec: dict
  21. '''
  22. self.core_gui = base_frame
  23. self.build_spec = build_spec
  24. def OnCancelButton(self, widget, event):
  25. msg = i18n.translate('sure_you_want_to_exit')
  26. dlg = wx.MessageDialog(None, msg, i18n.translate('close_program'), wx.YES_NO)
  27. result = dlg.ShowModal()
  28. if result == YES:
  29. dlg.Destroy()
  30. self.core_gui.Destroy()
  31. sys.exit()
  32. dlg.Destroy()
  33. def OnStartButton(self, widget, event):
  34. cmd_line_args = self.core_gui.GetOptions()
  35. if not self.build_spec['manual_start']:
  36. _required = self.core_gui.GetRequiredArgs()
  37. if _required and any(req == '' for req in _required):
  38. self.ShowDialog(i18n.translate('error_title'), "Must fill in all fields in the Required section!", wx.ICON_ERROR)
  39. return
  40. command = '{0} {1}'.format(self.build_spec['target'], cmd_line_args)
  41. self.core_gui.NextPage()
  42. self.RunClientCode(command)
  43. def RunClientCode(self, command):
  44. def doInBackground(process, callback):
  45. while True:
  46. line = process.stdout.readline()
  47. if not line:
  48. break
  49. wx.CallAfter(self.core_gui.PublishConsoleMsg, line)
  50. wx.CallAfter(callback, process)
  51. p = subprocess.Popen(command, bufsize=1, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
  52. _pool = Pool(1)
  53. _pool.apply_async(doInBackground, (p, self.HandleResult))
  54. def HandleResult(self, process):
  55. _stdout, _stderr = process.communicate()
  56. if process.returncode == 0:
  57. self.core_gui.NextPage()
  58. self.ShowGoodFinishedDialog()
  59. else:
  60. self.core_gui.NextPage()
  61. self.ShowBadFinishedDialog(_stderr)
  62. def OnRestartButton(self, widget, event):
  63. self.OnStartButton(None, event)
  64. def ManualStart(self):
  65. self.OnStartButton(None, None)
  66. def OnCloseButton(self, widget, event):
  67. self.core_gui.Destroy()
  68. sys.exit()
  69. def ShowGoodFinishedDialog(self):
  70. self.ShowDialog(i18n.translate("execution_finished"),
  71. i18n.translate('success_message'),
  72. wx.ICON_INFORMATION)
  73. def ShowBadFinishedDialog(self, error_msg):
  74. msg = i18n.translate('uh_oh').format(error_msg)
  75. self.ShowDialog(i18n.translate('error_title'), msg, wx.ICON_ERROR)
  76. def ShowDialog(self, title, content, style):
  77. a = wx.MessageDialog(None, content, title, style)
  78. a.ShowModal()
  79. a.Destroy()