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