Use six module

This commit is contained in:
Ruud committed 2014-10-06 17:30:58 +02:00
1 parent c50c290c3e
commit 97099f4d69
13 files changed
+50 -40

No files matched your search

+2 -2
View File
@@ -1,7 +1,7 @@
from base64 import b16encode, b32decode from base64 import b16encode, b32decode
from datetime import timedelta from datetime import timedelta
from hashlib import sha1 from hashlib import sha1
from urlparse import urlparse from six.moves import urllib
import os import os
from couchpotato.core._base.downloader.main import DownloaderBase, ReleaseDownloadList from couchpotato.core._base.downloader.main import DownloaderBase, ReleaseDownloadList
@@ -62,7 +62,7 @@ class rTorrent(DownloaderBase):
if self.conf('ssl') and url.startswith('httprpc://'): if self.conf('ssl') and url.startswith('httprpc://'):
url = url.replace('httprpc://', 'httprpc+https://') url = url.replace('httprpc://', 'httprpc+https://')
parsed = urlparse(url) parsed = urllib.urlparse(url)
# rpc_url is only used on http/https scgi pass-through # rpc_url is only used on http/https scgi pass-through
if parsed.scheme in ['http', 'https']: if parsed.scheme in ['http', 'https']:
+15 -9
View File
@@ -1,11 +1,11 @@
from string import ascii_letters, digits from string import ascii_letters, digits
from urllib import quote_plus
import os import os
import re import re
import traceback import traceback
import unicodedata import unicodedata
from chardet import detect from chardet import detect
from six.moves import urllib
from couchpotato.core.logger import CPLog from couchpotato.core.logger import CPLog
import six import six
@@ -16,7 +16,7 @@ log = CPLog(__name__)
def toSafeString(original): def toSafeString(original):
valid_chars = "-_.() %s%s" % (ascii_letters, digits) valid_chars = "-_.() %s%s" % (ascii_letters, digits)
cleaned_filename = unicodedata.normalize('NFKD', toUnicode(original)).encode('ASCII', 'ignore') 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()) return ' '.join(valid_string.split())
@@ -29,7 +29,7 @@ def simplifyString(original):
def toUnicode(original, *args): def toUnicode(original, *args):
try: try:
if isinstance(original, unicode): if isinstance(original, six.text_type):
return original return original
else: else:
try: try:
@@ -49,7 +49,7 @@ def toUnicode(original, *args):
def toUTF8(original): def toUTF8(original):
try: try:
if isinstance(original, str) and len(original) > 0: if isinstance(original, six.binary_type) and len(original) > 0:
# Try to detect # Try to detect
detected = detect(original) detected = detect(original)
return original.decode(detected.get('encoding')).encode('utf-8') return original.decode(detected.get('encoding')).encode('utf-8')
@@ -63,11 +63,16 @@ def ss(original, *args):
u_original = toUnicode(original, *args) u_original = toUnicode(original, *args)
try: try:
from couchpotato.environment import Env if isinstance(u_original, six.text_type):
return u_original.encode(Env.get('encoding')) u_original = u_original.encode('unicode_escape')
else:
u_original = u_original
return six.u(u_original)
except Exception as e: except Exception as e:
log.debug('Failed ss encoding char, force UTF8: %s', e) log.debug('Failed ss encoding char, force UTF8: %s', e)
try: try:
from couchpotato.environment import Env
return u_original.encode(Env.get('encoding'), 'replace') return u_original.encode(Env.get('encoding'), 'replace')
except: except:
return u_original.encode('utf-8', 'replace') return u_original.encode('utf-8', 'replace')
@@ -83,7 +88,7 @@ def sp(path, *args):
if os.path.sep == '/' and '\\' in path: if os.path.sep == '/' and '\\' in path:
path = '/' + path.replace(':', '').replace('\\', '/') path = '/' + path.replace(':', '').replace('\\', '/')
path = os.path.normpath(ss(path, *args)) path = os.path.normpath(path)
# Remove any trailing path separators # Remove any trailing path separators
if path != os.path.sep: if path != os.path.sep:
@@ -125,14 +130,15 @@ def stripAccents(s):
def tryUrlencode(s): def tryUrlencode(s):
new = six.u('') new = six.u('')
if isinstance(s, dict): 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)) new += six.u('&%s=%s') % (key, tryUrlencode(value))
return new[1:] return new[1:]
else: else:
for letter in ss(s): for letter in ss(s):
letter = six.unichr(letter)
try: try:
new += quote_plus(letter) new += urllib.parse.quote_plus(letter)
except: except:
new += letter new += letter
+4 -4
View File
@@ -1,7 +1,7 @@
from urllib import unquote
import re import re
from couchpotato.core.helpers.encoding import toUnicode from couchpotato.core.helpers.encoding import toUnicode
from six.moves import urllib
from couchpotato.core.helpers.variable import natsortKey from couchpotato.core.helpers.variable import natsortKey
@@ -10,7 +10,7 @@ def getParams(params):
reg = re.compile('^[a-z0-9_\.]+$') reg = re.compile('^[a-z0-9_\.]+$')
# Sort keys # Sort keys
param_keys = params.keys() param_keys = list(params.keys())
param_keys.sort(key = natsortKey) param_keys.sort(key = natsortKey)
temp = {} temp = {}
@@ -28,7 +28,7 @@ def getParams(params):
for item in nested: for item in nested:
if item is nested[-1]: if item is nested[-1]:
current[item] = toUnicode(unquote(value)) current[item] = toUnicode(urllib.unquote(value))
else: else:
try: try:
current[item] current[item]
@@ -37,7 +37,7 @@ def getParams(params):
current = current[item] current = current[item]
else: else:
temp[param] = toUnicode(unquote(value)) temp[param] = toUnicode(urllib.unquote(value))
if temp[param].lower() in ['true', 'false']: if temp[param].lower() in ['true', 'false']:
temp[param] = temp[param].lower() != 'false' temp[param] = temp[param].lower() != 'false'
+1 -1
View File
@@ -132,7 +132,7 @@ class Loader(object):
return False return False
try: try:
# Load single file plugin # Load single file plugin
if isinstance(module.autoload, (str, unicode)): if isinstance(module.autoload, (six.string_types, six.text_type)):
getattr(module, module.autoload)() getattr(module, module.autoload)()
# Load folder plugin # Load folder plugin
else: else:
@@ -1,4 +1,4 @@
from urlparse import urlparse from six.moves import urllib
import json import json
import re import re
import time import time
@@ -50,7 +50,7 @@ class Provider(Plugin):
if Env.get('dev'): return True if Env.get('dev'): return True
now = time.time() now = time.time()
host = urlparse(test_url).hostname host = urllib.urlparse(test_url).hostname
if self.last_available_check.get(host) < now - 900: if self.last_available_check.get(host) < now - 900:
self.last_available_check[host] = now self.last_available_check[host] = now
@@ -219,7 +219,7 @@ class YarrProvider(Provider):
if provider and provider == self.getName(): if provider and provider == self.getName():
return self return self
hostname = urlparse(url).hostname hostname = urllib.urlparse(url).hostname
if host and hostname in host: if host and hostname in host:
return self return self
else: else:
@@ -1,4 +1,4 @@
from urlparse import urlparse from six.moves import urllib
import time import time
import traceback import traceback
import re import re
@@ -97,7 +97,7 @@ class Base(NZBProvider, RSS):
results.append({ results.append({
'id': nzb_id, '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': toUnicode(name),
'name_extra': name_extra, 'name_extra': name_extra,
'age': self.calculateAge(int(time.mktime(parse(date).timetuple()))), 'age': self.calculateAge(int(time.mktime(parse(date).timetuple()))),
@@ -175,7 +175,7 @@ class Base(NZBProvider, RSS):
return '&apikey=%s' % host['api_key'] return '&apikey=%s' % host['api_key']
def download(self, url = '', nzb_id = ''): def download(self, url = '', nzb_id = ''):
host = urlparse(url).hostname host = urllib.urlparse(url).hostname
if self.limits_reached.get(host): if self.limits_reached.get(host):
# Try again in 3 hours # Try again in 3 hours
@@ -1,4 +1,4 @@
from urlparse import urlparse, parse_qs from six.moves import urllib
import time import time
from couchpotato.core.event import fireEvent from couchpotato.core.event import fireEvent
@@ -52,7 +52,7 @@ class Base(NZBProvider, RSS):
for nzb in nzbs: for nzb in nzbs:
enclosure = self.getElement(nzb, 'enclosure').attrib 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({ results.append({
'id': nzb_id, 'id': nzb_id,
@@ -1,4 +1,4 @@
import htmlentitydefs from six.moves import html_entities
import json import json
import re import re
import time import time
@@ -145,15 +145,15 @@ class Base(TorrentProvider):
# character reference # character reference
try: try:
if txt[:3] == "&#x": if txt[:3] == "&#x":
return unichr(int(txt[3:-1], 16)) return six.unichr(int(txt[3:-1], 16))
else: else:
return unichr(int(txt[2:-1])) return six.unichr(int(txt[2:-1]))
except ValueError: except ValueError:
pass pass
else: else:
# named entity # named entity
try: try:
txt = unichr(htmlentitydefs.name2codepoint[txt[1:-1]]) txt = six.unichr(html_entities.name2codepoint[txt[1:-1]])
except KeyError: except KeyError:
pass pass
return txt # leave as is return txt # leave as is
@@ -1,4 +1,4 @@
from urlparse import urlparse from six.moves import urllib
import re import re
import traceback import traceback
@@ -45,7 +45,7 @@ class Base(TorrentProvider):
results.append({ results.append({
'id': torrent.get('torrent_id'), 'id': torrent.get('torrent_id'),
'protocol': 'torrent' if re.match('^(http|https|ftp)://.*$', torrent.get('download_url')) else 'torrent_magnet', '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')), 'name': toUnicode(torrent.get('release_name')),
'url': torrent.get('download_url'), 'url': torrent.get('download_url'),
'detail_url': torrent.get('details_url'), 'detail_url': torrent.get('details_url'),
@@ -1,4 +1,4 @@
from urlparse import urlparse from six.moves import urllib
from couchpotato.core.event import addEvent, fireEvent from couchpotato.core.event import addEvent, fireEvent
from couchpotato.core.helpers.encoding import simplifyString from couchpotato.core.helpers.encoding import simplifyString
@@ -34,7 +34,7 @@ class UserscriptBase(Plugin):
def belongsTo(self, url): def belongsTo(self, url):
host = urlparse(url).hostname host = urllib.urlparse(url).hostname
host_split = host.split('.') host_split = host.split('.')
if len(host_split) > 2: if len(host_split) > 2:
host = host[len(host_split[0]):] host = host[len(host_split[0]):]
@@ -1,5 +1,5 @@
from datetime import timedelta, datetime from datetime import timedelta, datetime
from urlparse import urlparse from six.moves import urllib
import traceback import traceback
from couchpotato.core.helpers.variable import cleanHost from couchpotato.core.helpers.variable import cleanHost
@@ -106,7 +106,7 @@ class PlexServer(object):
def createHost(self, host, port = None): def createHost(self, host, port = None):
h = cleanHost(host) h = cleanHost(host)
p = urlparse(h) p = urllib.urlparse(h)
h = h.rstrip('/') h = h.rstrip('/')
if port and not p.port: if port and not p.port:
+3 -4
View File
@@ -1,6 +1,5 @@
import threading import threading
from urllib import quote from six.moves import urllib
from urlparse import urlparse
import glob import glob
import inspect import inspect
import os.path import os.path
@@ -183,13 +182,13 @@ class Plugin(object):
# http request # http request
def urlopen(self, url, timeout = 30, data = None, headers = None, files = None, show_error = True, stream = False): 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 headers: headers = {}
if not data: data = {} if not data: data = {}
# Fill in some headers # 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 '')) 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)) headers['Referer'] = headers.get('Referer', '%s://%s' % (parsed_url.scheme, host))
+7 -2
View File
@@ -6,7 +6,9 @@ from couchpotato.api import addApiView
from couchpotato.core.event import addEvent, fireEvent from couchpotato.core.event import addEvent, fireEvent
from couchpotato.core.helpers.database import HashIndex from couchpotato.core.helpers.database import HashIndex
from couchpotato.core.helpers.encoding import toUnicode from couchpotato.core.helpers.encoding import toUnicode
from six.moves import configparser
from couchpotato.core.helpers.variable import mergeDicts, tryInt, tryFloat from couchpotato.core.helpers.variable import mergeDicts, tryInt, tryFloat
import six
class Settings(object): class Settings(object):
@@ -62,7 +64,7 @@ class Settings(object):
def setFile(self, config_file): def setFile(self, config_file):
self.file = config_file self.file = config_file
self.p = ConfigParser.RawConfigParser() self.p = configparser.RawConfigParser()
self.p.read(config_file) self.p.read(config_file)
from couchpotato.core.logger import CPLog from couchpotato.core.logger import CPLog
@@ -148,7 +150,10 @@ class Settings(object):
return tryFloat(self.p.get(section, option)) return tryFloat(self.p.get(section, option))
def getUnicode(self, 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() return toUnicode(value).strip()
def getValues(self): def getValues(self):