From 97099f4d69991e47881cdf9ab37387b2a1f29922 Mon Sep 17 00:00:00 2001 From: Ruud Date: Mon, 6 Oct 2014 17:30:58 +0200 Subject: [PATCH] Use six module --- couchpotato/core/downloaders/rtorrent_.py | 4 ++-- couchpotato/core/helpers/encoding.py | 24 ++++++++++++------- couchpotato/core/helpers/request.py | 8 +++---- couchpotato/core/loader.py | 2 +- .../core/media/_base/providers/base.py | 6 ++--- .../core/media/_base/providers/nzb/newznab.py | 6 ++--- .../media/_base/providers/nzb/omgwtfnzbs.py | 4 ++-- .../_base/providers/torrent/passthepopcorn.py | 8 +++---- .../_base/providers/torrent/torrentpotato.py | 4 ++-- .../media/_base/providers/userscript/base.py | 4 ++-- couchpotato/core/notifications/plex/server.py | 4 ++-- couchpotato/core/plugins/base.py | 7 +++--- couchpotato/core/settings.py | 9 +++++-- 13 files changed, 50 insertions(+), 40 deletions(-) diff --git a/couchpotato/core/downloaders/rtorrent_.py b/couchpotato/core/downloaders/rtorrent_.py index 7474697f..35d3ff55 100644 --- a/couchpotato/core/downloaders/rtorrent_.py +++ b/couchpotato/core/downloaders/rtorrent_.py @@ -1,7 +1,7 @@ from base64 import b16encode, b32decode from datetime import timedelta from hashlib import sha1 -from urlparse import urlparse +from six.moves import urllib import os from couchpotato.core._base.downloader.main import DownloaderBase, ReleaseDownloadList @@ -62,7 +62,7 @@ class rTorrent(DownloaderBase): if self.conf('ssl') and url.startswith('httprpc://'): url = url.replace('httprpc://', 'httprpc+https://') - parsed = urlparse(url) + parsed = urllib.urlparse(url) # rpc_url is only used on http/https scgi pass-through if parsed.scheme in ['http', 'https']: diff --git a/couchpotato/core/helpers/encoding.py b/couchpotato/core/helpers/encoding.py index 6c4faf8f..2e7a5fcf 100644 --- a/couchpotato/core/helpers/encoding.py +++ b/couchpotato/core/helpers/encoding.py @@ -1,11 +1,11 @@ from string import ascii_letters, digits -from urllib import quote_plus import os import re import traceback import unicodedata from chardet import detect +from six.moves import urllib from couchpotato.core.logger import CPLog import six @@ -16,7 +16,7 @@ log = CPLog(__name__) def toSafeString(original): valid_chars = "-_.() %s%s" % (ascii_letters, digits) cleaned_filename = unicodedata.normalize('NFKD', toUnicode(original)).encode('ASCII', 'ignore') - valid_string = ''.join(c for c in cleaned_filename if c in valid_chars) + valid_string = ''.join(list(six.unichr(c) for c in cleaned_filename if six.unichr(c) in valid_chars)) return ' '.join(valid_string.split()) @@ -29,7 +29,7 @@ def simplifyString(original): def toUnicode(original, *args): try: - if isinstance(original, unicode): + if isinstance(original, six.text_type): return original else: try: @@ -49,7 +49,7 @@ def toUnicode(original, *args): def toUTF8(original): try: - if isinstance(original, str) and len(original) > 0: + if isinstance(original, six.binary_type) and len(original) > 0: # Try to detect detected = detect(original) return original.decode(detected.get('encoding')).encode('utf-8') @@ -63,11 +63,16 @@ def ss(original, *args): u_original = toUnicode(original, *args) try: - from couchpotato.environment import Env - return u_original.encode(Env.get('encoding')) + if isinstance(u_original, six.text_type): + u_original = u_original.encode('unicode_escape') + else: + u_original = u_original + + return six.u(u_original) except Exception as e: log.debug('Failed ss encoding char, force UTF8: %s', e) try: + from couchpotato.environment import Env return u_original.encode(Env.get('encoding'), 'replace') except: return u_original.encode('utf-8', 'replace') @@ -83,7 +88,7 @@ def sp(path, *args): if os.path.sep == '/' and '\\' in path: path = '/' + path.replace(':', '').replace('\\', '/') - path = os.path.normpath(ss(path, *args)) + path = os.path.normpath(path) # Remove any trailing path separators if path != os.path.sep: @@ -125,14 +130,15 @@ def stripAccents(s): def tryUrlencode(s): new = six.u('') if isinstance(s, dict): - for key, value in s.items(): + for key, value in list(s.items()): new += six.u('&%s=%s') % (key, tryUrlencode(value)) return new[1:] else: for letter in ss(s): + letter = six.unichr(letter) try: - new += quote_plus(letter) + new += urllib.parse.quote_plus(letter) except: new += letter diff --git a/couchpotato/core/helpers/request.py b/couchpotato/core/helpers/request.py index 4c0add18..d732596a 100644 --- a/couchpotato/core/helpers/request.py +++ b/couchpotato/core/helpers/request.py @@ -1,7 +1,7 @@ -from urllib import unquote import re from couchpotato.core.helpers.encoding import toUnicode +from six.moves import urllib from couchpotato.core.helpers.variable import natsortKey @@ -10,7 +10,7 @@ def getParams(params): reg = re.compile('^[a-z0-9_\.]+$') # Sort keys - param_keys = params.keys() + param_keys = list(params.keys()) param_keys.sort(key = natsortKey) temp = {} @@ -28,7 +28,7 @@ def getParams(params): for item in nested: if item is nested[-1]: - current[item] = toUnicode(unquote(value)) + current[item] = toUnicode(urllib.unquote(value)) else: try: current[item] @@ -37,7 +37,7 @@ def getParams(params): current = current[item] else: - temp[param] = toUnicode(unquote(value)) + temp[param] = toUnicode(urllib.unquote(value)) if temp[param].lower() in ['true', 'false']: temp[param] = temp[param].lower() != 'false' diff --git a/couchpotato/core/loader.py b/couchpotato/core/loader.py index f3b5f775..6bfb8d41 100644 --- a/couchpotato/core/loader.py +++ b/couchpotato/core/loader.py @@ -132,7 +132,7 @@ class Loader(object): return False try: # Load single file plugin - if isinstance(module.autoload, (str, unicode)): + if isinstance(module.autoload, (six.string_types, six.text_type)): getattr(module, module.autoload)() # Load folder plugin else: diff --git a/couchpotato/core/media/_base/providers/base.py b/couchpotato/core/media/_base/providers/base.py index 587545c8..02ff60a8 100644 --- a/couchpotato/core/media/_base/providers/base.py +++ b/couchpotato/core/media/_base/providers/base.py @@ -1,4 +1,4 @@ -from urlparse import urlparse +from six.moves import urllib import json import re import time @@ -50,7 +50,7 @@ class Provider(Plugin): if Env.get('dev'): return True now = time.time() - host = urlparse(test_url).hostname + host = urllib.urlparse(test_url).hostname if self.last_available_check.get(host) < now - 900: self.last_available_check[host] = now @@ -219,7 +219,7 @@ class YarrProvider(Provider): if provider and provider == self.getName(): return self - hostname = urlparse(url).hostname + hostname = urllib.urlparse(url).hostname if host and hostname in host: return self else: diff --git a/couchpotato/core/media/_base/providers/nzb/newznab.py b/couchpotato/core/media/_base/providers/nzb/newznab.py index 62b787d8..1b3190e1 100644 --- a/couchpotato/core/media/_base/providers/nzb/newznab.py +++ b/couchpotato/core/media/_base/providers/nzb/newznab.py @@ -1,4 +1,4 @@ -from urlparse import urlparse +from six.moves import urllib import time import traceback import re @@ -97,7 +97,7 @@ class Base(NZBProvider, RSS): results.append({ 'id': nzb_id, - 'provider_extra': urlparse(host['host']).hostname or host['host'], + 'provider_extra': urllib.urlparse(host['host']).hostname or host['host'], 'name': toUnicode(name), 'name_extra': name_extra, 'age': self.calculateAge(int(time.mktime(parse(date).timetuple()))), @@ -175,7 +175,7 @@ class Base(NZBProvider, RSS): return '&apikey=%s' % host['api_key'] def download(self, url = '', nzb_id = ''): - host = urlparse(url).hostname + host = urllib.urlparse(url).hostname if self.limits_reached.get(host): # Try again in 3 hours diff --git a/couchpotato/core/media/_base/providers/nzb/omgwtfnzbs.py b/couchpotato/core/media/_base/providers/nzb/omgwtfnzbs.py index bac0614d..f4ab8631 100644 --- a/couchpotato/core/media/_base/providers/nzb/omgwtfnzbs.py +++ b/couchpotato/core/media/_base/providers/nzb/omgwtfnzbs.py @@ -1,4 +1,4 @@ -from urlparse import urlparse, parse_qs +from six.moves import urllib import time from couchpotato.core.event import fireEvent @@ -52,7 +52,7 @@ class Base(NZBProvider, RSS): for nzb in nzbs: enclosure = self.getElement(nzb, 'enclosure').attrib - nzb_id = parse_qs(urlparse(self.getTextElement(nzb, 'link')).query).get('id')[0] + nzb_id = urllib.parse_qs(urllib.urlparse(self.getTextElement(nzb, 'link')).query).get('id')[0] results.append({ 'id': nzb_id, diff --git a/couchpotato/core/media/_base/providers/torrent/passthepopcorn.py b/couchpotato/core/media/_base/providers/torrent/passthepopcorn.py index 40a55674..6059dd37 100644 --- a/couchpotato/core/media/_base/providers/torrent/passthepopcorn.py +++ b/couchpotato/core/media/_base/providers/torrent/passthepopcorn.py @@ -1,4 +1,4 @@ -import htmlentitydefs +from six.moves import html_entities import json import re import time @@ -145,15 +145,15 @@ class Base(TorrentProvider): # character reference try: if txt[:3] == "&#x": - return unichr(int(txt[3:-1], 16)) + return six.unichr(int(txt[3:-1], 16)) else: - return unichr(int(txt[2:-1])) + return six.unichr(int(txt[2:-1])) except ValueError: pass else: # named entity try: - txt = unichr(htmlentitydefs.name2codepoint[txt[1:-1]]) + txt = six.unichr(html_entities.name2codepoint[txt[1:-1]]) except KeyError: pass return txt # leave as is diff --git a/couchpotato/core/media/_base/providers/torrent/torrentpotato.py b/couchpotato/core/media/_base/providers/torrent/torrentpotato.py index d1426765..b3b99198 100644 --- a/couchpotato/core/media/_base/providers/torrent/torrentpotato.py +++ b/couchpotato/core/media/_base/providers/torrent/torrentpotato.py @@ -1,4 +1,4 @@ -from urlparse import urlparse +from six.moves import urllib import re import traceback @@ -45,7 +45,7 @@ class Base(TorrentProvider): results.append({ 'id': torrent.get('torrent_id'), 'protocol': 'torrent' if re.match('^(http|https|ftp)://.*$', torrent.get('download_url')) else 'torrent_magnet', - 'provider_extra': urlparse(host['host']).hostname or host['host'], + 'provider_extra': urllib.urlparse(host['host']).hostname or host['host'], 'name': toUnicode(torrent.get('release_name')), 'url': torrent.get('download_url'), 'detail_url': torrent.get('details_url'), diff --git a/couchpotato/core/media/_base/providers/userscript/base.py b/couchpotato/core/media/_base/providers/userscript/base.py index 6491ac34..7b1ad71f 100644 --- a/couchpotato/core/media/_base/providers/userscript/base.py +++ b/couchpotato/core/media/_base/providers/userscript/base.py @@ -1,4 +1,4 @@ -from urlparse import urlparse +from six.moves import urllib from couchpotato.core.event import addEvent, fireEvent from couchpotato.core.helpers.encoding import simplifyString @@ -34,7 +34,7 @@ class UserscriptBase(Plugin): def belongsTo(self, url): - host = urlparse(url).hostname + host = urllib.urlparse(url).hostname host_split = host.split('.') if len(host_split) > 2: host = host[len(host_split[0]):] diff --git a/couchpotato/core/notifications/plex/server.py b/couchpotato/core/notifications/plex/server.py index cd11f49b..90aa155c 100644 --- a/couchpotato/core/notifications/plex/server.py +++ b/couchpotato/core/notifications/plex/server.py @@ -1,5 +1,5 @@ from datetime import timedelta, datetime -from urlparse import urlparse +from six.moves import urllib import traceback from couchpotato.core.helpers.variable import cleanHost @@ -106,7 +106,7 @@ class PlexServer(object): def createHost(self, host, port = None): h = cleanHost(host) - p = urlparse(h) + p = urllib.urlparse(h) h = h.rstrip('/') if port and not p.port: diff --git a/couchpotato/core/plugins/base.py b/couchpotato/core/plugins/base.py index 704aa62c..4b169fbf 100644 --- a/couchpotato/core/plugins/base.py +++ b/couchpotato/core/plugins/base.py @@ -1,6 +1,5 @@ import threading -from urllib import quote -from urlparse import urlparse +from six.moves import urllib import glob import inspect import os.path @@ -183,13 +182,13 @@ class Plugin(object): # http request def urlopen(self, url, timeout = 30, data = None, headers = None, files = None, show_error = True, stream = False): - url = quote(ss(url), safe = "%/:=&?~#+!$,;'@()*[]") + url = urllib.parse.quote(ss(url), safe = "%/:=&?~#+!$,;'@()*[]") if not headers: headers = {} if not data: data = {} # Fill in some headers - parsed_url = urlparse(url) + parsed_url = urllib.parse.urlparse(url) host = '%s%s' % (parsed_url.hostname, (':' + str(parsed_url.port) if parsed_url.port else '')) headers['Referer'] = headers.get('Referer', '%s://%s' % (parsed_url.scheme, host)) diff --git a/couchpotato/core/settings.py b/couchpotato/core/settings.py index 16432479..c40e3e57 100644 --- a/couchpotato/core/settings.py +++ b/couchpotato/core/settings.py @@ -6,7 +6,9 @@ from couchpotato.api import addApiView from couchpotato.core.event import addEvent, fireEvent from couchpotato.core.helpers.database import HashIndex from couchpotato.core.helpers.encoding import toUnicode +from six.moves import configparser from couchpotato.core.helpers.variable import mergeDicts, tryInt, tryFloat +import six class Settings(object): @@ -62,7 +64,7 @@ class Settings(object): def setFile(self, config_file): self.file = config_file - self.p = ConfigParser.RawConfigParser() + self.p = configparser.RawConfigParser() self.p.read(config_file) from couchpotato.core.logger import CPLog @@ -148,7 +150,10 @@ class Settings(object): return tryFloat(self.p.get(section, option)) def getUnicode(self, section, option): - value = self.p.get(section, option).decode('unicode_escape') + value = self.p.get(section, option) + if six.PY2: + value = value.decode('unicode_escape') + return toUnicode(value).strip() def getValues(self):