diff options
author | Walter Purcaro <vuolter@users.noreply.github.com> | 2015-12-20 23:13:55 +0100 |
---|---|---|
committer | Walter Purcaro <vuolter@users.noreply.github.com> | 2015-12-27 22:50:43 +0100 |
commit | 41da0b8a328e808fc2ec84a5718fd5f54098e630 (patch) | |
tree | a24c47fdcfcd0abe453e39e23168be8e7b40c128 /module/plugins/internal/Plugin.py | |
parent | [Crypter] Code cosmetics (diff) | |
download | pyload-41da0b8a328e808fc2ec84a5718fd5f54098e630.tar.xz |
[Plugin] Improve load routine and related
Diffstat (limited to 'module/plugins/internal/Plugin.py')
-rw-r--r-- | module/plugins/internal/Plugin.py | 168 |
1 files changed, 53 insertions, 115 deletions
diff --git a/module/plugins/internal/Plugin.py b/module/plugins/internal/Plugin.py index bf591d482..779865147 100644 --- a/module/plugins/internal/Plugin.py +++ b/module/plugins/internal/Plugin.py @@ -4,17 +4,22 @@ from __future__ import with_statement import inspect import os +import re if os.name is not "nt": import grp import pwd import pycurl +try: + import send2trash +except ImportError: + pass -import module.plugins.internal.utils as utils +import module.plugins.internal.misc as utils from module.plugins.Plugin import Abort, Fail, Reconnect, Retry, SkipDownload as Skip #@TODO: Remove in 0.4.10 -from module.plugins.internal.utils import * +from module.plugins.internal.misc import * #@NOTE: Don't use `*` class Plugin(object): @@ -46,10 +51,13 @@ class Plugin(object): def _init(self, core): - self.pyload = core - self.info = {} #: Provide information in dict here - self.req = None #: Browser instance, see `network.Browser` - self.last_html = None + self.pyload = core + self.db = DB(self) + self.config = Config(self) + self.info = {} #: Provide information in dict here + self.req = None #: Browser instance, see `network.Browser` + self.last_html = "" + self.last_header = {} def init(self): @@ -71,124 +79,71 @@ class Plugin(object): def log_debug(self, *args, **kwargs): self._log("debug", self.__type__, self.__name__, args) if self.pyload.debug and kwargs.get('trace'): - self.print_exc() + self._print_exc() def log_info(self, *args, **kwargs): self._log("info", self.__type__, self.__name__, args) if self.pyload.debug and kwargs.get('trace'): - self.print_exc() + self._print_exc() def log_warning(self, *args, **kwargs): self._log("warning", self.__type__, self.__name__, args) if self.pyload.debug and kwargs.get('trace'): - self.print_exc() + self._print_exc() def log_error(self, *args, **kwargs): self._log("error", self.__type__, self.__name__, args) if self.pyload.debug and kwargs.get('trace', True): - self.print_exc() + self._print_exc() def log_critical(self, *args, **kwargs): self._log("critical", self.__type__, self.__name__, args) if kwargs.get('trace', True): - self.print_exc() + self._print_exc() - def print_exc(self): + def _print_exc(self): frame = inspect.currentframe() print format_exc(frame.f_back) del frame - def set_permissions(self, path): - if not os.path.exists(path): - return - - try: - if self.pyload.config.get("permission", "change_file"): - if os.path.isfile(path): - os.chmod(path, int(self.pyload.config.get("permission", "file"), 8)) - - elif os.path.isdir(path): - os.chmod(path, int(self.pyload.config.get("permission", "folder"), 8)) - - except OSError, e: - self.log_warning(_("Setting path mode failed"), e) - - try: - if os.name is not "nt" and self.pyload.config.get("permission", "change_dl"): - uid = pwd.getpwnam(self.pyload.config.get("permission", "user"))[2] - gid = grp.getgrnam(self.pyload.config.get("permission", "group"))[2] - os.chown(path, uid, gid) - - except OSError, e: - self.log_warning(_("Setting owner and group failed"), e) - - - def set_config(self, option, value, plugin=None): - """ - Set config value for current plugin - - :param option: - :param value: - :return: - """ - self.pyload.api.setConfigValue(plugin or self.classname, option, value, section="plugin") - - - def get_config(self, option, default="", plugin=None): - """ - Returns config value for current plugin - - :param option: - :return: - """ + #@TODO: Move to misc + def remove(self, path, trash=False): #@TODO: Change to `trash=True` in 0.4.10 try: - return self.pyload.config.getPlugin(plugin or self.classname, option) - - except KeyError: - self.log_debug("Config option `%s` not found, use default `%s`" % (option, default or None)) #@TODO: Restore to `log_warning` in 0.4.10 - return default + remove(path, trash) + except (NameError, OSError), e: + self.log_warning(_("Error removing `%s`") % os.path.abspath(path), e) + return False - def store(self, key, value): - """ - Saves a value persistently to the database - """ - value = map(decode, value) if isiterable(value) else decode(value) - entry = json.dumps(value).encode('base64') - self.pyload.db.setStorage(self.classname, key, entry) + else: + self.log_info(_("Path deleted: ") + os.path.abspath(path)) + return True - def retrieve(self, key=None, default=None): - """ - Retrieves saved value or dict of all saved entries if key is None - """ - entry = self.pyload.db.getStorage(self.classname, key) + def set_permissions(self, path): + path = encode(path) - if key: - if entry is None: - value = default - else: - value = json.loads(entry.decode('base64')) - else: - if not entry: - value = default - else: - value = dict((k, json.loads(v.decode('base64'))) for k, v in value.items()) + if not exists(path): + return - return value + file_perms = False + dl_perms = False + if self.pyload.config.get("permission", "change_file"): + permission = self.pyload.config.get("permission", "folder" if os.path.isdir(path) else "file") + mode = int(permission, 8) + os.chmod(path, mode) - def delete(self, key): - """ - Delete entry in db - """ - self.pyload.db.delStorage(self.classname, key) + if os.name is not "nt" and self.pyload.config.get("permission", "change_dl"): + uid = pwd.getpwnam(self.pyload.config.get("permission", "user"))[2] + gid = grp.getgrnam(self.pyload.config.get("permission", "group"))[2] + os.chown(path, uid, gid) def fail(self, msg): @@ -240,7 +195,7 @@ class Plugin(object): req.http.c.setopt(pycurl.FOLLOWLOCATION, 1) elif type(redirect) is int: - maxredirs = self.get_config("maxredirs", default=5, plugin="UserAgentSwitcher") + maxredirs = self.pyload.api.getConfigValue("UserAgentSwitcher", "maxredirs", "plugin") or 5 req.http.c.setopt(pycurl.MAXREDIRS, maxredirs) #@TODO: Move to network in 0.4.10 @@ -257,8 +212,8 @@ class Plugin(object): frame = inspect.currentframe() try: - framefile = fs_join("tmp", self.classname, "%s_line%s.dump.html" % - (frame.f_back.f_code.co_name, frame.f_back.f_lineno)) + framefile = fsjoin("tmp", self.classname, "%s_line%s.dump.html" + % (frame.f_back.f_code.co_name, frame.f_back.f_lineno)) if not exists(os.path.join("tmp", self.classname)): os.makedirs(os.path.join("tmp", self.classname)) @@ -272,33 +227,16 @@ class Plugin(object): finally: del frame #: Delete the frame or it wont be cleaned - if not just_header: - return html - - else: - #@TODO: Move to network in 0.4.10 - header = {'code': req.code} - - for line in html.splitlines(): - line = line.strip() - if not line or ":" not in line: - continue - - key, none, value = line.partition(":") - - key = key.strip().lower() - value = value.strip() + #@TODO: Move to network in 0.4.10 + header = {'code': req.code} + header.update(parse_html_header(req.http.header)) - if key in header: - header_key = header.get(key) - if type(header_key) is list: - header_key.append(value) - else: - header[key] = [header_key, value] - else: - header[key] = value + self.last_header = header + if just_header: return header + else: + return html def clean(self): |