Beispiel #1
0
    def getPluginByName(self, name, category="Default", timeout=150):
        plugin = super(XicamPluginManager,
                       self).getPluginByName(name, category)

        if plugin:
            with load_timer() as elapsed:
                while not plugin.plugin_object:
                    # the plugin was loaded, but the instanciation event hasn't fired yet
                    if threads.is_main_thread():
                        QApplication.processEvents()
                    else:
                        time.sleep(0.01)
                    if elapsed() > timeout:
                        raise TimeoutError(
                            f"Plugin named {name} waited too long to instanciate"
                        )
            return plugin

        # if queueing
        if len(self.loadqueue):
            for load_item in list(self.loadqueue):
                if load_item[2].name == name:
                    self.loadqueue.remove(
                        load_item)  # remove the item from the top-level queue
                    msg.logMessage(f"Immediately loading {load_item[2].name}.",
                                   level=msg.INFO)
                    self.load_marked_plugin(
                        *load_item)  # and load it immediately
                    break

            # Run a wait loop until the plugin element is instanciated by main thread
            with load_timer() as elapsed:
                while (not plugin) or (not plugin.plugin_object):
                    plugin = super(XicamPluginManager,
                                   self).getPluginByName(name, category)
                    if threads.is_main_thread():
                        QApplication.processEvents()
                    else:
                        time.sleep(0.01)
                    if elapsed() > timeout:
                        raise TimeoutError(
                            f"Plugin named {name} waited too long to instanciate"
                        )

        return plugin
Beispiel #2
0
 def getPluginsOfCategory(self, category_name):
     while not self.loadcomplete:
         if threads.is_main_thread():
             QApplication.processEvents()
         else:
             time.sleep(0.01)
     plugins = super(XicamPluginManager,
                     self).getPluginsOfCategory(category_name)
     return [plugin for plugin in plugins if plugin.plugin_object]
Beispiel #3
0
    def get_plugin_by_name(self, name, type_name=None, timeout=10):
        """
        Find a collected plugin named `name`, optionally by also specifying the type of plugin.

        Parameters
        ----------
        name : str
            name of the plugin to get
        type_name : str
            type of the plugin to get (optional)

        Returns
        -------
        object
            the matching plugin object (may be a class or instance), or None if not found
        """
        return_plugin = self._get_plugin_by_name(name, type_name)

        if return_plugin:
            return return_plugin

        # If still actively collecting plugins
        if self.state != State.READY:
            # find the matching entrypoint
            entrypoint, type_name = self._get_entrypoint_by_name(
                name, type_name)

            if not entrypoint:
                raise NameError(
                    f'The plugin named {name} of type {type_name} could not be discovered. '
                    f'Check your installation integrity.')

            # Load it immediately; it will move to top of instantiate queue as well
            msg.logMessage(f"Immediately loading {entrypoint.name}.",
                           level=msg.INFO)
            self._load_plugin(type_name, entrypoint)

            # Add another instantiate event to the Qt event queue, so that it triggers in the next event loop
            threads.invoke_as_event(self._instantiate_plugin)

            # wait for it to load
            with load_timer() as elapsed:
                while not return_plugin:
                    return_plugin = self._get_plugin_by_name(name, type_name)
                    if threads.is_main_thread():
                        QApplication.processEvents()  #
                    else:
                        time.sleep(0.01)
                    if elapsed() > timeout:
                        raise TimeoutError(
                            f"Plugin named {name} waited too long to instantiate and timed out"
                        )

        return return_plugin
Beispiel #4
0
    def get_plugin_by_name(self, name, type_name=None, timeout=10):
        """
        Find a collected plugin named `name`, optionally by also specifying the type of plugin.

        Parameters
        ----------
        name : str
            name of the plugin to get
        type_name : str
            type of the plugin to get (optional)

        Returns
        -------
        object
            the matching plugin object (may be a class or instance), or None if not found
        """
        return_plugin = self._get_plugin_by_name(name, type_name)

        if return_plugin:
            return return_plugin

        # Find the matching plugin from the queue
        match_task = next(filter(lambda task: task.name==name and task.type_name == type_name, self._tasks), None)

        # If the matched task is already failed
        if match_task and match_task.status in [Status.FailedLoad, Status.FailedInstantiate]:
            raise NameError(f"The plugin named {name} of type {type_name} has already failed to load.")

        # Otherwise, prioritize it
        elif match_task:
            # If its queued to load, load it immediately in the main thread
            if match_task.status is Status.LoadingQueue:
                self._load_plugin(match_task)

            # If its queued to instantiate, instantiate it immediately
            if match_task.status is Status.InstantiateQueue:
                self._instantiate_plugin(match_task)

            # If the instantiate event chain isn't running, run it now
            if not self.instantiating:
                threads.invoke_as_event(self._instantiate_plugin)
                self.instantiating = True

        # Or, if there was no match
        else:
            raise NameError(
                f"The plugin named {name} of type {type_name} could not be discovered. "
                f"Check your installation integrity."
            )

        # wait for it to load
        with load_timer() as elapsed:
            while match_task.status not in [Status.Success, Status.FailedInstantiate, Status.FailedLoad]:
                if threads.is_main_thread():
                    from qtpy.QtWidgets import QApplication  # Required as late import to avoid loading Qt things too soon
                    QApplication.processEvents()
                else:
                    time.sleep(0.01)
                if elapsed() > timeout:
                    raise TimeoutError(f"Plugin named {name} waited too long to instantiate and timed out")

        if match_task.status in [Status.FailedInstantiate, Status.FailedLoad]:
            raise NameError(f"The plugin named {name} of type {type_name} failed to load while we were waiting for it.")

        elif match_task.status == Status.Success:
            return_plugin = self._get_plugin_by_name(name, type_name)

        return return_plugin