示例#1
0
    def __init__(self, core):
        self.core = core
        self.log = core.log

        # cached modules (type, name)
        self.modules = {}
        # match history to speedup parsing (type, name)
        self.history = []

        #register for import addon
        sys.meta_path.append(self)

        # add to path, so we can import from userplugins
        sys.path.append(abspath(""))
        self.loader = LoaderFactory(
            PluginLoader(abspath(self.LOCALROOT), self.LOCALROOT,
                         self.core.config),
            PluginLoader(abspath(join(pypath, "pyload", "plugins")), self.ROOT,
                         self.core.config))

        self.loader.checkVersions()

        # plugin matcher to overwrite some behaviour
        self.matcher = []
示例#2
0
    def __init__(self, core):
        self.core = core
        self.log = core.log

        # cached modules (type, name)
        self.modules = {}
        # match history to speedup parsing (type, name)
        self.history = []

        #register for import addon
        sys.meta_path.append(self)

        # add to path, so we can import from userplugins
        sys.path.append(abspath(""))
        self.loader = LoaderFactory(PluginLoader(abspath(self.LOCALROOT), self.LOCALROOT, self.core.config),
                                    PluginLoader(abspath(join(pypath, "pyload", "plugins")), self.ROOT,
                                                 self.core.config))

        self.loader.checkVersions()

        # plugin matcher to overwrite some behaviour
        self.matcher = []
示例#3
0
class PluginManager:
    ROOT = "pyload.plugins"
    LOCALROOT = "localplugins"

    MATCH_HISTORY = 10
    DEFAULT_PLUGIN = "BasePlugin"

    def __init__(self, core):
        self.core = core
        self.log = core.log

        # cached modules (type, name)
        self.modules = {}
        # match history to speedup parsing (type, name)
        self.history = []

        #register for import addon
        sys.meta_path.append(self)

        # add to path, so we can import from userplugins
        sys.path.append(abspath(""))
        self.loader = LoaderFactory(
            PluginLoader(abspath(self.LOCALROOT), self.LOCALROOT,
                         self.core.config),
            PluginLoader(abspath(join(pypath, "pyload", "plugins")), self.ROOT,
                         self.core.config))

        self.loader.checkVersions()

        # plugin matcher to overwrite some behaviour
        self.matcher = []

    def addMatcher(self, matcher, index=0):
        """ Inserts matcher at given index, first position by default """
        if not isinstance(matcher, PluginMatcher):
            raise TypeError(
                "Expected type of PluginMatcher, got '%s' instead" %
                type(matcher))

        if matcher in self.matcher:
            self.matcher.remove(matcher)

        self.matcher.insert(index, matcher)

    def removeMatcher(self, matcher):
        """ Removes a matcher if it exists """
        if matcher in self.matcher:
            self.matcher.remove(matcher)

    def parseUrls(self, urls):
        """parse plugins for given list of urls, separate to crypter and hoster"""

        res = {"hoster": [], "crypter": []}  # tupels of (url, plugin)

        for url in urls:
            if type(url) not in (str, unicode, buffer):
                self.log.debug("Parsing invalid type %s" % type(url))
                continue

            found = False

            # search the history
            for ptype, name in self.history:
                if self.loader.getPlugin(ptype, name).re.match(url):
                    res[ptype].append((url, name))
                    found = (ptype, name)
                    break  # need to exit this loop first

            if found:  # found match
                if self.history[0] != found:  #update history
                    self.history.remove(found)
                    self.history.insert(0, found)
                continue

            # matcher are tried secondly, they won't go to history
            for m in self.matcher:
                match = m.matchURL(url)
                if match and match[0] in res:
                    ptype, name = match
                    res[ptype].append((url, name))
                    found = True
                    break

            if found:
                continue

            for ptype in ("crypter", "hoster"):
                for loader in self.loader:
                    for name, plugin in loader.getPlugins(ptype).iteritems():
                        if plugin.re.match(url):
                            res[ptype].append((url, name))
                            self.history.insert(0, (ptype, name))
                            del self.history[
                                self.MATCH_HISTORY:]  # cut down to size
                            found = True
                            break
                    if found: break
                if found: break

            if not found:
                res["hoster"].append((url, self.DEFAULT_PLUGIN))

        return res["hoster"], res["crypter"]

    def findPlugin(self, name):
        """ Finds the type to a plugin name """
        return self.loader.findPlugin(name)

    def getPlugin(self, plugin, name):
        """ Retrieves the plugin tuple for a single plugin or none """
        return self.loader.getPlugin(plugin, name)

    def getPlugins(self, plugin):
        """  Get all plugins of a certain type in a dict """
        plugins = {}
        for loader in self.loader:
            plugins.update(loader.getPlugins(plugin))
        return plugins

    def getPluginClass(self, plugin, name, overwrite=True):
        """Gives the plugin class of a hoster or crypter plugin

        :param overwrite: allow the use of overwritten plugins
        """
        if overwrite:
            for m in self.matcher:
                match = m.matchPlugin(plugin, name)
                if match:
                    plugin, name = match

        return self.loadClass(plugin, name)

    def loadAttributes(self, plugin, name):
        for loader in self.loader:
            if loader.hasPlugin(plugin, name):
                return loader.loadAttributes(plugin, name)

        return {}

    def loadModule(self, plugin, name):
        """ Returns loaded module for plugin

        :param plugin: plugin type, subfolder of module.plugins
        """
        if (plugin, name) in self.modules: return self.modules[(plugin, name)]
        for loader in self.loader:
            if loader.hasPlugin(plugin, name):
                try:
                    module = loader.loadModule(plugin, name)
                    # cache import
                    self.modules[(plugin, name)] = module
                    return module
                except Exception, e:
                    self.log.error(
                        _("Error importing %(name)s: %(msg)s") % {
                            "name": name,
                            "msg": str(e)
                        })
                    self.core.print_exc()
