summaryrefslogtreecommitdiffstats
path: root/module/plugins/internal/Plugin.py
diff options
context:
space:
mode:
Diffstat (limited to 'module/plugins/internal/Plugin.py')
-rw-r--r--module/plugins/internal/Plugin.py71
1 files changed, 46 insertions, 25 deletions
diff --git a/module/plugins/internal/Plugin.py b/module/plugins/internal/Plugin.py
index 7b45c40a8..e4c16846c 100644
--- a/module/plugins/internal/Plugin.py
+++ b/module/plugins/internal/Plugin.py
@@ -6,7 +6,9 @@ import datetime
import inspect
import os
import re
+import sys
import urllib
+import unicodedata
if os.name != "nt":
import grp
@@ -22,7 +24,7 @@ def decode(string, encoding='utf8'):
if type(string) is str:
return string.decode(encoding, "replace")
else:
- return string
+ return unicode(string)
#@TODO: Move to utils in 0.4.10
@@ -31,7 +33,7 @@ def encode(string, encoding='utf8'):
if type(string) is unicode:
return string.encode(encoding, "replace")
else:
- return string
+ return str(string)
#@TODO: Move to utils in 0.4.10
@@ -51,11 +53,40 @@ def fixurl(url):
return html_unescape(urllib.unquote(url.decode('unicode-escape'))).strip().rstrip('/')
+def fixname(m):
+ m = unicodedata.normalize('NFKD', m)
+ output = ''
+ for c in m:
+ if not unicodedata.combining(c):
+ output += c
+ return output
+
+
#@TODO: Move to utils in 0.4.10
def timestamp():
return int(time.time() * 1000)
+#@TODO: Move to utils in 0.4.10
+def which(program):
+ """
+ Works exactly like the unix command which
+ Courtesy of http://stackoverflow.com/a/377028/675646
+ """
+ isExe = lambda x: os.path.isfile(x) and os.access(x, os.X_OK)
+
+ fpath, fname = os.path.split(program)
+
+ if fpath:
+ if isExe(program):
+ return program
+ else:
+ for path in os.environ['PATH'].split(os.pathsep):
+ exe_file = os.path.join(path.strip('"'), program)
+ if isExe(exe_file):
+ return exe_file
+
+
def seconds_to_midnight(gmt=0):
now = datetime.datetime.utcnow() + datetime.timedelta(hours=gmt)
@@ -145,8 +176,8 @@ def chunks(iterable, size):
class Plugin(object):
__name__ = "Plugin"
- __type__ = "hoster"
- __version__ = "0.30"
+ __type__ = "plugin"
+ __version__ = "0.33"
__status__ = "testing"
__pattern__ = r'^unmatchable$'
@@ -165,6 +196,11 @@ class Plugin(object):
self.init()
+ def __repr__(self):
+ return "<%(type)s %(name)s>" % {'type': self.__type__.capitalize()
+ 'name': self.__name__}
+
+
def _init(self, core):
self.pyload = core
self.info = {} #: Provide information in dict here
@@ -180,11 +216,10 @@ class Plugin(object):
def _log(self, level, plugintype, pluginname, messages):
log = getattr(self.pyload.log, level)
- msg = encode(" | ".join((a if isinstance(a, basestring) else str(a)).strip() for a in messages if a))
- log("%(plugintype)s %(pluginname)s%(id)s: %(msg)s"
+ msg = " | ".join(encode(a).strip() for a in messages if a)
+ log("%(plugintype)s %(pluginname)s%: %(msg)s"
% {'plugintype': plugintype.upper(),
'pluginname': pluginname,
- 'id' : ("[%s]" % self.pyfile.id) if hasattr(self, 'pyfile') else "",
'msg' : msg})
@@ -287,21 +322,10 @@ class Plugin(object):
self.pyload.db.delStorage(self.__name__, key)
- def fail(self, reason):
+ def fail(self, msg):
"""
- Fail and give reason
+ Fail and give msg
"""
- raise Fail(encode(reason)) #@TODO: Remove `encode` in 0.4.10
-
-
- def error(self, reason="", type=_("Parse")):
- if not reason:
- type = _("Unknown")
-
- msg = _("%s error") % type.strip().capitalize() if type else _("Error")
- msg += (": %s" % reason.strip()) if reason else ""
- msg += _(" | Plugin may be out of date")
-
raise Fail(encode(msg)) #@TODO: Remove `encode` in 0.4.10
@@ -318,13 +342,10 @@ class Plugin(object):
:param decode: Wether to decode the output according to http header, should be True in most cases
:return: Loaded content
"""
- if hasattr(self, 'pyfile') and self.pyfile.abort:
- self.abort()
-
url = fixurl(url)
if not url or not isinstance(url, basestring):
- self.fail(_("No url given"))
+ self.fail(_("No given url"))
if self.pyload.debug:
self.log_debug("LOAD URL " + url,
@@ -345,7 +366,7 @@ class Plugin(object):
#@TODO: Move to network in 0.4.10
if isinstance(decode, basestring):
- res = decode(res, decode)
+ res = sys.modules[self.__name__].decode(res, decode) #@TODO: See #1787, use utils.decode() in 0.4.10
if self.pyload.debug:
frame = inspect.currentframe()