def show_notification(cls, notification: Notification): from ...utils.settings import get_settings settings = get_settings() # after https://github.com/napari/napari/issues/2370, # the os.getenv can be removed (and NAPARI_CATCH_ERRORS retired) if ( os.getenv("NAPARI_CATCH_ERRORS") not in ('0', 'False') and notification.severity >= settings.application.gui_notification_level ): application_instance = QApplication.instance() if application_instance: # Check if this is running from a thread if application_instance.thread() != QThread.currentThread(): dispatcher = getattr( application_instance, "_dispatcher", None ) if dispatcher: dispatcher.sig_notified.emit(notification) return cls.from_notification(notification).show()
def my_excepthook(type_, value, trace_back): """ Custom excepthook. base on base on :py:data:`state_store.show_error_dialog` decide if shown error dialog. """ # log the exception here if state_store.show_error_dialog and not isinstance(value, KeyboardInterrupt): if state_store.report_errors and parsed_version.is_devrelease: with sentry_sdk.push_scope() as scope: scope.set_tag("auto_report", "true") sentry_sdk.capture_exception(value) try: # noinspection PyUnresolvedReferences from qtpy.QtWidgets import QApplication if QApplication.instance(): from qtpy.QtCore import QMetaObject, Qt, QThread QApplication.instance().error = value if QThread.currentThread() != QApplication.instance().thread(): QMetaObject.invokeMethod(QApplication.instance(), "show_error", Qt.QueuedConnection) else: QApplication.instance().show_error() except ImportError: sys.__excepthook__(type_, value, trace_back) elif isinstance(value, KeyboardInterrupt): print("KeyboardInterrupt close", file=sys.stderr) sys.exit(1) else: # then call the default handler sys.__excepthook__(type_, value, trace_back)
def revert(self): """ Takes the data stored in the models and displays them in the widgets. """ # make sure it is called from main thread if (not QThread.currentThread() == QCoreApplication.instance( ).thread()): QTimer.singleShot(0, self.revert) return for key in self._mappings: self._on_model_notification(key)
def write(self, text): """ Override of the write function used to display output Args: text (str): Python output from stdout """ # Add thread name to the output when writting in the the widget current_thread = QThread.currentThread() thread_text = text + str(current_thread.name) self.terminal.write(str(text)) dir_path = current_thread.cicada_analysis.get_results_path() self.parent.normalOutputWritten(thread_text, dir_path)
def __call__(self, *args, **kwargs): """ If the current thread is the qApp thread then this performs a straight call to the wrapped callable_obj. Otherwise it invokes the do_call method as a slot via a BlockingQueuedConnection. """ if QThread.currentThread() == self.qApp.thread(): return self.callee(*args, **kwargs) else: self._store_function_args(*args, **kwargs) QMetaObject.invokeMethod(self, "on_call", Qt.BlockingQueuedConnection) if self._exc_info is not None: reraise(*self._exc_info) return self._result
def __call__(self, *args, **kwargs): """ If the current thread is the qApp thread then this performs a straight call to the wrapped callable_obj. Otherwise it invokes the do_call method as a slot via a BlockingQueuedConnection. """ if QThread.currentThread() == qApp.thread(): return self.callee(*args, **kwargs) else: self._store_function_args(*args, **kwargs) QMetaObject.invokeMethod(self, "on_call", Qt.BlockingQueuedConnection) if self._exc_info is not None: reraise(*self._exc_info) return self._result
def show_results(self): header_labels_set = False self.show_results_event.clear() t0 = time.monotonic() counter = 0 try: msg.showBusy() while not self._new_uids_queue.empty(): counter += 1 row = [] new_uid = self._new_uids_queue.get() try: entry = self.get_run_by_uid(new_uid) row_data = self.apply_search_result_row(entry) except SkipRow as e: msg.showMessage(str(msg)) msg.logError(e) continue if not header_labels_set: # Set header labels just once. threads.invoke_in_main_thread( self.search_results_model.setHorizontalHeaderLabels, list(row_data)) header_labels_set = True for value in row_data.values(): item = QStandardItem() item.setData(value, Qt.DisplayRole) item.setData(new_uid, Qt.UserRole) row.append(item) if QThread.currentThread().isInterruptionRequested(): self.show_results_event.set() msg.logMessage("Interrupt requested") return threads.invoke_in_main_thread( self.search_results_model.appendRow, row) if counter: self.sig_update_header.emit() duration = time.monotonic() - t0 msg.showMessage("Displayed {} new results {}.".format( counter, duration)) self.show_results_event.set() except Exception as e: msg.showMessage("Error displaying runs") msg.logError(e) finally: msg.hideBusy()
def submit(self): """ Submits the current values stored in the widgets to the models. """ # make sure it is called from main thread if (not QThread.currentThread() == QCoreApplication.instance( ).thread()): QTimer.singleShot(0, self.submit) return submit_policy = self._submit_policy self.submit_policy = SUBMIT_POLICY_AUTO try: for key in self._mappings: self._on_widget_property_notification(key) finally: self.submit_policy = submit_policy
def update_progressbar(self, time_started, increment_value=0, new_set_value=0): """ Args: time_started (float): Start time of the analysis increment_value (float): Value that should be added to the current value of the progress bar new_set_value (float): Value that should be set as the current value of the progress bar """ if self.gui: worker = QThread.currentThread() worker.setProgress(name=worker.name, time_started=time_started, increment_value=increment_value, new_set_value=new_set_value) else: pass
def update_progress_bar(self, time_started, increment_value=0, new_set_value=0): """ Update the progress bar in the analysis widget and the corresponding remaining time Args: time_started (float): Start time of the analysis increment_value (float): Value that should be added to the current value of the progress bar new_set_value (float): Value that should be set as the current value of the progress bar Returns: """ self.current_thread = QThread.currentThread() self.setEnabled(True) if new_set_value != 0: self.setValue(new_set_value) if increment_value != 0: self.setValue(self.value() + increment_value) if self.isEnabled() and self.value() != 0: if self.value() == 100: self.remaining_time_label.update_remaining_time(self.value(), time_started, True) else: self.remaining_time_label.update_remaining_time(self.value(), time_started)
def is_qapp_thread(): """Returns True iff a QApplication instance exists and the current thread matches the thread the QApplication was created in """ qapp = QApplication.instance() return qapp is None or (QThread.currentThread() == qapp.thread())