Example #1
0
    def register_message_listener(self, listener):
        """Register a object so the application dispatch messages to it.

        The message listener is registered using a Weak Reference, so the
        messages system will not keep the objects alive when all the other
        references are deleted.

        Parameters
        ----------
        listener : obj
            The object that will receive the message.

        Returns
        -------
        int
            An unique identifier to use when referencing the this listener in
            the messages system.

        Raises
        ------
        ValueError : If the listener object is already registered

        """
        #Check if the listener is in the list already
        for r in self._message_listeners.itervaluerefs():
            if r() is listener:
                raise ValueError("The object is already registered")

        id_ = find_free_key(self._message_listeners)
        self._message_listeners[id_] = listener
        return id_
Example #2
0
    def _load_plugin(self, plugin_name):
        # The `Plugin` class holds information about the plugin.
        # The loaded_ui_objects is a list of the loaded ui objects, but these
        # ui are those located in the plugin's `ui` dir, and their names are
        # local to the 'plugin scope', thus they have their own list.
        class Plugin(object):
            def __init__(slf, plugin_type, plugin_name, root_path, instance):
                slf.plugin_type       = plugin_type
                slf.plugin_name       = plugin_name
                slf.root_path         = root_path
                slf.instance          = instance
                slf.loaded_ui_objects = {}
                slf.gui_interface     = None
                slf.info              = None

        join = os.path.join
        listdir = os.listdir
        isdir = os.path.isdir
        isfile = os.path.isfile
        app = get_app()

        # A plugin folder should contain the following structure
        #
        # Plugin_Folder -> info.xml
        #               -> main.py
        #               -> ui      -> [.ui files only]

        # Get a list to the installed plugins
        plist = self._list_plugins()
        # Check if the plugin is in the list and get it
        p = [p_data for p_data in plist if p_data[1] == plugin_name]
        if not p:
            raise ValueError("There is no plugin '{}' installed".format(plugin_name))

        # Unpack the data
        ptype, pname, ppath, pinfo = p[0]

        # Check if there is a main.py file in the root folder
        main_path = join(ppath, "main.py")
        if not isfile(main_path):
            raise IOError("The plugin '{}' doesn't have a main module".format(plugin_name))

        # Load the module containing the main() function and call it to create
        # the plugin instance
        instance = None
        try:
            with open(main_path, "r") as f:
                #Load the plugin module
                plugin_module = imp.load_module("main", f, main_path, (".py", "r", imp.PY_SOURCE))
                #Call the main method to construct the plugin instance
                instance = plugin_module.main(ppath)
        except:
            raise

        # Create the plugin object that will be hold in the dictionary
        plugin = Plugin(ptype, pname, ppath, instance)
        # Find a free id
        id_ = find_free_key(self._loaded_plugins)
        # Save it to the dictionary and return the plugin object
        self._loaded_plugins[id_] = plugin

        app.post_message("plugin_loaded",
            {"id": id_, "type": plugin.plugin_type,
            "plugin": plugin}, -1)

        return (id_, plugin)