From 5060e4c6374a5116d0d8b02528f910f8c5f8bcf9 Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Tue, 15 Jul 2014 16:25:41 +0200 Subject: Fix code indentation, some bad whitespaces and missing authors + use 'not' instead 'is None' + replace __pattern__'s r" with r' + other minor cosmetics --- module/plugins/PluginManager.py | 1 + 1 file changed, 1 insertion(+) (limited to 'module/plugins/PluginManager.py') diff --git a/module/plugins/PluginManager.py b/module/plugins/PluginManager.py index e976b2c4a..b290c2746 100644 --- a/module/plugins/PluginManager.py +++ b/module/plugins/PluginManager.py @@ -29,6 +29,7 @@ from traceback import print_exc from module.lib.SafeEval import const_eval as literal_eval from module.ConfigParser import IGNORE + class PluginManager: ROOT = "module.plugins." USERROOT = "userplugins." -- cgit v1.2.3 From a1495eb2f9502fdf29974458845f928025bedf53 Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Sat, 26 Jul 2014 02:36:15 +0200 Subject: Prefer single quote for dict key --- module/plugins/PluginManager.py | 60 ++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 30 deletions(-) (limited to 'module/plugins/PluginManager.py') diff --git a/module/plugins/PluginManager.py b/module/plugins/PluginManager.py index b290c2746..adfa5845e 100644 --- a/module/plugins/PluginManager.py +++ b/module/plugins/PluginManager.py @@ -65,14 +65,14 @@ class PluginManager: f = open(join("userplugins", "__init__.py"), "wb") f.close() - self.plugins["crypter"] = self.crypterPlugins = self.parse("crypter", pattern=True) - self.plugins["container"] = self.containerPlugins = self.parse("container", pattern=True) - self.plugins["hoster"] = self.hosterPlugins = self.parse("hoster", pattern=True) + self.plugins['crypter'] = self.crypterPlugins = self.parse("crypter", pattern=True) + self.plugins['container'] = self.containerPlugins = self.parse("container", pattern=True) + self.plugins['hoster'] = self.hosterPlugins = self.parse("hoster", pattern=True) - self.plugins["captcha"] = self.captchaPlugins = self.parse("captcha") - self.plugins["accounts"] = self.accountPlugins = self.parse("accounts") - self.plugins["hooks"] = self.hookPlugins = self.parse("hooks") - self.plugins["internal"] = self.internalPlugins = self.parse("internal") + self.plugins['captcha'] = self.captchaPlugins = self.parse("captcha") + self.plugins['accounts'] = self.accountPlugins = self.parse("accounts") + self.plugins['hooks'] = self.hookPlugins = self.parse("hooks") + self.plugins['internal'] = self.internalPlugins = self.parse("internal") self.log.debug("created index of plugins") @@ -123,20 +123,20 @@ class PluginManager: # home contains plugins from pyload root if home and name in home: - if home[name]["v"] >= version: + if home[name]['v'] >= version: continue if name in IGNORE or (folder, name) in IGNORE: continue plugins[name] = {} - plugins[name]["v"] = version + plugins[name]['v'] = version module = f.replace(".pyc", "").replace(".py", "") # the plugin is loaded from user directory - plugins[name]["user"] = True if home else False - plugins[name]["name"] = module + plugins[name]['user'] = True if home else False + plugins[name]['name'] = module if pattern: pattern = self.PATTERN.findall(content) @@ -146,10 +146,10 @@ class PluginManager: else: pattern = "^unmachtable$" - plugins[name]["pattern"] = pattern + plugins[name]['pattern'] = pattern try: - plugins[name]["re"] = re.compile(pattern) + plugins[name]['re'] = re.compile(pattern) except: self.log.error(_("%s has a invalid pattern.") % name) @@ -210,13 +210,13 @@ class PluginManager: if type(url) not in (str, unicode, buffer): continue found = False - if last and last[1]["re"].match(url): + if last and last[1]['re'].match(url): res.append((url, last[0])) continue for name, value in chain(self.crypterPlugins.iteritems(), self.hosterPlugins.iteritems(), self.containerPlugins.iteritems()): - if value["re"].match(url): + if value['re'].match(url): res.append((url, name)) last = (name, value) found = True @@ -239,10 +239,10 @@ class PluginManager: if not plugin: self.log.warning("Plugin %s not found." % name) - plugin = self.hosterPlugins["BasePlugin"] + plugin = self.hosterPlugins['BasePlugin'] if "new_module" in plugin and not original: - return plugin["new_module"] + return plugin['new_module'] return self.loadModule(type, name) @@ -251,7 +251,7 @@ class PluginManager: plugin, type = self.findPlugin(name) if "new_name" in plugin: - return plugin["new_name"] + return plugin['new_name'] return name @@ -263,11 +263,11 @@ class PluginManager: """ plugins = self.plugins[type] if name in plugins: - if "module" in plugins[name]: return plugins[name]["module"] + if "module" in plugins[name]: return plugins[name]['module'] try: - module = __import__(self.ROOT + "%s.%s" % (type, plugins[name]["name"]), globals(), locals(), - plugins[name]["name"]) - plugins[name]["module"] = module #cache import, maybe unneeded + module = __import__(self.ROOT + "%s.%s" % (type, plugins[name]['name']), globals(), locals(), + plugins[name]['name']) + plugins[name]['module'] = module #cache import, maybe unneeded return module except Exception, e: self.log.error(_("Error importing %(name)s: %(msg)s") % {"name": name, "msg": str(e)}) @@ -295,10 +295,10 @@ class PluginManager: if type in self.plugins and name in self.plugins[type]: #userplugin is a newer version - if not user and self.plugins[type][name]["user"]: + if not user and self.plugins[type][name]['user']: return self #imported from userdir, but pyloads is newer - if user and not self.plugins[type][name]["user"]: + if user and not self.plugins[type][name]['user']: return self @@ -345,14 +345,14 @@ class PluginManager: if plugin in self.plugins[type]: if "module" in self.plugins[type][plugin]: self.log.debug("Reloading %s" % plugin) - reload(self.plugins[type][plugin]["module"]) + reload(self.plugins[type][plugin]['module']) #index creation - self.plugins["crypter"] = self.crypterPlugins = self.parse("crypter", pattern=True) - self.plugins["container"] = self.containerPlugins = self.parse("container", pattern=True) - self.plugins["hoster"] = self.hosterPlugins = self.parse("hoster", pattern=True) - self.plugins["captcha"] = self.captchaPlugins = self.parse("captcha") - self.plugins["accounts"] = self.accountPlugins = self.parse("accounts") + self.plugins['crypter'] = self.crypterPlugins = self.parse("crypter", pattern=True) + self.plugins['container'] = self.containerPlugins = self.parse("container", pattern=True) + self.plugins['hoster'] = self.hosterPlugins = self.parse("hoster", pattern=True) + self.plugins['captcha'] = self.captchaPlugins = self.parse("captcha") + self.plugins['accounts'] = self.accountPlugins = self.parse("accounts") if "accounts" in as_dict: #accounts needs to be reloaded self.core.accountManager.initPlugins() -- cgit v1.2.3 From ba916633f2bedb04c7358000b91aed69f52e8e43 Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Fri, 1 Aug 2014 19:35:59 +0200 Subject: Remove trailing whitespaces + remove license headers + import urllib methods directly + sort and fix key attributes + use save_join instead join + sort some import declarations + other minor code cosmetics --- module/plugins/PluginManager.py | 22 +++------------------- 1 file changed, 3 insertions(+), 19 deletions(-) (limited to 'module/plugins/PluginManager.py') diff --git a/module/plugins/PluginManager.py b/module/plugins/PluginManager.py index adfa5845e..9c7cab64c 100644 --- a/module/plugins/PluginManager.py +++ b/module/plugins/PluginManager.py @@ -1,32 +1,16 @@ # -*- coding: utf-8 -*- -""" - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 3 of the License, - or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - See the GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, see . - - @author: mkaay, RaNaN -""" - import re import sys +from itertools import chain from os import listdir, makedirs from os.path import isfile, join, exists, abspath from sys import version_info -from itertools import chain from traceback import print_exc from module.lib.SafeEval import const_eval as literal_eval + from module.ConfigParser import IGNORE @@ -78,7 +62,7 @@ class PluginManager: def parse(self, folder, pattern=False, home={}): """ - returns dict with information + returns dict with information home contains parsed plugins from module. { -- cgit v1.2.3 From f76e5c2336718dca9da8033ba22cd83c72c7b3b3 Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Sat, 11 Oct 2014 15:14:28 +0200 Subject: Pattern update 1 --- module/plugins/PluginManager.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'module/plugins/PluginManager.py') diff --git a/module/plugins/PluginManager.py b/module/plugins/PluginManager.py index 9c7cab64c..e263f8e04 100644 --- a/module/plugins/PluginManager.py +++ b/module/plugins/PluginManager.py @@ -20,7 +20,7 @@ class PluginManager: TYPES = ("crypter", "container", "hoster", "captcha", "accounts", "hooks", "internal") PATTERN = re.compile(r'__pattern__.*=.*r("|\')([^"\']+)') - VERSION = re.compile(r'__version__.*=.*("|\')([0-9.]+)') + VERSION = re.compile(r'__version__.*=.*("|\')([\d.]+)') CONFIG = re.compile(r'__config__.*=.*\[([^\]]+)', re.MULTILINE) DESC = re.compile(r'__description__.?=.?("|"""|\')([^"\']+)') -- cgit v1.2.3 From 0eb6e7ec4a1144dcca824d8add049787d3da1762 Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Wed, 22 Oct 2014 19:44:59 +0200 Subject: Two space before function declaration --- module/plugins/PluginManager.py | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'module/plugins/PluginManager.py') diff --git a/module/plugins/PluginManager.py b/module/plugins/PluginManager.py index e263f8e04..034e40d4a 100644 --- a/module/plugins/PluginManager.py +++ b/module/plugins/PluginManager.py @@ -60,6 +60,7 @@ class PluginManager: self.log.debug("created index of plugins") + def parse(self, folder, pattern=False, home={}): """ returns dict with information @@ -211,12 +212,14 @@ class PluginManager: return res + def findPlugin(self, name, pluginlist=("hoster", "crypter", "container")): for ptype in pluginlist: if name in self.plugins[ptype]: return self.plugins[ptype][name], ptype return None, None + def getPlugin(self, name, original=False): """return plugin module from hoster|decrypter|container""" plugin, type = self.findPlugin(name) @@ -230,6 +233,7 @@ class PluginManager: return self.loadModule(type, name) + def getPluginName(self, name): """ used to obtain new name if other plugin was injected""" plugin, type = self.findPlugin(name) @@ -239,6 +243,7 @@ class PluginManager: return name + def loadModule(self, type, name): """ Returns loaded module for plugin @@ -258,15 +263,18 @@ class PluginManager: if self.core.debug: print_exc() + def loadClass(self, type, name): """Returns the class of a plugin with the same name""" module = self.loadModule(type, name) if module: return getattr(module, name) + def getAccountPlugins(self): """return list of account plugin names""" return self.accountPlugins.keys() + def find_module(self, fullname, path=None): #redirecting imports if necesarry if fullname.startswith(self.ROOT) or fullname.startswith(self.USERROOT): #seperate pyload plugins -- cgit v1.2.3 From 4da90891eb2544ac15a7d512aba8cb357f68ee5f Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Sat, 25 Oct 2014 01:11:29 +0200 Subject: Spare code cosmetics --- module/plugins/PluginManager.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'module/plugins/PluginManager.py') diff --git a/module/plugins/PluginManager.py b/module/plugins/PluginManager.py index 034e40d4a..c4f4f8924 100644 --- a/module/plugins/PluginManager.py +++ b/module/plugins/PluginManager.py @@ -21,7 +21,7 @@ class PluginManager: PATTERN = re.compile(r'__pattern__.*=.*r("|\')([^"\']+)') VERSION = re.compile(r'__version__.*=.*("|\')([\d.]+)') - CONFIG = re.compile(r'__config__.*=.*\[([^\]]+)', re.MULTILINE) + CONFIG = re.compile(r'__config__.*=.*\[([^\]]+)', re.M) DESC = re.compile(r'__description__.?=.?("|"""|\')([^"\']+)') -- cgit v1.2.3 From 9f2ebe486a3e155fb6a60e07cccb77ab6a772eb2 Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Sun, 26 Oct 2014 02:31:54 +0200 Subject: Extend translation support in plugins + a lot of code cosmetics and typo fixes --- module/plugins/PluginManager.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'module/plugins/PluginManager.py') diff --git a/module/plugins/PluginManager.py b/module/plugins/PluginManager.py index c4f4f8924..47f48c031 100644 --- a/module/plugins/PluginManager.py +++ b/module/plugins/PluginManager.py @@ -136,7 +136,7 @@ class PluginManager: try: plugins[name]['re'] = re.compile(pattern) except: - self.log.error(_("%s has a invalid pattern.") % name) + self.log.error(_("%s has a invalid pattern") % name) # internals have no config -- cgit v1.2.3 From aa0751bcfd995e308bcd586a6965c75e68b1274b Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Mon, 27 Oct 2014 23:05:13 +0100 Subject: Code cosmetics --- module/plugins/PluginManager.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'module/plugins/PluginManager.py') diff --git a/module/plugins/PluginManager.py b/module/plugins/PluginManager.py index 47f48c031..5348c070e 100644 --- a/module/plugins/PluginManager.py +++ b/module/plugins/PluginManager.py @@ -15,14 +15,14 @@ from module.ConfigParser import IGNORE class PluginManager: - ROOT = "module.plugins." + ROOT = "module.plugins." USERROOT = "userplugins." - TYPES = ("crypter", "container", "hoster", "captcha", "accounts", "hooks", "internal") + TYPES = ("crypter", "container", "hoster", "captcha", "accounts", "hooks", "internal") - PATTERN = re.compile(r'__pattern__.*=.*r("|\')([^"\']+)') - VERSION = re.compile(r'__version__.*=.*("|\')([\d.]+)') - CONFIG = re.compile(r'__config__.*=.*\[([^\]]+)', re.M) - DESC = re.compile(r'__description__.?=.?("|"""|\')([^"\']+)') + PATTERN = re.compile(r'__pattern__\s*=\s*[a-z]*("|\')([^"\']+)') + VERSION = re.compile(r'__version__\s*=\s*("|\')([\d.]+)') + CONFIG = re.compile(r'__config__\s*=\s*\[([^\]]+)', re.M) + DESC = re.compile(r'__description__\s*=\s*("|"""|\')([^"\']+)') def __init__(self, core): -- cgit v1.2.3 From 9d036eee4862537261e82fda6d0afb8d4cb2afe3 Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Tue, 28 Oct 2014 03:59:24 +0100 Subject: [PluginManager] Fix activated parsing --- module/plugins/PluginManager.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) (limited to 'module/plugins/PluginManager.py') diff --git a/module/plugins/PluginManager.py b/module/plugins/PluginManager.py index 5348c070e..3d2b05e75 100644 --- a/module/plugins/PluginManager.py +++ b/module/plugins/PluginManager.py @@ -129,7 +129,7 @@ class PluginManager: if pattern: pattern = pattern[0][1] else: - pattern = "^unmachtable$" + pattern = "^unmatchable$" plugins[name]['pattern'] = pattern @@ -155,13 +155,8 @@ class PluginManager: else: config = [list(config)] - if folder == "hooks": - append = True - for item in config: - if item[0] == "activated": append = False - - # activated flag missing - if append: config.append(["activated", "bool", "Activated", False]) + if folder not in ("accounts", "internal") and not [True for item in config if item[0] == "activated"]: + config.insert(0, ["activated", "bool", "Activated", False if folder == "hooks" else True]) try: self.core.config.addPluginConfig(name, config, desc) -- cgit v1.2.3 From 951f0051f1a649f9da42561b6f2d11e55b6f2b64 Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Sat, 1 Nov 2014 02:55:18 +0100 Subject: [PluginManager] Better PATTERN --- module/plugins/PluginManager.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'module/plugins/PluginManager.py') diff --git a/module/plugins/PluginManager.py b/module/plugins/PluginManager.py index 3d2b05e75..9a9ebad1d 100644 --- a/module/plugins/PluginManager.py +++ b/module/plugins/PluginManager.py @@ -19,7 +19,7 @@ class PluginManager: USERROOT = "userplugins." TYPES = ("crypter", "container", "hoster", "captcha", "accounts", "hooks", "internal") - PATTERN = re.compile(r'__pattern__\s*=\s*[a-z]*("|\')([^"\']+)') + PATTERN = re.compile(r'__pattern__\s*=\s*u?r("|\')([^"\']+)') VERSION = re.compile(r'__version__\s*=\s*("|\')([\d.]+)') CONFIG = re.compile(r'__config__\s*=\s*\[([^\]]+)', re.M) DESC = re.compile(r'__description__\s*=\s*("|"""|\')([^"\']+)') -- cgit v1.2.3 From 84462a9beee71d3a0ed5be1a3cea14979be09c30 Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Mon, 3 Nov 2014 12:46:17 +0100 Subject: [PluginManager] Fix parse --- module/plugins/PluginManager.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'module/plugins/PluginManager.py') diff --git a/module/plugins/PluginManager.py b/module/plugins/PluginManager.py index 9a9ebad1d..ae8333c0e 100644 --- a/module/plugins/PluginManager.py +++ b/module/plugins/PluginManager.py @@ -61,7 +61,7 @@ class PluginManager: self.log.debug("created index of plugins") - def parse(self, folder, pattern=False, home={}): + def parse(self, folder, pattern=False, home=None): """ returns dict with information home contains parsed plugins from module. @@ -173,7 +173,7 @@ class PluginManager: except: self.log.error("Invalid config in %s: %s" % (name, config)) - if not home: + if home is None: temp = self.parse(folder, pattern, plugins) plugins.update(temp) -- cgit v1.2.3 From 29b39ec6020ec8be623687f03bdf48172a612a66 Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Thu, 6 Nov 2014 04:32:37 +0100 Subject: Code cosmetics --- module/plugins/PluginManager.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'module/plugins/PluginManager.py') diff --git a/module/plugins/PluginManager.py b/module/plugins/PluginManager.py index ae8333c0e..a74c01ec0 100644 --- a/module/plugins/PluginManager.py +++ b/module/plugins/PluginManager.py @@ -314,7 +314,7 @@ class PluginManager: """ reloads and reindexes plugins """ if not type_plugins: return False - self.log.debug("Request reload of plugins: %s" % type_plugins) + self.log.debug("Request reload of plugins: %s" % ", ".join(type_plugins)) as_dict = {} for t,n in type_plugins: -- cgit v1.2.3 From 5808282e10117332ce97911bac48a4a142d42f28 Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Thu, 6 Nov 2014 04:51:45 +0100 Subject: Tiny code cosmetics to fix previous ones --- module/plugins/PluginManager.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'module/plugins/PluginManager.py') diff --git a/module/plugins/PluginManager.py b/module/plugins/PluginManager.py index a74c01ec0..ae8333c0e 100644 --- a/module/plugins/PluginManager.py +++ b/module/plugins/PluginManager.py @@ -314,7 +314,7 @@ class PluginManager: """ reloads and reindexes plugins """ if not type_plugins: return False - self.log.debug("Request reload of plugins: %s" % ", ".join(type_plugins)) + self.log.debug("Request reload of plugins: %s" % type_plugins) as_dict = {} for t,n in type_plugins: -- cgit v1.2.3 From 2bdbf0d79786dd8936bcda15aee69fa446a2a5e3 Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Sun, 9 Nov 2014 03:09:42 +0100 Subject: [PluginManager] Fix parse method --- module/plugins/PluginManager.py | 48 +++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 23 deletions(-) (limited to 'module/plugins/PluginManager.py') diff --git a/module/plugins/PluginManager.py b/module/plugins/PluginManager.py index ae8333c0e..3085c87e3 100644 --- a/module/plugins/PluginManager.py +++ b/module/plugins/PluginManager.py @@ -12,6 +12,7 @@ from traceback import print_exc from module.lib.SafeEval import const_eval as literal_eval from module.ConfigParser import IGNORE +from module.utils import save_join class PluginManager: @@ -43,12 +44,6 @@ class PluginManager: sys.path.append(abspath("")) - if not exists("userplugins"): - makedirs("userplugins") - if not exists(join("userplugins", "__init__.py")): - f = open(join("userplugins", "__init__.py"), "wb") - f.close() - self.plugins['crypter'] = self.crypterPlugins = self.parse("crypter", pattern=True) self.plugins['container'] = self.containerPlugins = self.parse("container", pattern=True) self.plugins['hoster'] = self.hosterPlugins = self.parse("hoster", pattern=True) @@ -72,23 +67,31 @@ class PluginManager: """ plugins = {} - if home: - pfolder = join("userplugins", folder) - if not exists(pfolder): - makedirs(pfolder) - if not exists(join(pfolder, "__init__.py")): - f = open(join(pfolder, "__init__.py"), "wb") - f.close() - - else: - pfolder = join(pypath, "module", "plugins", folder) - + + try: + try: + pfolder = save_join("userplugins", folder) + if not exists(pfolder): + makedirs(pfolder) + + ifile = join(pfolder, "__init__.py") + if not exists(ifile): + f = open(ifile, "a") + f.close() + + except IOError, e: + pfolder = join(pypath, "module", "plugins", folder) + + except Exception, e: + self.logCritical(str(e)) + return plugins + for f in listdir(pfolder): if (isfile(join(pfolder, f)) and f.endswith(".py") or f.endswith("_25.pyc") or f.endswith( "_26.pyc") or f.endswith("_27.pyc")) and not f.startswith("_"): - data = open(join(pfolder, f)) - content = data.read() - data.close() + + with open(join(pfolder, f)) as data: + content = data.read() if f.endswith("_25.pyc") and version_info[0:2] != (2, 5): continue @@ -107,7 +110,7 @@ class PluginManager: version = 0 # home contains plugins from pyload root - if home and name in home: + if isinstance(home, dict) and name in home: if home[name]['v'] >= version: continue @@ -174,8 +177,7 @@ class PluginManager: self.log.error("Invalid config in %s: %s" % (name, config)) if home is None: - temp = self.parse(folder, pattern, plugins) - plugins.update(temp) + plugins.update(self.parse(folder, pattern, plugins)) return plugins -- cgit v1.2.3 From 59f72bfc5ed721c80c821bd0ca1bc8daf0d49880 Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Sun, 9 Nov 2014 03:12:41 +0100 Subject: Code cosmetics --- module/plugins/PluginManager.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'module/plugins/PluginManager.py') diff --git a/module/plugins/PluginManager.py b/module/plugins/PluginManager.py index 3085c87e3..43e9f9ad4 100644 --- a/module/plugins/PluginManager.py +++ b/module/plugins/PluginManager.py @@ -67,29 +67,29 @@ class PluginManager: """ plugins = {} - + try: try: pfolder = save_join("userplugins", folder) if not exists(pfolder): makedirs(pfolder) - + ifile = join(pfolder, "__init__.py") if not exists(ifile): f = open(ifile, "a") f.close() - + except IOError, e: pfolder = join(pypath, "module", "plugins", folder) - + except Exception, e: self.logCritical(str(e)) return plugins - + for f in listdir(pfolder): if (isfile(join(pfolder, f)) and f.endswith(".py") or f.endswith("_25.pyc") or f.endswith( "_26.pyc") or f.endswith("_27.pyc")) and not f.startswith("_"): - + with open(join(pfolder, f)) as data: content = data.read() -- cgit v1.2.3 From 12bf6e492c980814646c6bc6e88a2ed759f5acbc Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Sun, 9 Nov 2014 16:52:01 +0100 Subject: [PluginManager] Improve error handling for parse method --- module/plugins/PluginManager.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'module/plugins/PluginManager.py') diff --git a/module/plugins/PluginManager.py b/module/plugins/PluginManager.py index 43e9f9ad4..82702eb7f 100644 --- a/module/plugins/PluginManager.py +++ b/module/plugins/PluginManager.py @@ -149,19 +149,19 @@ class PluginManager: config = self.CONFIG.findall(content) if config: - config = literal_eval(config[0].strip().replace("\n", "").replace("\r", "")) - desc = self.DESC.findall(content) - desc = desc[0][1] if desc else "" + try: + config = literal_eval(config[0].strip().replace("\n", "").replace("\r", "")) + desc = self.DESC.findall(content) + desc = desc[0][1] if desc else "" - if type(config[0]) == tuple: - config = [list(x) for x in config] - else: - config = [list(config)] + if type(config[0]) == tuple: + config = [list(x) for x in config] + else: + config = [list(config)] - if folder not in ("accounts", "internal") and not [True for item in config if item[0] == "activated"]: - config.insert(0, ["activated", "bool", "Activated", False if folder == "hooks" else True]) + if folder not in ("accounts", "internal") and not [True for item in config if item[0] == "activated"]: + config.insert(0, ["activated", "bool", "Activated", False if folder == "hooks" else True]) - try: self.core.config.addPluginConfig(name, config, desc) except: self.log.error("Invalid config in %s: %s" % (name, config)) -- cgit v1.2.3 From c9e31d875d32de31e54959b82bc35eff2b3e0f3f Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Mon, 10 Nov 2014 00:19:51 +0100 Subject: Code cosmetics --- module/plugins/PluginManager.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'module/plugins/PluginManager.py') diff --git a/module/plugins/PluginManager.py b/module/plugins/PluginManager.py index 82702eb7f..9ef211f35 100644 --- a/module/plugins/PluginManager.py +++ b/module/plugins/PluginManager.py @@ -83,7 +83,7 @@ class PluginManager: pfolder = join(pypath, "module", "plugins", folder) except Exception, e: - self.logCritical(str(e)) + self.logCritical(e) return plugins for f in listdir(pfolder): -- cgit v1.2.3 From 0852ad27d129276b3eb1c40a14dfaa390175ea77 Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Mon, 10 Nov 2014 11:56:51 +0100 Subject: [PluginManager] Improve some routines --- module/plugins/PluginManager.py | 100 ++++++++++++++++++++++------------------ 1 file changed, 55 insertions(+), 45 deletions(-) (limited to 'module/plugins/PluginManager.py') diff --git a/module/plugins/PluginManager.py b/module/plugins/PluginManager.py index 9ef211f35..59fdb31e8 100644 --- a/module/plugins/PluginManager.py +++ b/module/plugins/PluginManager.py @@ -44,19 +44,14 @@ class PluginManager: sys.path.append(abspath("")) - self.plugins['crypter'] = self.crypterPlugins = self.parse("crypter", pattern=True) - self.plugins['container'] = self.containerPlugins = self.parse("container", pattern=True) - self.plugins['hoster'] = self.hosterPlugins = self.parse("hoster", pattern=True) + for type in ('accounts', 'captcha', 'container', 'crypter', 'hooks', 'hoster', 'internal') + self.plugins[type] = self.parse(type) + setattr(self, "%sPlugins" % type, self.plugins[type]) - self.plugins['captcha'] = self.captchaPlugins = self.parse("captcha") - self.plugins['accounts'] = self.accountPlugins = self.parse("accounts") - self.plugins['hooks'] = self.hookPlugins = self.parse("hooks") - self.plugins['internal'] = self.internalPlugins = self.parse("internal") + self.log.debug("Created index of plugins") - self.log.debug("created index of plugins") - - def parse(self, folder, pattern=False, home=None): + def parse(self, folder, rootplugins={}): """ returns dict with information home contains parsed plugins from module. @@ -70,7 +65,7 @@ class PluginManager: try: try: - pfolder = save_join("userplugins", folder) + pfolder = join("userplugins", folder) if not exists(pfolder): makedirs(pfolder) @@ -90,13 +85,20 @@ class PluginManager: if (isfile(join(pfolder, f)) and f.endswith(".py") or f.endswith("_25.pyc") or f.endswith( "_26.pyc") or f.endswith("_27.pyc")) and not f.startswith("_"): - with open(join(pfolder, f)) as data: - content = data.read() + try: + with open(join(pfolder, f)) as data: + content = data.read() + + except IOError, e: + self.logError(e) + continue if f.endswith("_25.pyc") and version_info[0:2] != (2, 5): continue + elif f.endswith("_26.pyc") and version_info[0:2] != (2, 6): continue + elif f.endswith("_27.pyc") and version_info[0:2] != (2, 7): continue @@ -109,9 +111,8 @@ class PluginManager: else: version = 0 - # home contains plugins from pyload root - if isinstance(home, dict) and name in home: - if home[name]['v'] >= version: + if rootplugins and name in rootplugins: + if rootplugins[name]['v'] >= version: continue if name in IGNORE or (folder, name) in IGNORE: @@ -123,16 +124,13 @@ class PluginManager: module = f.replace(".pyc", "").replace(".py", "") # the plugin is loaded from user directory - plugins[name]['user'] = True if home else False + plugins[name]['user'] = True if rootplugins else False plugins[name]['name'] = module - if pattern: - pattern = self.PATTERN.findall(content) + pattern = self.PATTERN.findall(content) - if pattern: - pattern = pattern[0][1] - else: - pattern = "^unmatchable$" + if pattern: + pattern = pattern[0][1] plugins[name]['pattern'] = pattern @@ -176,8 +174,8 @@ class PluginManager: except: self.log.error("Invalid config in %s: %s" % (name, config)) - if home is None: - plugins.update(self.parse(folder, pattern, plugins)) + if not rootplugins: + plugins.update(self.parse(folder, plugins)) return plugins @@ -222,7 +220,7 @@ class PluginManager: plugin, type = self.findPlugin(name) if not plugin: - self.log.warning("Plugin %s not found." % name) + self.log.warning("Plugin %s not found" % name) plugin = self.hosterPlugins['BasePlugin'] if "new_module" in plugin and not original: @@ -249,22 +247,32 @@ class PluginManager: """ plugins = self.plugins[type] if name in plugins: - if "module" in plugins[name]: return plugins[name]['module'] + if "module" in plugins[name]: + return plugins[name]['module'] + try: module = __import__(self.ROOT + "%s.%s" % (type, plugins[name]['name']), globals(), locals(), - plugins[name]['name']) + plugins[name]['name']) plugins[name]['module'] = module #cache import, maybe unneeded - return module + except Exception, e: self.log.error(_("Error importing %(name)s: %(msg)s") % {"name": name, "msg": str(e)}) if self.core.debug: print_exc() + else: + self.log.debug(_("Loaded module %(name)s (v%(version).2f)") % {'name': name, 'version': plugins[name]['version']}) + return module + def loadClass(self, type, name): """Returns the class of a plugin with the same name""" module = self.loadModule(type, name) - if module: return getattr(module, name) + if module: + return getattr(module, name) + else: + self.log.error(_("%s class %s not loaded") % (type.capitalize(), name)) + return None def getAccountPlugins(self): @@ -298,7 +306,8 @@ class PluginManager: newname = name.replace(self.ROOT, self.USERROOT) else: newname = name.replace(self.USERROOT, self.ROOT) - else: newname = name + else: + newname = name base, plugin = newname.rsplit(".", 1) @@ -314,40 +323,41 @@ class PluginManager: def reloadPlugins(self, type_plugins): """ reloads and reindexes plugins """ - if not type_plugins: return False + if not type_plugins: + return None self.log.debug("Request reload of plugins: %s" % type_plugins) + flag = True as_dict = {} + for t,n in type_plugins: if t in as_dict: as_dict[t].append(n) else: as_dict[t] = [n] - # we do not reload hooks or internals, would cause to much side effects - if "hooks" in as_dict or "internal" in as_dict: - return False - for type in as_dict.iterkeys(): + # we do not reload hooks or internals, would cause to much side effects + if type in ("hooks", "internal"): + flag = False + continue + for plugin in as_dict[type]: if plugin in self.plugins[type]: if "module" in self.plugins[type][plugin]: - self.log.debug("Reloading %s" % plugin) + self.log.debug("Reloading module %s" % plugin) reload(self.plugins[type][plugin]['module']) - #index creation - self.plugins['crypter'] = self.crypterPlugins = self.parse("crypter", pattern=True) - self.plugins['container'] = self.containerPlugins = self.parse("container", pattern=True) - self.plugins['hoster'] = self.hosterPlugins = self.parse("hoster", pattern=True) - self.plugins['captcha'] = self.captchaPlugins = self.parse("captcha") - self.plugins['accounts'] = self.accountPlugins = self.parse("accounts") + #index creation + self.plugins[type] = self.parse(type) + setattr(self, "%sPlugins" % type, self.plugins[type]) - if "accounts" in as_dict: #accounts needs to be reloaded + if "accounts" in as_dict: #: accounts needs to be reloaded self.core.accountManager.initPlugins() self.core.scheduler.addJob(0, self.core.accountManager.getAccountInfos) - return True + return flag -- cgit v1.2.3 From 8eb504f8db6aefa97c92f25ced59bf434367a76a Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Tue, 11 Nov 2014 11:51:58 +0100 Subject: [PluginManager] Fix self.plugins ref (thx clinton-hall) --- module/plugins/PluginManager.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'module/plugins/PluginManager.py') diff --git a/module/plugins/PluginManager.py b/module/plugins/PluginManager.py index 59fdb31e8..a18a53bdb 100644 --- a/module/plugins/PluginManager.py +++ b/module/plugins/PluginManager.py @@ -44,7 +44,10 @@ class PluginManager: sys.path.append(abspath("")) - for type in ('accounts', 'captcha', 'container', 'crypter', 'hooks', 'hoster', 'internal') + #@NOTE: In 0.4.10 directory "accounts" changes to "account" + self.plugins['accounts'] = self.accountPlugins = self.parse("accounts") + + for type in ('captcha', 'container', 'crypter', 'hooks', 'hoster', 'internal'): self.plugins[type] = self.parse(type) setattr(self, "%sPlugins" % type, self.plugins[type]) @@ -351,7 +354,11 @@ class PluginManager: #index creation self.plugins[type] = self.parse(type) - setattr(self, "%sPlugins" % type, self.plugins[type]) + + if type is "accounts": + self.accountPlugins = self.plugins[type] #@TODO: Remove in 0.4.10 + else: + setattr(self, "%sPlugins" % type, self.plugins[type]) if "accounts" in as_dict: #: accounts needs to be reloaded self.core.accountManager.initPlugins() -- cgit v1.2.3 From decaff08ff93ce78ab0d988e37df5908c2fea86c Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Tue, 11 Nov 2014 12:43:44 +0100 Subject: [PluginManager] Fix broken parse routine --- module/plugins/PluginManager.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) (limited to 'module/plugins/PluginManager.py') diff --git a/module/plugins/PluginManager.py b/module/plugins/PluginManager.py index a18a53bdb..a1efc449c 100644 --- a/module/plugins/PluginManager.py +++ b/module/plugins/PluginManager.py @@ -12,7 +12,6 @@ from traceback import print_exc from module.lib.SafeEval import const_eval as literal_eval from module.ConfigParser import IGNORE -from module.utils import save_join class PluginManager: @@ -44,10 +43,11 @@ class PluginManager: sys.path.append(abspath("")) - #@NOTE: In 0.4.10 directory "accounts" changes to "account" + #@NOTE: In 0.4.10 directory "accounts" changes to "account" and "hooks" changes to "hook" self.plugins['accounts'] = self.accountPlugins = self.parse("accounts") + self.plugins['hooks'] = self.hookPlugins = self.parse("hooks") - for type in ('captcha', 'container', 'crypter', 'hooks', 'hoster', 'internal'): + for type in ('captcha', 'container', 'crypter', 'hoster', 'internal'): self.plugins[type] = self.parse(type) setattr(self, "%sPlugins" % type, self.plugins[type]) @@ -64,25 +64,27 @@ class PluginManager: } """ + plugins = {} - try: + if rootplugins: try: pfolder = join("userplugins", folder) if not exists(pfolder): makedirs(pfolder) - ifile = join(pfolder, "__init__.py") - if not exists(ifile): - f = open(ifile, "a") - f.close() + for ifile in (join(join("userplugins", "__init__.py")), + join(pfolder, "__init__.py")): + if not exists(ifile): + f = open(ifile, "wb") + f.close() except IOError, e: - pfolder = join(pypath, "module", "plugins", folder) + self.logCritical(e) + return rootplugins - except Exception, e: - self.logCritical(e) - return plugins + else: + pfolder = join(pypath, "module", "plugins", folder) for f in listdir(pfolder): if (isfile(join(pfolder, f)) and f.endswith(".py") or f.endswith("_25.pyc") or f.endswith( @@ -142,7 +144,6 @@ class PluginManager: except: self.log.error(_("%s has a invalid pattern") % name) - # internals have no config if folder == "internal": self.core.config.deleteConfig(name) @@ -177,7 +178,7 @@ class PluginManager: except: self.log.error("Invalid config in %s: %s" % (name, config)) - if not rootplugins: + if not rootplugins and plugins: #: Double check plugins.update(self.parse(folder, plugins)) return plugins -- cgit v1.2.3 From 60d594ac498dcb4ff81ea1530d680053f6f0bb22 Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Tue, 11 Nov 2014 12:48:02 +0100 Subject: [PluginManager] Fix typo --- module/plugins/PluginManager.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'module/plugins/PluginManager.py') diff --git a/module/plugins/PluginManager.py b/module/plugins/PluginManager.py index a1efc449c..df22b69e7 100644 --- a/module/plugins/PluginManager.py +++ b/module/plugins/PluginManager.py @@ -73,7 +73,7 @@ class PluginManager: if not exists(pfolder): makedirs(pfolder) - for ifile in (join(join("userplugins", "__init__.py")), + for ifile in (join("userplugins", "__init__.py"), join(pfolder, "__init__.py")): if not exists(ifile): f = open(ifile, "wb") -- cgit v1.2.3 From ff0e1a28ce6ac27d11cea7cceaeb50a779facafb Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Wed, 12 Nov 2014 01:52:08 +0100 Subject: [Plugin] Save reason as pyfile.error when fails --- module/plugins/PluginManager.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'module/plugins/PluginManager.py') diff --git a/module/plugins/PluginManager.py b/module/plugins/PluginManager.py index df22b69e7..78b5a9337 100644 --- a/module/plugins/PluginManager.py +++ b/module/plugins/PluginManager.py @@ -43,7 +43,7 @@ class PluginManager: sys.path.append(abspath("")) - #@NOTE: In 0.4.10 directory "accounts" changes to "account" and "hooks" changes to "hook" + #@NOTE: In 0.4.10 directory "accounts" changes to "account" and "hooks" changes to "addon" self.plugins['accounts'] = self.accountPlugins = self.parse("accounts") self.plugins['hooks'] = self.hookPlugins = self.parse("hooks") -- cgit v1.2.3 From 812aadc5a528910c2356f1b5106498b388c474ec Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Wed, 12 Nov 2014 14:23:21 +0100 Subject: [PluginManager] Use key 'version' instead 'v' --- module/plugins/PluginManager.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'module/plugins/PluginManager.py') diff --git a/module/plugins/PluginManager.py b/module/plugins/PluginManager.py index 78b5a9337..6511f8ae6 100644 --- a/module/plugins/PluginManager.py +++ b/module/plugins/PluginManager.py @@ -117,14 +117,14 @@ class PluginManager: version = 0 if rootplugins and name in rootplugins: - if rootplugins[name]['v'] >= version: + if rootplugins[name]['version'] >= version: continue if name in IGNORE or (folder, name) in IGNORE: continue plugins[name] = {} - plugins[name]['v'] = version + plugins[name]['version'] = version module = f.replace(".pyc", "").replace(".py", "") -- cgit v1.2.3 From 3b717bf5bd36f69fb37ec72d07c32279244b4452 Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Wed, 12 Nov 2014 22:12:29 +0100 Subject: [PluginManager] Fix pattern creation in parse routine --- module/plugins/PluginManager.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'module/plugins/PluginManager.py') diff --git a/module/plugins/PluginManager.py b/module/plugins/PluginManager.py index 6511f8ae6..c955f9d44 100644 --- a/module/plugins/PluginManager.py +++ b/module/plugins/PluginManager.py @@ -137,12 +137,15 @@ class PluginManager: if pattern: pattern = pattern[0][1] - plugins[name]['pattern'] = pattern - try: - plugins[name]['re'] = re.compile(pattern) + regexp = re.compile(pattern) except: self.log.error(_("%s has a invalid pattern") % name) + pattern = r'^unmatchable$' + regexp = re.compile(pattern) + + plugins[name]['pattern'] = pattern + plugins[name]['re'] = regexp # internals have no config if folder == "internal": -- cgit v1.2.3 From 72f92fd106bbf56e2804a8be307c6d5db13ace9a Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Thu, 13 Nov 2014 01:00:16 +0100 Subject: [PluginManager] Fix parseUrls routine + code cosmetics --- module/plugins/PluginManager.py | 39 ++++++++++++++++----------------------- 1 file changed, 16 insertions(+), 23 deletions(-) (limited to 'module/plugins/PluginManager.py') diff --git a/module/plugins/PluginManager.py b/module/plugins/PluginManager.py index c955f9d44..59ba47410 100644 --- a/module/plugins/PluginManager.py +++ b/module/plugins/PluginManager.py @@ -11,8 +11,6 @@ from traceback import print_exc from module.lib.SafeEval import const_eval as literal_eval -from module.ConfigParser import IGNORE - class PluginManager: ROOT = "module.plugins." @@ -47,7 +45,7 @@ class PluginManager: self.plugins['accounts'] = self.accountPlugins = self.parse("accounts") self.plugins['hooks'] = self.hookPlugins = self.parse("hooks") - for type in ('captcha', 'container', 'crypter', 'hoster', 'internal'): + for type in set(self.TYPES) - set(('accounts', 'hooks')): self.plugins[type] = self.parse(type) setattr(self, "%sPlugins" % type, self.plugins[type]) @@ -58,11 +56,6 @@ class PluginManager: """ returns dict with information home contains parsed plugins from module. - - { - name : {path, version, config, (pattern, re), (plugin, class)} - } - """ plugins = {} @@ -98,17 +91,18 @@ class PluginManager: self.logError(e) continue - if f.endswith("_25.pyc") and version_info[0:2] != (2, 5): + if f.endswith("_25.pyc") and version_info[0:2] != (2, 5): #@TODO: Remove in 0.4.10 continue - elif f.endswith("_26.pyc") and version_info[0:2] != (2, 6): + elif f.endswith("_26.pyc") and version_info[0:2] != (2, 6): #@TODO: Remove in 0.4.10 continue - elif f.endswith("_27.pyc") and version_info[0:2] != (2, 7): + elif f.endswith("_27.pyc") and version_info[0:2] != (2, 7): #@TODO: Remove in 0.4.10 continue name = f[:-3] - if name[-1] == ".": name = name[:-4] + if name[-1] == ".": + name = name[:-4] version = self.VERSION.findall(content) if version: @@ -120,9 +114,6 @@ class PluginManager: if rootplugins[name]['version'] >= version: continue - if name in IGNORE or (folder, name) in IGNORE: - continue - plugins[name] = {} plugins[name]['version'] = version @@ -202,8 +193,8 @@ class PluginManager: continue for name, value in chain(self.crypterPlugins.iteritems(), self.hosterPlugins.iteritems(), - self.containerPlugins.iteritems()): - if value['re'].match(url): + self.containerPlugins.iteritems()): + if 're' in value and value['re'].match(url): #@TODO: Rewrite this check to report missing __pattern__ attribute alert res.append((url, name)) last = (name, value) found = True @@ -260,15 +251,18 @@ class PluginManager: try: module = __import__(self.ROOT + "%s.%s" % (type, plugins[name]['name']), globals(), locals(), plugins[name]['name']) - plugins[name]['module'] = module #cache import, maybe unneeded except Exception, e: - self.log.error(_("Error importing %(name)s: %(msg)s") % {"name": name, "msg": str(e)}) + self.log.error(_("Error importing plugin: [%(type)s] %(name)s (v%(version).2f) | %(errmsg)s") + % {'name': name, 'type': type, 'version': plugins[name]['version'], "errmsg": str(e)}) if self.core.debug: print_exc() else: - self.log.debug(_("Loaded module %(name)s (v%(version).2f)") % {'name': name, 'version': plugins[name]['version']}) + plugins[name]['module'] = module #: cache import, maybe unneeded + + self.log.debug(_("Loaded plugin: [%(type)s] %(name)s (v%(version).2f)") + % {'name': name, 'type': type, 'version': plugins[name]['version']}) return module @@ -278,7 +272,6 @@ class PluginManager: if module: return getattr(module, name) else: - self.log.error(_("%s class %s not loaded") % (type.capitalize(), name)) return None @@ -359,8 +352,8 @@ class PluginManager: #index creation self.plugins[type] = self.parse(type) - if type is "accounts": - self.accountPlugins = self.plugins[type] #@TODO: Remove in 0.4.10 + if type is "accounts": #@TODO: Remove this check in 0.4.10 + self.accountPlugins = self.plugins[type] else: setattr(self, "%sPlugins" % type, self.plugins[type]) -- cgit v1.2.3 From ef4bc4b73756565e40c7453f6b71bc1021735033 Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Sat, 22 Nov 2014 19:38:25 +0100 Subject: Revert plugins to stable --- module/plugins/PluginManager.py | 263 ++++++++++++++++++++-------------------- 1 file changed, 130 insertions(+), 133 deletions(-) (limited to 'module/plugins/PluginManager.py') diff --git a/module/plugins/PluginManager.py b/module/plugins/PluginManager.py index 59ba47410..f3f5f47bc 100644 --- a/module/plugins/PluginManager.py +++ b/module/plugins/PluginManager.py @@ -1,26 +1,43 @@ # -*- coding: utf-8 -*- +""" + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, + or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . + + @author: mkaay, RaNaN +""" + import re import sys -from itertools import chain from os import listdir, makedirs from os.path import isfile, join, exists, abspath from sys import version_info +from itertools import chain from traceback import print_exc from module.lib.SafeEval import const_eval as literal_eval - +from module.ConfigParser import IGNORE class PluginManager: - ROOT = "module.plugins." + ROOT = "module.plugins." USERROOT = "userplugins." - TYPES = ("crypter", "container", "hoster", "captcha", "accounts", "hooks", "internal") + TYPES = ("crypter", "container", "hoster", "captcha", "accounts", "hooks", "internal") - PATTERN = re.compile(r'__pattern__\s*=\s*u?r("|\')([^"\']+)') - VERSION = re.compile(r'__version__\s*=\s*("|\')([\d.]+)') - CONFIG = re.compile(r'__config__\s*=\s*\[([^\]]+)', re.M) - DESC = re.compile(r'__description__\s*=\s*("|"""|\')([^"\']+)') + PATTERN = re.compile(r'__pattern__.*=.*r("|\')([^"\']+)') + VERSION = re.compile(r'__version__.*=.*("|\')([0-9.]+)') + CONFIG = re.compile(r'__config__.*=.*\[([^\]]+)', re.MULTILINE) + DESC = re.compile(r'__description__.?=.?("|"""|\')([^"\']+)') def __init__(self, core): @@ -41,40 +58,41 @@ class PluginManager: sys.path.append(abspath("")) - #@NOTE: In 0.4.10 directory "accounts" changes to "account" and "hooks" changes to "addon" - self.plugins['accounts'] = self.accountPlugins = self.parse("accounts") - self.plugins['hooks'] = self.hookPlugins = self.parse("hooks") + if not exists("userplugins"): + makedirs("userplugins") + if not exists(join("userplugins", "__init__.py")): + f = open(join("userplugins", "__init__.py"), "wb") + f.close() - for type in set(self.TYPES) - set(('accounts', 'hooks')): - self.plugins[type] = self.parse(type) - setattr(self, "%sPlugins" % type, self.plugins[type]) + self.plugins["crypter"] = self.crypterPlugins = self.parse("crypter", pattern=True) + self.plugins["container"] = self.containerPlugins = self.parse("container", pattern=True) + self.plugins["hoster"] = self.hosterPlugins = self.parse("hoster", pattern=True) - self.log.debug("Created index of plugins") + self.plugins["captcha"] = self.captchaPlugins = self.parse("captcha") + self.plugins["accounts"] = self.accountPlugins = self.parse("accounts") + self.plugins["hooks"] = self.hookPlugins = self.parse("hooks") + self.plugins["internal"] = self.internalPlugins = self.parse("internal") + self.log.debug("created index of plugins") - def parse(self, folder, rootplugins={}): + def parse(self, folder, pattern=False, home={}): """ - returns dict with information + returns dict with information home contains parsed plugins from module. + + { + name : {path, version, config, (pattern, re), (plugin, class)} + } + """ - plugins = {} - - if rootplugins: - try: - pfolder = join("userplugins", folder) - if not exists(pfolder): - makedirs(pfolder) - - for ifile in (join("userplugins", "__init__.py"), - join(pfolder, "__init__.py")): - if not exists(ifile): - f = open(ifile, "wb") - f.close() - - except IOError, e: - self.logCritical(e) - return rootplugins + if home: + pfolder = join("userplugins", folder) + if not exists(pfolder): + makedirs(pfolder) + if not exists(join(pfolder, "__init__.py")): + f = open(join(pfolder, "__init__.py"), "wb") + f.close() else: pfolder = join(pypath, "module", "plugins", folder) @@ -82,27 +100,19 @@ class PluginManager: for f in listdir(pfolder): if (isfile(join(pfolder, f)) and f.endswith(".py") or f.endswith("_25.pyc") or f.endswith( "_26.pyc") or f.endswith("_27.pyc")) and not f.startswith("_"): + data = open(join(pfolder, f)) + content = data.read() + data.close() - try: - with open(join(pfolder, f)) as data: - content = data.read() - - except IOError, e: - self.logError(e) + if f.endswith("_25.pyc") and version_info[0:2] != (2, 5): continue - - if f.endswith("_25.pyc") and version_info[0:2] != (2, 5): #@TODO: Remove in 0.4.10 + elif f.endswith("_26.pyc") and version_info[0:2] != (2, 6): continue - - elif f.endswith("_26.pyc") and version_info[0:2] != (2, 6): #@TODO: Remove in 0.4.10 - continue - - elif f.endswith("_27.pyc") and version_info[0:2] != (2, 7): #@TODO: Remove in 0.4.10 + elif f.endswith("_27.pyc") and version_info[0:2] != (2, 7): continue name = f[:-3] - if name[-1] == ".": - name = name[:-4] + if name[-1] == ".": name = name[:-4] version = self.VERSION.findall(content) if version: @@ -110,33 +120,38 @@ class PluginManager: else: version = 0 - if rootplugins and name in rootplugins: - if rootplugins[name]['version'] >= version: + # home contains plugins from pyload root + if home and name in home: + if home[name]["v"] >= version: continue + if name in IGNORE or (folder, name) in IGNORE: + continue + plugins[name] = {} - plugins[name]['version'] = version + plugins[name]["v"] = version module = f.replace(".pyc", "").replace(".py", "") # the plugin is loaded from user directory - plugins[name]['user'] = True if rootplugins else False - plugins[name]['name'] = module - - pattern = self.PATTERN.findall(content) + plugins[name]["user"] = True if home else False + plugins[name]["name"] = module if pattern: - pattern = pattern[0][1] + pattern = self.PATTERN.findall(content) + + if pattern: + pattern = pattern[0][1] + else: + pattern = "^unmachtable$" + + plugins[name]["pattern"] = pattern try: - regexp = re.compile(pattern) + plugins[name]["re"] = re.compile(pattern) except: - self.log.error(_("%s has a invalid pattern") % name) - pattern = r'^unmatchable$' - regexp = re.compile(pattern) + self.log.error(_("%s has a invalid pattern.") % name) - plugins[name]['pattern'] = pattern - plugins[name]['re'] = regexp # internals have no config if folder == "internal": @@ -145,19 +160,24 @@ class PluginManager: config = self.CONFIG.findall(content) if config: - try: - config = literal_eval(config[0].strip().replace("\n", "").replace("\r", "")) - desc = self.DESC.findall(content) - desc = desc[0][1] if desc else "" + config = literal_eval(config[0].strip().replace("\n", "").replace("\r", "")) + desc = self.DESC.findall(content) + desc = desc[0][1] if desc else "" - if type(config[0]) == tuple: - config = [list(x) for x in config] - else: - config = [list(config)] + if type(config[0]) == tuple: + config = [list(x) for x in config] + else: + config = [list(config)] - if folder not in ("accounts", "internal") and not [True for item in config if item[0] == "activated"]: - config.insert(0, ["activated", "bool", "Activated", False if folder == "hooks" else True]) + if folder == "hooks": + append = True + for item in config: + if item[0] == "activated": append = False + # activated flag missing + if append: config.append(["activated", "bool", "Activated", False]) + + try: self.core.config.addPluginConfig(name, config, desc) except: self.log.error("Invalid config in %s: %s" % (name, config)) @@ -172,8 +192,9 @@ class PluginManager: except: self.log.error("Invalid config in %s: %s" % (name, config)) - if not rootplugins and plugins: #: Double check - plugins.update(self.parse(folder, plugins)) + if not home: + temp = self.parse(folder, pattern, plugins) + plugins.update(temp) return plugins @@ -188,13 +209,13 @@ class PluginManager: if type(url) not in (str, unicode, buffer): continue found = False - if last and last[1]['re'].match(url): + if last and last[1]["re"].match(url): res.append((url, last[0])) continue for name, value in chain(self.crypterPlugins.iteritems(), self.hosterPlugins.iteritems(), - self.containerPlugins.iteritems()): - if 're' in value and value['re'].match(url): #@TODO: Rewrite this check to report missing __pattern__ attribute alert + self.containerPlugins.iteritems()): + if value["re"].match(url): res.append((url, name)) last = (name, value) found = True @@ -205,38 +226,34 @@ class PluginManager: return res - def findPlugin(self, name, pluginlist=("hoster", "crypter", "container")): for ptype in pluginlist: if name in self.plugins[ptype]: return self.plugins[ptype][name], ptype return None, None - def getPlugin(self, name, original=False): """return plugin module from hoster|decrypter|container""" plugin, type = self.findPlugin(name) if not plugin: - self.log.warning("Plugin %s not found" % name) - plugin = self.hosterPlugins['BasePlugin'] + self.log.warning("Plugin %s not found." % name) + plugin = self.hosterPlugins["BasePlugin"] if "new_module" in plugin and not original: - return plugin['new_module'] + return plugin["new_module"] return self.loadModule(type, name) - def getPluginName(self, name): """ used to obtain new name if other plugin was injected""" plugin, type = self.findPlugin(name) if "new_name" in plugin: - return plugin['new_name'] + return plugin["new_name"] return name - def loadModule(self, type, name): """ Returns loaded module for plugin @@ -245,41 +262,26 @@ class PluginManager: """ plugins = self.plugins[type] if name in plugins: - if "module" in plugins[name]: - return plugins[name]['module'] - + if "module" in plugins[name]: return plugins[name]["module"] try: - module = __import__(self.ROOT + "%s.%s" % (type, plugins[name]['name']), globals(), locals(), - plugins[name]['name']) - + module = __import__(self.ROOT + "%s.%s" % (type, plugins[name]["name"]), globals(), locals(), + plugins[name]["name"]) + plugins[name]["module"] = module #cache import, maybe unneeded + return module except Exception, e: - self.log.error(_("Error importing plugin: [%(type)s] %(name)s (v%(version).2f) | %(errmsg)s") - % {'name': name, 'type': type, 'version': plugins[name]['version'], "errmsg": str(e)}) + self.log.error(_("Error importing %(name)s: %(msg)s") % {"name": name, "msg": str(e)}) if self.core.debug: print_exc() - else: - plugins[name]['module'] = module #: cache import, maybe unneeded - - self.log.debug(_("Loaded plugin: [%(type)s] %(name)s (v%(version).2f)") - % {'name': name, 'type': type, 'version': plugins[name]['version']}) - return module - - def loadClass(self, type, name): """Returns the class of a plugin with the same name""" module = self.loadModule(type, name) - if module: - return getattr(module, name) - else: - return None - + if module: return getattr(module, name) def getAccountPlugins(self): """return list of account plugin names""" return self.accountPlugins.keys() - def find_module(self, fullname, path=None): #redirecting imports if necesarry if fullname.startswith(self.ROOT) or fullname.startswith(self.USERROOT): #seperate pyload plugins @@ -292,10 +294,10 @@ class PluginManager: if type in self.plugins and name in self.plugins[type]: #userplugin is a newer version - if not user and self.plugins[type][name]['user']: + if not user and self.plugins[type][name]["user"]: return self #imported from userdir, but pyloads is newer - if user and not self.plugins[type][name]['user']: + if user and not self.plugins[type][name]["user"]: return self @@ -306,8 +308,7 @@ class PluginManager: newname = name.replace(self.ROOT, self.USERROOT) else: newname = name.replace(self.USERROOT, self.ROOT) - else: - newname = name + else: newname = name base, plugin = newname.rsplit(".", 1) @@ -323,45 +324,40 @@ class PluginManager: def reloadPlugins(self, type_plugins): """ reloads and reindexes plugins """ - if not type_plugins: - return None + if not type_plugins: return False self.log.debug("Request reload of plugins: %s" % type_plugins) - flag = True as_dict = {} - for t,n in type_plugins: if t in as_dict: as_dict[t].append(n) else: as_dict[t] = [n] - for type in as_dict.iterkeys(): - # we do not reload hooks or internals, would cause to much side effects - if type in ("hooks", "internal"): - flag = False - continue + # we do not reload hooks or internals, would cause to much side effects + if "hooks" in as_dict or "internal" in as_dict: + return False + for type in as_dict.iterkeys(): for plugin in as_dict[type]: if plugin in self.plugins[type]: if "module" in self.plugins[type][plugin]: - self.log.debug("Reloading module %s" % plugin) - reload(self.plugins[type][plugin]['module']) + self.log.debug("Reloading %s" % plugin) + reload(self.plugins[type][plugin]["module"]) - #index creation - self.plugins[type] = self.parse(type) - - if type is "accounts": #@TODO: Remove this check in 0.4.10 - self.accountPlugins = self.plugins[type] - else: - setattr(self, "%sPlugins" % type, self.plugins[type]) + #index creation + self.plugins["crypter"] = self.crypterPlugins = self.parse("crypter", pattern=True) + self.plugins["container"] = self.containerPlugins = self.parse("container", pattern=True) + self.plugins["hoster"] = self.hosterPlugins = self.parse("hoster", pattern=True) + self.plugins["captcha"] = self.captchaPlugins = self.parse("captcha") + self.plugins["accounts"] = self.accountPlugins = self.parse("accounts") - if "accounts" in as_dict: #: accounts needs to be reloaded + if "accounts" in as_dict: #accounts needs to be reloaded self.core.accountManager.initPlugins() self.core.scheduler.addJob(0, self.core.accountManager.getAccountInfos) - return flag + return True @@ -375,9 +371,10 @@ if __name__ == "__main__": a = time() - test = ["http://www.youtube.com/watch?v=%s" % x for x in xrange(0, 100)] + test = ["http://www.youtube.com/watch?v=%s" % x for x in range(0, 100)] print p.parseUrls(test) b = time() print b - a, "s" + -- cgit v1.2.3