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.

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