diff options
-rw-r--r-- | docs/module_overview.rst | 1 | ||||
-rw-r--r-- | module/plugins/Account.py | 19 | ||||
-rw-r--r-- | module/plugins/Hook.py | 25 | ||||
-rw-r--r-- | module/plugins/Plugin.py | 141 | ||||
-rw-r--r-- | module/plugins/PluginStorage.py | 29 | ||||
-rw-r--r-- | module/plugins/hooks/Ev0InFetcher.py | 3 | ||||
-rw-r--r-- | module/web/api_app.py | 4 |
7 files changed, 104 insertions, 118 deletions
diff --git a/docs/module_overview.rst b/docs/module_overview.rst index a3c31a88b..d51202c88 100644 --- a/docs/module_overview.rst +++ b/docs/module_overview.rst @@ -7,6 +7,7 @@ You can find an overview of some important classes here: :toctree: module module.Api.Api + module.plugins.Plugin.Base module.plugins.Plugin.Plugin module.plugins.Crypter.Crypter module.plugins.Account.Account diff --git a/module/plugins/Account.py b/module/plugins/Account.py index 202a7ad67..c147404e0 100644 --- a/module/plugins/Account.py +++ b/module/plugins/Account.py @@ -22,13 +22,14 @@ from time import time from traceback import print_exc from threading import RLock +from Plugin import Base from module.utils import compare_time, parseFileSize, lock class WrongPassword(Exception): pass -class Account(): +class Account(Base): """ Base class for every Account plugin. Just overwrite `login` and cookies will be stored and account becomes accessible in\ @@ -48,8 +49,9 @@ class Account(): def __init__(self, manager, accounts): + Base.__init__(self, manager.core) + self.manager = manager - self.core = manager.core self.accounts = {} self.infos = {} # cache for account information self.lock = RLock() @@ -288,16 +290,3 @@ class Account(): return False return True - - #log functions - def logInfo(self, msg): - self.core.log.info("%s: %s" % (self.__name__, msg)) - - def logWarning(self, msg): - self.core.log.warning("%s: %s" % (self.__name__, msg)) - - def logError(self, msg): - self.core.log.error("%s: %s" % (self.__name__, msg)) - - def logDebug(self, msg): - self.core.log.debug("%s: %s" % (self.__name__, msg)) diff --git a/module/plugins/Hook.py b/module/plugins/Hook.py index 85fb49190..fdcaccfe3 100644 --- a/module/plugins/Hook.py +++ b/module/plugins/Hook.py @@ -21,6 +21,8 @@ from thread import start_new_thread from traceback import print_exc +from Plugin import Base + class Expose(object): """ used for decoration to declare rpc services """ @@ -33,7 +35,7 @@ def threaded(f): return start_new_thread(f, args, kwargs) return run -class Hook(): +class Hook(Base): """ Base class for hook plugins. """ @@ -58,9 +60,7 @@ class Hook(): interval = 60 def __init__(self, core, manager): - self.core = core - self.log = core.log - self.config = core.config + Base.__init__(self, core) #: Provide information in dict here, usable by API `getInfo` self.info = None @@ -122,23 +122,6 @@ class Hook(): """ checks if hook is activated""" return self.config.getPlugin(self.__name__, "activated") - def getConfig(self, option): - """ gets config values """ - return self.config.getPlugin(self.__name__, option) - - def setConfig(self, option, value): - """ sets config value """ - self.config.setPlugin(self.__name__, option, value) - - #log functions - def logInfo(self, msg): - self.log.info("%s: %s" % (self.__name__, msg)) - def logWarning(self, msg): - self.log.warning("%s: %s" % (self.__name__, msg)) - def logError(self, msg): - self.log.error("%s: %s" % (self.__name__, msg)) - def logDebug(self, msg): - self.log.debug("%s: %s" % (self.__name__, msg)) #event methods - overwrite these if needed def coreReady(self): diff --git a/module/plugins/Plugin.py b/module/plugins/Plugin.py index 2ecd3215c..720d64091 100644 --- a/module/plugins/Plugin.py +++ b/module/plugins/Plugin.py @@ -67,7 +67,82 @@ class SkipDownload(Exception): """ raised when download should be skipped """ -class Plugin(object): +class Base(object): + """ + A Base class with log/config/db methods *all* plugin types can use + """ + + def __init__(self, core): + #: Core instance + self.core = core + #: logging instance + self.log = core.log + #: core config + self.config = core.config + + #log functions + def logInfo(self, msg): + self.log.info("%s: %s" % (self.__name__, msg)) + + def logWarning(self, msg): + self.log.warning("%s: %s" % (self.__name__, msg)) + + def logError(self, msg): + self.log.error("%s: %s" % (self.__name__, msg)) + + def logDebug(self, msg): + self.log.debug("%s: %s" % (self.__name__, msg)) + + + def setConf(self, option, value): + """ see `setConfig` """ + self.core.config.setPlugin(self.__name__, option, value) + + def setConfig(self, option, value): + """ Set config value for current plugin + + :param option: + :param value: + :return: + """ + self.setConf(option, value) + + def getConf(self, option): + """ see `getConfig` """ + return self.core.config.getPlugin(self.__name__, option) + + def getConfig(self, option): + """ Returns config value for current plugin + + :param option: + :return: + """ + return self.getConf(option) + + def setStorage(self, key, value): + """ Saves a value persistently to the database """ + self.core.db.setStorage(self.__name__, key, value) + + def store(self, key, value): + """ same as `setStorage` """ + self.core.db.setStorage(self.__name__, key, value) + + def getStorage(self, key=None, default=None): + """ Retrieves saved value or dict of all saved entries if key is None """ + if key is not None: + return self.core.db.getStorage(self.__name__, key) or default + return self.core.db.getStorage(self.__name__, key) + + def retrieve(self, *args, **kwargs): + """ same as `getStorage` """ + return self.getStorage(*args, **kwargs) + + def delStorage(self, key): + """ Delete entry in db """ + self.core.db.delStorage(self.__name__, key) + + +class Plugin(Base): """ Base plugin for hoster/crypter. Overwrite `process` / `decrypt` in your subclassed plugin. @@ -82,8 +157,7 @@ class Plugin(object): __author_mail__ = ("RaNaN@pyload.org", "spoob@pyload.org", "mkaay@mkaay.de") def __init__(self, pyfile): - self.config = pyfile.m.core.config - self.core = pyfile.m.core + Base.__init__(self, pyfile.m.core) self.wantReconnect = False #: enables simultaneous processing of multiple downloads @@ -120,8 +194,6 @@ class Plugin(object): else: self.req = pyfile.m.core.requestFactory.getRequest(self.__name__) - self.log = pyfile.m.core.log - #: associated pyfile instance, see `PyFile` self.pyfile = pyfile self.thread = None # holds thread in future @@ -195,23 +267,6 @@ class Plugin(object): return True, 10 - def setConf(self, option, value): - """ sets a config value """ - self.config.setPlugin(self.__name__, option, value) - - def getConf(self, option): - """ gets a config value """ - return self.config.getPlugin(self.__name__, option) - - def setConfig(self, option, value): - """ sets a config value """ - self.setConf(option, value) - - def getConfig(self, option): - """ gets a config value """ - return self.getConf(option) - - def setWait(self, seconds, reconnect=False): """Set a specific wait time later used with `wait` @@ -277,7 +332,8 @@ class Plugin(object): if self.cTask: self.cTask.correct() - def decryptCaptcha(self, url, get={}, post={}, cookies=False, forceUser=False, imgtype='jpg', result_type='textual'): + def decryptCaptcha(self, url, get={}, post={}, cookies=False, forceUser=False, imgtype='jpg', + result_type='textual'): """ Loads a captcha and decrypts it with ocr, plugin, user input :param url: url of captcha image @@ -292,16 +348,16 @@ class Plugin(object): :return: result of decrypting """ - + img = self.load(url, get=get, post=post, cookies=cookies) - id = ("%.2f" % time())[-6:].replace(".","") - temp_file = open(join("tmp", "tmpCaptcha_%s_%s.%s" % (self.__name__, id, imgtype)), "wb") + id = ("%.2f" % time())[-6:].replace(".", "") + temp_file = open(join("tmp", "tmpCaptcha_%s_%s.%s" % (self.__name__, id, imgtype)), "wb") temp_file.write(img) temp_file.close() has_plugin = self.__name__ in self.core.pluginManager.captchaPlugins - + if self.core.captcha: Ocr = self.core.pluginManager.getCaptchaPlugin(self.__name__) else: @@ -310,16 +366,15 @@ class Plugin(object): if Ocr and not forceUser: sleep(randint(3000, 5000) / 1000.0) if self.pyfile.abort: raise Abort - + ocr = Ocr() result = ocr.get_captcha(temp_file.name) else: - captchaManager = self.core.captchaManager task = captchaManager.newTask(img, imgtype, temp_file.name, result_type) self.cTask = task captchaManager.handleCaptcha(task) - + while task.isWaiting(): if self.pyfile.abort: captchaManager.removeTask(task) @@ -335,16 +390,15 @@ class Plugin(object): elif not task.result: self.fail(_("No captcha result obtained in appropiate time by any of the plugins.")) - result = task.result self.log.debug("Received captcha result: %s" % str(result)) if not self.core.debug: - try: - remove(temp_file.name) - except: - pass - + try: + remove(temp_file.name) + except: + pass + return result @@ -539,10 +593,10 @@ class Plugin(object): if pyfile != self.pyfile and pyfile.name == self.pyfile.name and pyfile.package().folder == pack.folder: if pyfile.status in (0, 12): #finished or downloading raise SkipDownload(pyfile.pluginname) - elif pyfile.status in (5, 7) and starting: #a download is waiting/starting and was appenrently started before + elif pyfile.status in ( + 5, 7) and starting: #a download is waiting/starting and was appenrently started before raise SkipDownload(pyfile.pluginname) - download_folder = self.config['general']['download_folder'] location = save_join(download_folder, pack.folder, self.pyfile.name) @@ -558,17 +612,6 @@ class Plugin(object): self.log.debug("File %s not skipped, because it does not exists." % self.pyfile.name) - - #log functions - def logInfo(self, msg): - self.log.info("%s: %s" % (self.__name__, msg)) - def logWarning(self, msg): - self.log.warning("%s: %s" % (self.__name__, msg)) - def logError(self, msg): - self.log.error("%s: %s" % (self.__name__, msg)) - def logDebug(self, msg): - self.log.debug("%s: %s" % (self.__name__, msg)) - def clean(self): """ clean everything and remove references """ if hasattr(self, "pyfile"): diff --git a/module/plugins/PluginStorage.py b/module/plugins/PluginStorage.py deleted file mode 100644 index 02498a446..000000000 --- a/module/plugins/PluginStorage.py +++ /dev/null @@ -1,29 +0,0 @@ -# -*- 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 <http://www.gnu.org/licenses/>. - - @author: mkaay -""" - -class PluginStorage(): - def getStorage(self, key=None, default=None): - if key is not None: - return self.core.db.getStorage(self.__name__, key) or default - return self.core.db.getStorage(self.__name__, key) - - def setStorage(self, key, value): - self.core.db.setStorage(self.__name__, key, value) - - def delStorage(self, key): - self.core.db.delStorage(self.__name__, key) diff --git a/module/plugins/hooks/Ev0InFetcher.py b/module/plugins/hooks/Ev0InFetcher.py index bcd53722c..5941cf38c 100644 --- a/module/plugins/hooks/Ev0InFetcher.py +++ b/module/plugins/hooks/Ev0InFetcher.py @@ -19,9 +19,8 @@ from module.lib import feedparser from time import mktime, time from module.plugins.Hook import Hook -from module.plugins.PluginStorage import PluginStorage -class Ev0InFetcher(Hook, PluginStorage): +class Ev0InFetcher(Hook): __name__ = "Ev0InFetcher" __version__ = "0.2" __description__ = """checks rss feeds for ev0.in""" diff --git a/module/web/api_app.py b/module/web/api_app.py index 156922d6a..12fffe099 100644 --- a/module/web/api_app.py +++ b/module/web/api_app.py @@ -46,10 +46,10 @@ def call_api(func, args=""): s = s.get_by_id(request.POST['session']) if not s or not s.get("authenticated", False): - return HTTPError(401, json_dumps("Unauthorized")) + return HTTPError(403, json_dumps("Forbidden")) if not PYLOAD.isAuthorized(func, {"role": s["role"], "permission": s["perms"]}): - return HTTPError(403, json_dumps("Forbidden")) + return HTTPError(401, json_dumps("Unauthorized")) args = args.split("/")[1:] kwargs = {} |