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.

268 lines
9.1 KiB

  1. """
  2. Primary orchestration and control point for Gooey.
  3. """
  4. import sys
  5. import wx
  6. from gooey.gui import cli
  7. from gooey.gui import events
  8. from gooey.gui import seeder
  9. from gooey.gui.components import modals
  10. from gooey.gui.components.config import ConfigPage, TabbedConfigPage
  11. from gooey.gui.components.console import Console
  12. from gooey.gui.components.footer import Footer
  13. from gooey.gui.components.header import FrameHeader
  14. from gooey.gui.components.menubar import MenuBar
  15. from gooey.gui.components.sidebar import Sidebar
  16. from gooey.gui.components.tabbar import Tabbar
  17. from gooey.gui.lang.i18n import _
  18. from gooey.gui.processor import ProcessController
  19. from gooey.gui.pubsub import pub
  20. from gooey.gui.util import wx_util
  21. from gooey.gui.util.wx_util import transactUI
  22. from gooey.python_bindings import constants
  23. class GooeyApplication(wx.Frame):
  24. """
  25. Main window for Gooey.
  26. """
  27. def __init__(self, buildSpec, *args, **kwargs):
  28. super(GooeyApplication, self).__init__(None, *args, **kwargs)
  29. self._state = {}
  30. self.buildSpec = buildSpec
  31. self.applyConfiguration()
  32. self.menu = MenuBar(buildSpec)
  33. self.SetMenuBar(self.menu)
  34. self.header = FrameHeader(self, buildSpec)
  35. self.configs = self.buildConfigPanels(self)
  36. self.navbar = self.buildNavigation()
  37. self.footer = Footer(self, buildSpec)
  38. self.console = Console(self, buildSpec)
  39. self.layoutComponent()
  40. self.clientRunner = ProcessController(
  41. self.buildSpec.get('progress_regex'),
  42. self.buildSpec.get('progress_expr'),
  43. self.buildSpec.get('hide_progress_msg'),
  44. self.buildSpec.get('encoding'),
  45. self.buildSpec.get('requires_shell'),
  46. )
  47. pub.subscribe(events.WINDOW_START, self.onStart)
  48. pub.subscribe(events.WINDOW_RESTART, self.onStart)
  49. pub.subscribe(events.WINDOW_STOP, self.onStopExecution)
  50. pub.subscribe(events.WINDOW_CLOSE, self.onClose)
  51. pub.subscribe(events.WINDOW_CANCEL, self.onCancel)
  52. pub.subscribe(events.WINDOW_EDIT, self.onEdit)
  53. pub.subscribe(events.CONSOLE_UPDATE, self.console.logOutput)
  54. pub.subscribe(events.EXECUTION_COMPLETE, self.onComplete)
  55. pub.subscribe(events.PROGRESS_UPDATE, self.footer.updateProgressBar)
  56. # Top level wx close event
  57. self.Bind(wx.EVT_CLOSE, self.onClose)
  58. if self.buildSpec['poll_external_updates']:
  59. self.fetchExternalUpdates()
  60. if self.buildSpec.get('auto_start', False):
  61. self.onStart()
  62. def applyConfiguration(self):
  63. self.SetTitle(self.buildSpec['program_name'])
  64. self.SetBackgroundColour(self.buildSpec.get('body_bg_color'))
  65. def onStart(self, *args, **kwarg):
  66. """
  67. Verify user input and kick off the client's program if valid
  68. """
  69. with transactUI(self):
  70. config = self.navbar.getActiveConfig()
  71. config.resetErrors()
  72. if config.isValid():
  73. if self.buildSpec['clear_before_run']:
  74. self.console.clear()
  75. self.clientRunner.run(self.buildCliString())
  76. self.showConsole()
  77. else:
  78. config.displayErrors()
  79. self.Layout()
  80. def onEdit(self):
  81. """Return the user to the settings screen for further editing"""
  82. with transactUI(self):
  83. if self.buildSpec['poll_external_updates']:
  84. self.fetchExternalUpdates()
  85. self.showSettings()
  86. def buildCliString(self):
  87. """
  88. Collect all of the required information from the config screen and
  89. build a CLI string which can be used to invoke the client program
  90. """
  91. config = self.navbar.getActiveConfig()
  92. group = self.buildSpec['widgets'][self.navbar.getSelectedGroup()]
  93. positional = config.getPositionalArgs()
  94. optional = config.getOptionalArgs()
  95. return cli.buildCliString(
  96. self.buildSpec['target'],
  97. group['command'],
  98. positional,
  99. optional
  100. )
  101. def onComplete(self, *args, **kwargs):
  102. """
  103. Display the appropriate screen based on the success/fail of the
  104. host program
  105. """
  106. with transactUI(self):
  107. if self.clientRunner.was_success():
  108. if self.buildSpec.get('return_to_config', False):
  109. self.showSettings()
  110. else:
  111. self.showSuccess()
  112. if self.buildSpec.get('show_success_modal', True):
  113. wx.CallAfter(modals.showSuccess)
  114. else:
  115. if self.clientRunner.wasForcefullyStopped:
  116. self.showForceStopped()
  117. else:
  118. self.showError()
  119. wx.CallAfter(modals.showFailure)
  120. def onStopExecution(self):
  121. """Displays a scary message and then force-quits the executing
  122. client code if the user accepts"""
  123. if self.buildSpec['show_stop_warning'] and modals.confirmForceStop():
  124. self.clientRunner.stop()
  125. def fetchExternalUpdates(self):
  126. """
  127. !Experimental!
  128. Calls out to the client code requesting seed values to use in the UI
  129. !Experimental!
  130. """
  131. seeds = seeder.fetchDynamicProperties(
  132. self.buildSpec['target'],
  133. self.buildSpec['encoding']
  134. )
  135. for config in self.configs:
  136. config.seedUI(seeds)
  137. def onCancel(self):
  138. """Close the program after confirming"""
  139. if modals.confirmExit():
  140. self.onClose()
  141. def onClose(self, *args, **kwargs):
  142. """Cleanup the top level WxFrame and shutdown the process"""
  143. self.Destroy()
  144. sys.exit()
  145. def layoutComponent(self):
  146. sizer = wx.BoxSizer(wx.VERTICAL)
  147. sizer.Add(self.header, 0, wx.EXPAND)
  148. sizer.Add(wx_util.horizontal_rule(self), 0, wx.EXPAND)
  149. sizer.Add(self.navbar, 1, wx.EXPAND)
  150. sizer.Add(self.console, 1, wx.EXPAND)
  151. sizer.Add(wx_util.horizontal_rule(self), 0, wx.EXPAND)
  152. sizer.Add(self.footer, 0, wx.EXPAND)
  153. self.SetMinSize((400, 300))
  154. self.SetSize(self.buildSpec['default_size'])
  155. self.SetSizer(sizer)
  156. self.console.Hide()
  157. self.Layout()
  158. self.SetIcon(wx.Icon(self.buildSpec['images']['programIcon'], wx.BITMAP_TYPE_ICO))
  159. def buildNavigation(self):
  160. """
  161. Chooses the appropriate layout navigation component based on user prefs
  162. """
  163. if self.buildSpec['navigation'] == constants.TABBED:
  164. navigation = Tabbar(self, self.buildSpec, self.configs)
  165. else:
  166. navigation = Sidebar(self, self.buildSpec, self.configs)
  167. if self.buildSpec['navigation'] == constants.HIDDEN:
  168. navigation.Hide()
  169. return navigation
  170. def buildConfigPanels(self, parent):
  171. page_class = TabbedConfigPage if self.buildSpec['tabbed_groups'] else ConfigPage
  172. return [page_class(parent, widgets, self.buildSpec)
  173. for widgets in self.buildSpec['widgets'].values()]
  174. def showSettings(self):
  175. self.navbar.Show(True)
  176. self.console.Show(False)
  177. self.header.setImage('settings_img')
  178. self.header.setTitle(_("settings_title"))
  179. self.header.setSubtitle(self.buildSpec['program_description'])
  180. self.footer.showButtons('cancel_button', 'start_button')
  181. self.footer.progress_bar.Show(False)
  182. def showConsole(self):
  183. self.navbar.Show(False)
  184. self.console.Show(True)
  185. self.header.setImage('running_img')
  186. self.header.setTitle(_("running_title"))
  187. self.header.setSubtitle(_('running_msg'))
  188. self.footer.showButtons('stop_button')
  189. self.footer.progress_bar.Show(True)
  190. if not self.buildSpec['progress_regex']:
  191. self.footer.progress_bar.Pulse()
  192. def showComplete(self):
  193. self.navbar.Show(False)
  194. self.console.Show(True)
  195. buttons = (['edit_button', 'restart_button', 'close_button']
  196. if self.buildSpec.get('show_restart_button', True)
  197. else ['edit_button', 'close_button'])
  198. self.footer.showButtons(*buttons)
  199. self.footer.progress_bar.Show(False)
  200. def showSuccess(self):
  201. self.showComplete()
  202. self.header.setImage('check_mark')
  203. self.header.setTitle(_('finished_title'))
  204. self.header.setSubtitle(_('finished_msg'))
  205. self.Layout()
  206. def showError(self):
  207. self.showComplete()
  208. self.header.setImage('error_symbol')
  209. self.header.setTitle(_('finished_title'))
  210. self.header.setSubtitle(_('finished_error'))
  211. def showForceStopped(self):
  212. self.showComplete()
  213. if self.buildSpec.get('force_stop_is_error', True):
  214. self.showError()
  215. else:
  216. self.showSuccess()
  217. self.header.setSubtitle(_('finished_forced_quit'))