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.

158 lines
5.1 KiB

9 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
8 years ago
8 years ago
  1. import sys
  2. from gooey.gui.processor import ProcessController
  3. from gooey.gui.model import States
  4. from gooey.gui.pubsub import pub
  5. from gooey.gui import events
  6. class Presenter(object):
  7. def __init__(self, view, model):
  8. self.view = view
  9. self.model = model
  10. self.client_runner = ProcessController(
  11. self.model.progress_regex,
  12. self.model.progress_expr
  13. )
  14. pub.subscribe(self.on_cancel, events.WINDOW_CANCEL)
  15. pub.subscribe(self.on_stop, events.WINDOW_STOP)
  16. pub.subscribe(self.on_start, events.WINDOW_START)
  17. pub.subscribe(self.on_restart, events.WINDOW_RESTART)
  18. pub.subscribe(self.on_edit, events.WINDOW_EDIT)
  19. pub.subscribe(self.on_close, events.WINDOW_CLOSE)
  20. # console statuses from the other thread
  21. pub.subscribe(self.on_new_message, 'console_update')
  22. pub.subscribe(self.on_progress_change, 'progress_update')
  23. pub.subscribe(self.on_client_done, 'execution_complete')
  24. def initialize_view(self):
  25. self.view.window_title = self.model.program_name
  26. self.view.window_size = self.model.default_size
  27. self.view.required_section.populate(self.model.required_args)
  28. self.view.optional_section.populate(self.model.optional_args)
  29. if self.model.use_monospace_font:
  30. self.view.set_display_font_style('monospace')
  31. if self.should_disable_stop_button():
  32. self.view.disable_stop_button()
  33. else:
  34. self.view.enable_stop_button()
  35. self.syncronize_from_model()
  36. def update_model(self):
  37. self.update_list(self.model.required_args, self.view.required_section.get_values())
  38. self.update_list(self.model.optional_args, self.view.optional_section.get_values())
  39. self.syncronize_from_model()
  40. def syncronize_from_model(self):
  41. # update heading titles
  42. self.view.heading_title = self.model.heading_title
  43. self.view.heading_subtitle = self.model.heading_subtitle
  44. if not self.model.stop_button_disabled:
  45. self.view.enable_stop_button()
  46. # refresh the widgets
  47. for index, widget in enumerate(self.view.required_section):
  48. widget.set_value(self.model.required_args[index]._value)
  49. for index, widget in enumerate(self.view.optional_section):
  50. widget.set_value(self.model.optional_args[index]._value)
  51. # swap the views
  52. getattr(self, self.model.current_state)()
  53. def should_disable_stop_button(self):
  54. return self.model.stop_button_disabled
  55. def on_start(self):
  56. self.update_model()
  57. if not self.model.is_valid():
  58. return self.view.show_missing_args_dialog()
  59. command = self.model.build_command_line_string()
  60. self.client_runner.run(command)
  61. self.model.update_state(States.RUNNNING)
  62. self.syncronize_from_model()
  63. def on_stop(self):
  64. self.ask_stop()
  65. def on_edit(self):
  66. self.model.update_state(States.CONFIGURING)
  67. self.syncronize_from_model()
  68. def on_restart(self):
  69. self.on_start()
  70. def on_cancel(self):
  71. if self.view.confirm_exit_dialog():
  72. self.view.Destroy()
  73. sys.exit()
  74. def on_close(self):
  75. self.view.Destroy()
  76. sys.exit()
  77. def on_new_message(self, msg):
  78. # observes changes coming from the subprocess
  79. self.view.update_console_async(msg)
  80. def on_progress_change(self, progress):
  81. # observes changes coming from the subprocess
  82. self.view.update_progress_aync(progress)
  83. def on_client_done(self):
  84. if self.client_runner.was_success():
  85. self.model.update_state(States.SUCCESS)
  86. else:
  87. self.model.update_state(States.ERROR)
  88. self.syncronize_from_model()
  89. def ask_stop(self):
  90. if self.view.confirm_stop_dialog():
  91. self.stop()
  92. return True
  93. return False
  94. def stop(self):
  95. self.client_runner.stop()
  96. def configuring(self):
  97. self.view.hide_all_buttons()
  98. self.view.hide('check_mark', 'running_img', 'error_symbol', 'runtime_display')
  99. self.view.show('settings_img', 'cancel_button', 'start_button', 'config_panel')
  100. self.view.Layout()
  101. def running(self):
  102. self.view.hide_all_buttons()
  103. self.view.hide('check_mark', 'settings_img', 'error_symbol', 'config_panel')
  104. self.view.show('running_img', 'stop_button', 'progress_bar', 'runtime_display')
  105. self.view.progress_bar.Pulse()
  106. self.view.Layout()
  107. def success(self):
  108. self.view.hide_all_buttons()
  109. self.view.hide('running_img', 'progress_bar', 'config_panel')
  110. self.view.show('check_mark', 'edit_button', 'restart_button', 'close_button', 'runtime_display')
  111. self.view.Layout()
  112. def error(self):
  113. self.view.hide_all_buttons()
  114. self.view.hide('running_img', 'progress_bar', 'config_panel')
  115. self.view.show('error_symbol', 'edit_button', 'restart_button', 'close_button', 'runtime_display')
  116. self.view.Layout()
  117. @staticmethod
  118. def partition(collection, condition):
  119. return filter(condition, collection), filter(lambda x: not condition(x), collection)
  120. def update_list(self, collection, new_values):
  121. # convenience method for syncronizing the model -> widget list collections
  122. for index, val in enumerate(new_values):
  123. collection[index].value = val