示例#4
0
class PluginManager:
    ROOT = "pyload.plugins"
    LOCALROOT = "localplugins"

    MATCH_HISTORY = 10
    DEFAULT_PLUGIN = "BasePlugin"

    def __init__(self, core):
        self.core = core
        self.log = core.log

        # cached modules (type, name)
        self.modules = {}
        # match history to speedup parsing (type, name)
        self.history = []

        #register for import addon
        sys.meta_path.append(self)

        # add to path, so we can import from userplugins
        sys.path.append(abspath(""))
        self.loader = LoaderFactory(PluginLoader(abspath(self.LOCALROOT), self.LOCALROOT, self.core.config),
                                    PluginLoader(abspath(join(pypath, "pyload", "plugins")), self.ROOT,
                                                 self.core.config))

        self.loader.checkVersions()

        # plugin matcher to overwrite some behaviour
        self.matcher = []

    def addMatcher(self, matcher, index=0):
        """ Inserts matcher at given index, first position by default """
        if not isinstance(matcher, PluginMatcher):
            raise TypeError("Expected type of PluginMatcher, got %s instead" % type(matcher))

        if matcher in self.matcher:
            self.matcher.remove(matcher)

        self.matcher.insert(index, matcher)

    def removeMatcher(self, matcher):
        """ Removes a matcher if it exists """
        if matcher in self.matcher:
            self.matcher.remove(matcher)

    def parseUrls(self, urls):
        """parse plugins for given list of urls, separate to crypter and hoster"""

        res = {"hoster": [], "crypter": []} # tupels of (url, plugin)

        for url in urls:
            if type(url) not in (str, unicode, buffer):
                self.log.debug("Parsing invalid type %s" % type(url))
                continue

            found = False

            # search the history
            for ptype, name in self.history:
                if self.loader.getPlugin(ptype, name).re.match(url):
                    res[ptype].append((url, name))
                    found = (ptype, name)
                    break # need to exit this loop first

            if found:  # found match
                if self.history[0] != found: #update history
                    self.history.remove(found)
                    self.history.insert(0, found)
                continue


            # matcher won't go to history
            for m in self.matcher:
                match = m.matchURL(url)
                if match and match[0] in res:
                    ptype, name = match
                    res[ptype].append((url, name))
                    found = True
                    break

            if found:
                continue

            for ptype in ("crypter", "hoster"):
                for loader in self.loader:
                    for name, plugin in loader.getPlugins(ptype).iteritems():
                        if plugin.re.match(url):
                            res[ptype].append((url, name))
                            self.history.insert(0, (ptype, name))
                            del self.history[self.MATCH_HISTORY:] # cut down to size of 10
                            found = True
                            break
                    if found: break
                if found: break

            if not found:
                res["hoster"].append((url, self.DEFAULT_PLUGIN))

        return res["hoster"], res["crypter"]

    def getPlugins(self, plugin):
        """  Get all plugins of a certain type in a dict """
        plugins = {}
        for loader in self.loader:
            plugins.update(loader.getPlugins(plugin))
        return plugins

    def getPluginClass(self, plugin, name, overwrite=True):
        """Gives the plugin class of a hoster or crypter plugin

        :param overwrite: allow the use of overwritten plugins
        """
        if overwrite:
            for m in self.matcher:
                match = m.matchPlugin(plugin, name)
                if match:
                    plugin, name = match

        return self.loadClass(plugin, name)

    def loadAttributes(self, plugin, name):
        for loader in self.loader:
            if loader.hasPlugin(plugin, name):
                return loader.loadAttributes(plugin, name)

        return {}

    def loadModule(self, plugin, name):
        """ Returns loaded module for plugin

        :param plugin: plugin type, subfolder of module.plugins
        """
        if (plugin, name) in self.modules: return self.modules[(plugin, name)]
        for loader in self.loader:
            if loader.hasPlugin(plugin, name):
                try:
                    module = loader.loadModule(plugin, name)
                    # cache import
                    self.modules[(plugin, name)] = module
                    return module
                except Exception, e:
                    self.log.error(_("Error importing %(name)s: %(msg)s") % {"name": name, "msg": str(e)})
                    self.core.print_exc()