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.

109 lines
3.0 KiB

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