def __init__(self, script, widget=None, close_on_finish=True, pause=0, is_cli=False): """ Initialise a runner. :param script: The script to run. :param widget: The widget to test. :param close_on_finish: If true close the widget after the script has finished. :param is_cli: If true the script is to be run from a command line tool. Exceptions are treated slightly differently in this case. """ app = get_application() self.script = script self.widget = widget self.close_on_finish = close_on_finish self.pause = pause self.is_cli = is_cli self.error = None self.script_iter = [None] self.pause_timer = QTimer(app) self.pause_timer.setSingleShot(True) self.script_timer = QTimer(app)
def __init__(self, script, widget=None, close_on_finish=True, pause=0, is_cli=False): """ Initialise a runner. :param script: The script to run. :param widget: The widget to test. :param close_on_finish: If true close the widget after the script has finished. :param is_cli: If true the script is to be run from a command line tool. Exceptions are treated slightly differently in this case. """ app = get_application() self.script = script self.widget = widget self.close_on_finish = close_on_finish self.pause = pause self.is_cli = is_cli self.error = None self.script_iter = [None] self.pause_timer = QTimer(app) self.pause_timer.setSingleShot(True) self.script_timer = QTimer(app)
def __call__(self): app = get_application() if not self.pause_timer.isActive(): try: script_iter = self.script_iter[-1] if script_iter is None: if self.close_on_finish: app.closeAllWindows() app.exit() return # Run test script until the next 'yield' try: ret = next(script_iter) except ValueError: return while ret is not None: if inspect.isgenerator(ret): self.script_iter.append(ret) ret = None elif isinstance(ret, six.integer_types) or isinstance( ret, float): # Start non-blocking pause in seconds self.pause_timer.start(int(ret * 1000)) ret = None else: ret = ret() except StopIteration: if len(self.script_iter) > 1: self.script_iter.pop() else: self.script_iter = [None] self.script_timer.stop() if self.close_on_finish: app.closeAllWindows() app.exit(0) except Exception as e: self.script_iter = [None] traceback.print_exc() if self.close_on_finish: app.exit(1) self.error = e
def __call__(self): app = get_application() if not self.pause_timer.isActive(): try: script_iter = self.script_iter[-1] if script_iter is None: if self.close_on_finish: app.closeAllWindows() app.exit() return # Run test script until the next 'yield' try: ret = next(script_iter) except ValueError: return while ret is not None: if inspect.isgenerator(ret): self.script_iter.append(ret) ret = None elif isinstance(ret, six.integer_types) or isinstance(ret, float): # Start non-blocking pause in seconds self.pause_timer.start(int(ret * 1000)) ret = None else: ret = ret() except StopIteration: if len(self.script_iter) > 1: self.script_iter.pop() else: self.script_iter = [None] self.script_timer.stop() if self.close_on_finish: app.closeAllWindows() app.exit(0) except Exception as e: self.script_iter = [None] traceback.print_exc() if self.close_on_finish: app.exit(1) self.error = e
def open_in_window(widget_or_name, script, attach_debugger=True, pause=0, close_on_finish=False, is_cli=False, in_workbench=False): """ Displays a widget in a window. :param widget_or_name: A widget to display. 1. If a string it's a qualified name of a widget, eg mantidqt.mywidget.MyWidget 2. If a callable it must take no arguments and return a QWidget 3. If a QWidget it's used directly. :param script: A script to run after the widget is open. 1. If a string it's a qualified name of a test function that can be run after the widget is created. 2. If a callable it's used directly. The test function must have the signature: def test(widget): ... where argument widget is an instance of the tested widget. The test function can yield from time to time after which the widget can update itself. This will make the test non-blocking and changes can be viewed as the script runs. If the test yields an number it is interpreted as the number of seconds to wait until the next step. The test can yield a generator. In this case it will be iterated over until it stops and the iterations of the main script continue. :param attach_debugger: If true pause to let the user to attache a debugger before starting application. :param pause: A number of seconds to wait between the iterations. :param close_on_finish: An option to close the widget after the script finishes. :param is_cli: If true the script is to be run from a command line tool. Exceptions are treated slightly differently in this case. :param in_workbench: Set to True if the script will be run inside the workbench application. """ if attach_debugger: input('Please attach the Debugger now if required. Press any key to continue') app = get_application() if widget_or_name is not None: widget_name = 'Widget to test' if isinstance(widget_or_name, six.string_types): widget = create_widget(widget_or_name) widget_name = widget_or_name elif isinstance(widget_or_name, QWidget): widget = widget_or_name else: widget = widget_or_name() if hasattr(widget, 'setWindowTitle'): widget.setWindowTitle(widget_name) if widget is not None: widget.show() else: widget = None script_runner = None if script is not None: try: script_runner = ScriptRunner(script, widget, close_on_finish=close_on_finish, is_cli=is_cli, pause=pause) script_runner.run() except Exception as e: if not is_cli: raise e if not in_workbench: ret = app.exec_() if not is_cli and script_runner is not None and script_runner.error is not None: raise script_runner.error else: ret = 0 return ret
def open_in_window(widget_or_name, script, attach_debugger=True, pause=0, close_on_finish=False, is_cli=False, in_workbench=False): """ Displays a widget in a window. :param widget_or_name: A widget to display. 1. If a string it's a qualified name of a widget, eg mantidqt.mywidget.MyWidget 2. If a callable it must take no arguments and return a QWidget 3. If a QWidget it's used directly. :param script: A script to run after the widget is open. 1. If a string it's a qualified name of a test function that can be run after the widget is created. 2. If a callable it's used directly. The test function must have the signature: def test(widget): ... where argument widget is an instance of the tested widget. The test function can yield from time to time after which the widget can update itself. This will make the test non-blocking and changes can be viewed as the script runs. If the test yields an number it is interpreted as the number of seconds to wait until the next step. The test can yield a generator. In this case it will be iterated over until it stops and the iterations of the main script continue. :param attach_debugger: If true pause to let the user to attache a debugger before starting application. :param pause: A number of seconds to wait between the iterations. :param close_on_finish: An option to close the widget after the script finishes. :param is_cli: If true the script is to be run from a command line tool. Exceptions are treated slightly differently in this case. :param in_workbench: Set to True if the script will be run inside the workbench application. """ if attach_debugger: raw_input('Please attach the Debugger now if required. Press any key to continue') app = get_application() if widget_or_name is not None: widget_name = 'Widget to test' if isinstance(widget_or_name, six.string_types): widget = create_widget(widget_or_name) widget_name = widget_or_name elif isinstance(widget_or_name, QWidget): widget = widget_or_name else: widget = widget_or_name() if hasattr(widget, 'setWindowTitle'): widget.setWindowTitle(widget_name) if widget is not None: widget.show() else: widget = None script_runner = None if script is not None: try: script_runner = ScriptRunner(script, widget, close_on_finish=close_on_finish, is_cli=is_cli, pause=pause) script_runner.run() except Exception as e: if not is_cli: raise e if not in_workbench: ret = app.exec_() if not is_cli and script_runner is not None and script_runner.error is not None: raise script_runner.error else: ret = 0 return ret