diff options
Diffstat (limited to 'module/plugins/Plugin.py')
-rw-r--r-- | module/plugins/Plugin.py | 89 |
1 files changed, 57 insertions, 32 deletions
diff --git a/module/plugins/Plugin.py b/module/plugins/Plugin.py index 68b2311b3..8a2554ea0 100644 --- a/module/plugins/Plugin.py +++ b/module/plugins/Plugin.py @@ -14,8 +14,7 @@ if os.name != "nt": from itertools import islice -from module.utils import save_join, save_path, fs_encode, fs_decode - +from module.utils import safe_join, safe_path, fs_encode, fs_decode def chunks(iterable, size): it = iter(iterable) @@ -74,7 +73,7 @@ class Base(object): def setConf(self, option, value): """ see `setConfig` """ - self.core.config.setPlugin(self.__name__, option, value) + self.config.setPlugin(self.__name__, option, value) def setConfig(self, option, value): """ Set config value for current plugin @@ -85,9 +84,10 @@ class Base(object): """ self.setConf(option, value) + #: Deprecated method def getConf(self, option): """ see `getConfig` """ - return self.core.config.getPlugin(self.__name__, option) + return self.getConfig(option) def getConfig(self, option): """ Returns config value for current plugin @@ -95,7 +95,7 @@ class Base(object): :param option: :return: """ - return self.getConf(option) + return self.config.getPlugin(self.__name__, option) def setStorage(self, key, value): """ Saves a value persistently to the database """ @@ -127,7 +127,7 @@ class Plugin(Base): """ __name__ = "Plugin" __type__ = "hoster" - __version__ = "0.4" + __version__ = "0.5" __pattern__ = None __config__ = [("name", "type", "desc", "default")] @@ -140,10 +140,13 @@ class Plugin(Base): def __init__(self, pyfile): Base.__init__(self, pyfile.m.core) + #: engage wan reconnection self.wantReconnect = False - #: enables simultaneous processing of multiple downloads + + #: enable simultaneous processing of multiple downloads self.multiDL = True self.limitDL = 0 + #: chunk limit self.chunkLimit = 1 self.resumeDownload = False @@ -152,7 +155,9 @@ class Plugin(Base): self.waitUntil = 0 self.waiting = False - self.ocr = None #captcha reader instance + #: captcha reader instance + self.ocr = None + #: account handler instance, see :py:class:`Account` self.account = pyfile.m.core.accountManager.getAccountPlugin(self.__name__) @@ -161,7 +166,9 @@ class Plugin(Base): #: username/login self.user = None - if self.account and not self.account.canUse(): self.account = None + if self.account and not self.account.canUse(): + self.account = None + if self.account: self.user, data = self.account.selectAccount() #: Browser instance, see `network.Browser` @@ -177,18 +184,28 @@ class Plugin(Base): #: associated pyfile instance, see `PyFile` self.pyfile = pyfile + self.thread = None # holds thread in future #: location where the last call to download was saved self.lastDownload = "" #: re match of the last call to `checkDownload` self.lastCheck = None + #: js engine, see `JsEngine` self.js = self.core.js - self.cTask = None #captcha task - self.retries = 0 # amount of retries already made - self.html = None # some plugins store html code here + #: captcha task + self.cTask = None + + #: amount of retries already made + self.retries = 0 + + #: some plugins store html code here + self.html = None + + #: quick caller for API + self.api = self.core.api self.init() @@ -248,7 +265,7 @@ class Plugin(Base): return True, 10 - def setWait(self, seconds, reconnect=False): + def setWait(self, seconds, reconnect=None): """Set a specific wait time later used with `wait` :param seconds: wait time in seconds @@ -258,15 +275,23 @@ class Plugin(Base): self.wantReconnect = True self.pyfile.waitUntil = time() + int(seconds) - def wait(self): - """ waits the time previously set """ + def wait(self, seconds=None, reconnect=None): + """ Waits the time previously set or use these from arguments. See `setWait` + """ + if seconds: + self.setWait(seconds, reconnect) + + self._wait() + + def _wait(self): self.waiting = True self.pyfile.setStatus("waiting") while self.pyfile.waitUntil > time(): self.thread.m.reconnecting.wait(2) - if self.pyfile.abort: raise Abort + if self.pyfile.abort: + raise Abort if self.thread.m.reconnecting.isSet(): self.waiting = False self.wantReconnect = False @@ -372,7 +397,7 @@ class Plugin(Base): 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)) + self.logDebug("Received captcha result: %s" % str(result)) if not self.core.debug: try: @@ -462,23 +487,23 @@ class Plugin(Base): download_folder = self.config['general']['download_folder'] - location = save_join(download_folder, self.pyfile.package().folder) + location = safe_join(download_folder, self.pyfile.package().folder) if not exists(location): - makedirs(location, int(self.core.config['permission']['folder'], 8)) + makedirs(location, int(self.config['permission']['folder'], 8)) - if self.core.config['permission']['change_dl'] and os.name != "nt": + if self.config['permission']['change_dl'] and os.name != "nt": try: uid = getpwnam(self.config['permission']['user'])[2] gid = getgrnam(self.config['permission']['group'])[2] chown(location, uid, gid) except Exception, e: - self.log.warning(_("Setting User and Group failed: %s") % str(e)) + self.logWarning(_("Setting User and Group failed: %s") % str(e)) # convert back to unicode location = fs_decode(location) - name = save_path(self.pyfile.name) + name = safe_path(self.pyfile.name) filename = join(location, name) @@ -492,23 +517,23 @@ class Plugin(Base): self.pyfile.size = self.req.size if disposition and newname and newname != name: #triple check, just to be sure - self.log.info("%(name)s saved as %(newname)s" % {"name": name, "newname": newname}) + self.logInfo("%(name)s saved as %(newname)s" % {"name": name, "newname": newname}) self.pyfile.name = newname filename = join(location, newname) fs_filename = fs_encode(filename) - if self.core.config['permission']['change_file']: - chmod(fs_filename, int(self.core.config['permission']['file'], 8)) + if self.config['permission']['change_file']: + chmod(fs_filename, int(self.config['permission']['file'], 8)) - if self.core.config['permission']['change_dl'] and os.name != "nt": + if self.config['permission']['change_dl'] and os.name != "nt": try: uid = getpwnam(self.config['permission']['user'])[2] gid = getgrnam(self.config['permission']['group'])[2] chown(fs_filename, uid, gid) except Exception, e: - self.log.warning(_("Setting User and Group failed: %s") % str(e)) + self.logWarning(_("Setting User and Group failed: %s") % str(e)) self.lastDownload = filename return self.lastDownload @@ -531,12 +556,12 @@ class Plugin(Base): if api_size and api_size <= size: return None elif size > max_size and not read_size: return None - self.log.debug("Download Check triggered") + self.logDebug("Download Check triggered") f = open(lastDownload, "rb") content = f.read(read_size if read_size else -1) f.close() #produces encoding errors, better log to other file in the future? - #self.log.debug("Content: %s" % content) + #self.logDebug("Content: %s" % content) for name, rule in rules.iteritems(): if type(rule) in (str, unicode): if rule in content: @@ -577,9 +602,9 @@ class Plugin(Base): raise SkipDownload(pyfile.pluginname) download_folder = self.config['general']['download_folder'] - location = save_join(download_folder, pack.folder, self.pyfile.name) + location = safe_join(download_folder, pack.folder, self.pyfile.name) - if starting and self.core.config['download']['skip_existing'] and exists(location): + if starting and self.config['download']['skip_existing'] and exists(location): size = os.stat(location).st_size if size >= self.pyfile.size: raise SkipDownload("File exists.") @@ -589,7 +614,7 @@ class Plugin(Base): if exists(location): raise SkipDownload(pyfile[0]) - self.log.debug("File %s not skipped, because it does not exists." % self.pyfile.name) + self.logDebug("File %s not skipped, because it does not exists." % self.pyfile.name) def clean(self): """ clean everything and remove references """ |