def __init__(self, view, exit_code, application='mantidplot', traceback=''): self.error_log = Logger("error") self._view = view self._exit_code = exit_code self._application = application self._traceback = traceback self._view.set_report_callback(self.error_handler) if not traceback: traceback_file_path = os.path.join( ConfigService.getAppDataDirectory(), '{}_stacktrace.txt'.format(application)) try: if os.path.isfile(traceback_file_path): with open(traceback_file_path, 'r') as file: self._traceback = file.readlines() new_workspace_name = os.path.join( ConfigService.getAppDataDirectory(), '{}_stacktrace_sent.txt'.format(application)) os.rename(traceback_file_path, new_workspace_name) except OSError: pass
def __init__(self, multifileinterpreter, main_window=None, globalfiguremanager=None): """ Project Recovery class is aimed at allowing you to recovery your workbench project should you crash for whatever reason :param multifileinterpreter: MultiPythonFileInterpreter; An object that is used in workbench to represent the python script editor :param main_window: A reference to the main window object to be used as a parent to the project recovery GUIs :param globalfiguremanager: Based on the globalfiguremanager object expects an object with a dictionary on cls/self.figs for the object passed here which contains all of the plots open/needed to be saved """ self._recovery_directory = os.path.join(ConfigService.getAppDataDirectory(), self.recovery_workbench_recovery_name) self._recovery_directory_hostname = os.path.join(self.recovery_directory, socket.gethostname()) self._recovery_directory_pid = os.path.join(self.recovery_directory_hostname, str(os.getpid())) self._recovery_order_workspace_history_file = os.path.join(ConfigService.getAppDataDirectory(), self.recovery_ordered_recovery_file_name) self.recovery_enabled = ("true" == ConfigService[RECOVERY_ENABLED_KEY].lower()) self.maximum_num_checkpoints = int(ConfigService[NO_OF_CHECKPOINTS_KEY]) self.time_between_saves = int(ConfigService[SAVING_TIME_KEY]) # seconds # The recovery GUI's presenter is set when needed self.recovery_presenter = None self.thread_on = False # Set to true by workbench on close to kill the thread on completion of project save self.closing_workbench = False # Recovery loader and saver self.loader = ProjectRecoveryLoader(self, multi_file_interpreter=multifileinterpreter, main_window=main_window) self.saver = ProjectRecoverySaver(self, globalfiguremanager)
def __init__(self, multifileinterpreter, main_window=None, globalfiguremanager=None): """ Project Recovery class is aimed at allowing you to recovery your workbench project should you crash for whatever reason :param multifileinterpreter: MultiPythonFileInterpreter; An object that is used in workbench to represent the python script editor :param main_window: A reference to the main window object to be used as a parent to the project recovery GUIs :param globalfiguremanager: Based on the globalfiguremanager object expects an object with a dictionary on cls/self.figs for the object passed here which contains all of the plots open/needed to be saved """ self._recovery_directory = os.path.join(ConfigService.getAppDataDirectory(), self.recovery_workbench_recovery_name) self._recovery_directory_hostname = os.path.join(self.recovery_directory, socket.gethostname()) self._recovery_directory_pid = os.path.join(self.recovery_directory_hostname, str(os.getpid())) self._recovery_order_workspace_history_file = os.path.join(ConfigService.getAppDataDirectory(), self.recovery_ordered_recovery_file_name) self.recovery_enabled = ("true" == ConfigService[RECOVERY_ENABLED_KEY].lower()) self.maximum_num_checkpoints = int(ConfigService[NO_OF_CHECKPOINTS_KEY]) self.time_between_saves = int(ConfigService[SAVING_TIME_KEY]) # seconds # The recovery GUI's presenter is set when needed self.recovery_presenter = None self.thread_on = False # Set to true by workbench on close to kill the thread on completion of project save self.closing_workbench = False # Recovery loader and saver self.loader = ProjectRecoveryLoader(self, multi_file_interpreter=multifileinterpreter, main_window=main_window) self.saver = ProjectRecoverySaver(self, globalfiguremanager)
def __init__(self, view, exit_code: str, application: str, traceback: Optional[str] = None): """ :param view: A reference to the view managed by this presenter :param exit_code: A string containing the exit_code of the failing application :param application: A string containing the failing application name :param traceback: An optional string containing a traceback dumped as JSON-encoded string """ self.error_log = Logger("errorreports") self._view = view self._exit_code = exit_code self._application = application self._traceback = traceback if traceback else '' self._view.set_report_callback(self.error_handler) self._view.moreDetailsButton.clicked.connect(self.show_more_details) if not traceback: traceback_file_path = os.path.join( ConfigService.getAppDataDirectory(), '{}_stacktrace.txt'.format(application)) try: if os.path.isfile(traceback_file_path): with open(traceback_file_path, 'r') as file: self._traceback = file.readlines() new_workspace_name = os.path.join( ConfigService.getAppDataDirectory(), '{}_stacktrace_sent.txt'.format(application)) os.rename(traceback_file_path, new_workspace_name) except OSError: pass
def test_constructor_settings_are_set(self): # Test the paths set in the constructor that are generated. self.assertEqual( self.pr.recovery_directory, os.path.join(ConfigService.getAppDataDirectory(), "workbench-recovery")) self.assertEqual( self.pr.recovery_directory_hostname, os.path.join(ConfigService.getAppDataDirectory(), "workbench-recovery", socket.gethostname())) self.assertEqual( self.pr.recovery_directory_pid, os.path.join(ConfigService.getAppDataDirectory(), "workbench-recovery", socket.gethostname(), str(os.getpid()))) self.assertEqual( self.pr.recovery_order_workspace_history_file, os.path.join(ConfigService.getAppDataDirectory(), "ordered_recovery.py")) # Test config service values self.assertEqual(self.pr.time_between_saves, int(ConfigService[SAVING_TIME_KEY])) self.assertEqual(self.pr.maximum_num_checkpoints, int(ConfigService[NO_OF_CHECKPOINTS_KEY])) self.assertEqual( self.pr.recovery_enabled, ("true" == ConfigService[RECOVERY_ENABLED_KEY].lower()))
def test_constructor_settings_are_set(self): # Test the paths set in the constructor that are generated. self.assertEqual(self.pr.recovery_directory, os.path.join(ConfigService.getAppDataDirectory(), "workbench-recovery")) self.assertEqual(self.pr.recovery_directory_hostname, os.path.join(ConfigService.getAppDataDirectory(), "workbench-recovery", socket.gethostname())) self.assertEqual(self.pr.recovery_directory_pid, os.path.join(ConfigService.getAppDataDirectory(), "workbench-recovery", socket.gethostname(), str(os.getpid()))) self.assertEqual(self.pr.recovery_order_workspace_history_file, os.path.join(ConfigService.getAppDataDirectory(), "ordered_recovery.py")) # Test config service values self.assertEqual(self.pr.time_between_saves, int(ConfigService[SAVING_TIME_KEY])) self.assertEqual(self.pr.maximum_num_checkpoints, int(ConfigService[NO_OF_CHECKPOINTS_KEY])) self.assertEqual(self.pr.recovery_enabled, ("true" == ConfigService[RECOVERY_ENABLED_KEY].lower()))
def start(options): """Main entry point for the application""" # Set the global figure manager in matplotlib. Very important this happens first. from workbench.plotting.config import init_mpl_gcf init_mpl_gcf() # cleanup static resources at exit atexit.register(qCleanupResources) # fix/validate arguments if options.script is not None: # convert into absolute path options.script = os.path.abspath(os.path.expanduser(options.script)) if not os.path.exists(options.script): print('script "{}" does not exist'.format(options.script)) options.script = None app = initialize() # the default sys check interval leads to long lags # when request scripts to be aborted setswitchinterval(SYSCHECK_INTERVAL) exit_value = 0 try: exit_value = start_workbench(app, options) except BaseException: # We count this as a crash import traceback # This is type of thing we want to capture and have reports # about. Prints to stderr as we can't really count on anything # else traceback.print_exc(file=ORIGINAL_STDERR) try: print_file_path = os.path.join(ConfigService.getAppDataDirectory(), STACKTRACE_FILE) with open(print_file_path, 'w') as print_file: traceback.print_exc(file=print_file) except OSError: pass exit_value = -1 finally: ORIGINAL_SYS_EXIT(exit_value)
def main(): """Main entry point for the application""" # setup command line arguments parser = argparse.ArgumentParser(description='Mantid Workbench') parser.add_argument('script', nargs='?') parser.add_argument('-x', '--execute', action='store_true', help='execute the script file given as argument') parser.add_argument( '-q', '--quit', action='store_true', help= 'execute the script file with \'-x\' given as argument and then exit') # TODO -a or --about: show about dialog and exit # TODO -d or --default-settings: start MantidPlot with the default settings # DONE -h or --help: show command line options <- free with command line parser # TODO -v or --version: print MantidPlot version and release date # TODO -r or --revision: print MantidPlot version and release date # TODO -s or --silent: start mantidplot without any setup dialogs # DONE -x or --execute: execute the script file given as argument # DONE -xq or --executeandquit: execute the script file given as argument and then exit MantidPlot # this is not a valid short command line option try: # set up bash completion as a soft dependency import argcomplete argcomplete.autocomplete(parser) except ImportError: pass # silently skip this # parse the command line options options = parser.parse_args() # TODO handle options that don't require starting the workbench e.g. --help --version # fix/validate arguments if options.script is not None: # convert into absolute path options.script = os.path.abspath(os.path.expanduser(options.script)) if not os.path.exists(options.script): # TODO should be logged print('script "{}" does not exist'.format(options.script)) options.script = None app = initialize() # the default sys check interval leads to long lags # when request scripts to be aborted setswitchinterval(SYSCHECK_INTERVAL) exit_value = 0 try: exit_value = start_workbench(app, options) except BaseException: # We count this as a crash import traceback # This is type of thing we want to capture and have reports # about. Prints to stderr as we can't really count on anything # else traceback.print_exc(file=ORIGINAL_STDERR) try: print_file_path = os.path.join(ConfigService.getAppDataDirectory(), STACKTRACE_FILE) with open(print_file_path, 'w') as print_file: traceback.print_exc(file=print_file) except OSError: pass exit_value = -1 finally: ORIGINAL_SYS_EXIT(exit_value)
def create_and_launch_workbench(app, command_line_options): """Given an application instance create the MainWindow, show it and start the main event loop """ exit_value = 0 try: # MainWindow needs to be imported locally to ensure the matplotlib # backend is not imported too early. from workbench.app.mainwindow import MainWindow # The ordering here is very delicate. Test thoroughly when # changing anything! main_window = MainWindow() # Set the mainwindow as the parent for additional QMainWindow instances from workbench.config import set_additional_windows_parent set_additional_windows_parent(main_window) # decorates the excepthook callback with the reference to the main window # this is used in case the user wants to terminate the workbench from the error window shown sys.excepthook = partial(exception_logger, main_window) # Load matplotlib as early as possible and set our defaults # Setup our custom backend and monkey patch in custom current figure manager main_window.set_splash('Preloading matplotlib') from workbench.plotting.config import initialize_matplotlib # noqa initialize_matplotlib() # Setup widget layouts etc. mantid.simple cannot be used before this # or the log messages don't get through to the widget main_window.setup() # start mantid main_window.set_splash('Initializing mantid framework') FrameworkManagerImpl.Instance() main_window.post_mantid_init() if main_window.splash: main_window.splash.hide() if command_line_options.script is not None: main_window.editor.open_file_in_new_tab( command_line_options.script) editor_task = None if command_line_options.execute: # if the quit flag is not specified, this task reference will be # GC'ed, and the task will be finished alongside the GUI startup editor_task = main_window.editor.execute_current_async() if command_line_options.quit: # wait for the code interpreter thread to finish executing the script editor_task.join() main_window.close() # for task exit code descriptions see the classes AsyncTask and TaskExitCode return int(editor_task.exit_code) if editor_task else 0 main_window.show() main_window.setWindowIcon(QIcon(':/images/MantidIcon.ico')) # Project Recovery on startup main_window.project_recovery.repair_checkpoints() if main_window.project_recovery.check_for_recover_checkpoint(): main_window.project_recovery.attempt_recovery() else: main_window.project_recovery.start_recovery_thread() if not (command_line_options.execute or command_line_options.quit): if AboutPresenter.should_show_on_startup(): AboutPresenter(main_window).show() # lift-off! exit_value = app.exec_() except BaseException: # We count this as a crash import traceback # This is type of thing we want to capture and have reports # about. Prints to stderr as we can't really count on anything # else traceback.print_exc(file=ORIGINAL_STDERR) try: print_file_path = os.path.join(ConfigService.getAppDataDirectory(), STACKTRACE_FILE) with open(print_file_path, 'w') as print_file: traceback.print_exc(file=print_file) except OSError: pass exit_value = -1 finally: ORIGINAL_SYS_EXIT(exit_value)
def get_properties_directory(): return ConfigService.getAppDataDirectory()
def get_properties_directory(): return ConfigService.getAppDataDirectory()