Exemplo n.º 1
0
    def load_plugin(plugin, prefix = None):
        """
Load and register all plugins for the given plugin name and prefix (defaults
to "dNG.plugins").

:param plugin: Plugin name
:param prefix: Plugin name prefix

:return: (bool) True on success
:since:  v0.2.00
        """

        # pylint: disable=no-member

        _return = False

        if (prefix is None): prefix = "dNG.plugins"
        package = "{0}.{1}".format(prefix, plugin)

        if (NamedLoader._load_package(package) is not None):
            package_path = path.join(Manager._get_loader().get_base_dir(), package.replace(".", path.sep))
            if (not os.access(package_path, os.R_OK)): package_path = None
        else: package_path = None

        if (package_path is not None and path.isdir(package_path)):
            for dir_entry in os.listdir(package_path):
                if (dir_entry.endswith(".py") and dir_entry != "__init__.py"):
                    module_name = "{0}.{1}".format(package, dir_entry[:-3])
                    module = NamedLoader._load_module(module_name)

                    if (module is not None and hasattr(module, "register_plugin")):
                        with ExceptionLogTrap("pas_plugins"):
                            if (package not in Manager._plugins): Manager._plugins[package] = [ ]
                            if (module_name not in Manager._plugins[package]): Manager._plugins[package].append(module_name)

                            module.register_plugin()
                            _return = True
                        #
                    #
                #
            #
        #

        return _return