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.

257 lines
8.5 KiB

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