py3k port helpers
This commit is contained in:
@@ -60,10 +60,7 @@ addView('', index)
|
||||
|
||||
# API docs
|
||||
def apiDocs():
|
||||
routes = []
|
||||
|
||||
for route in api.iterkeys():
|
||||
routes.append(route)
|
||||
routes = list(api.keys())
|
||||
|
||||
if api_docs.get(''):
|
||||
del api_docs['']
|
||||
|
||||
@@ -15,6 +15,7 @@ import time
|
||||
import traceback
|
||||
import version
|
||||
import zipfile
|
||||
from six.moves import filter
|
||||
|
||||
log = CPLog(__name__)
|
||||
|
||||
@@ -350,8 +351,8 @@ class SourceUpdater(BaseUpdater):
|
||||
try:
|
||||
if os.path.isdir(path):
|
||||
shutil.rmtree(path)
|
||||
except OSError, inst:
|
||||
os.chmod(inst.filename, 0777)
|
||||
except OSError as inst:
|
||||
os.chmod(inst.filename, 0o777)
|
||||
self.removeDir(path)
|
||||
|
||||
def getVersion(self):
|
||||
|
||||
@@ -261,7 +261,7 @@ class uTorrentAPI(object):
|
||||
|
||||
def set_torrent(self, hash, params):
|
||||
action = 'action=setprops&hash=%s' % hash
|
||||
for k, v in params.iteritems():
|
||||
for k, v in params.items():
|
||||
action += '&s=%s&v=%s' % (k, v)
|
||||
return self._request(action)
|
||||
|
||||
|
||||
@@ -29,7 +29,7 @@ def addEvent(name, handler, priority = 100):
|
||||
has_parent = hasattr(handler, 'im_self')
|
||||
parent = None
|
||||
if has_parent:
|
||||
parent = handler.im_self
|
||||
parent = handler.__self__
|
||||
bc = hasattr(parent, 'beforeCall')
|
||||
if bc: parent.beforeCall(handler)
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import os
|
||||
import re
|
||||
import traceback
|
||||
import unicodedata
|
||||
import six
|
||||
|
||||
log = CPLog(__name__)
|
||||
|
||||
@@ -29,7 +30,7 @@ def toUnicode(original, *args):
|
||||
return original
|
||||
else:
|
||||
try:
|
||||
return unicode(original, *args)
|
||||
return six.text_type(original, *args)
|
||||
except:
|
||||
try:
|
||||
return ek(original, *args)
|
||||
@@ -102,10 +103,10 @@ def stripAccents(s):
|
||||
|
||||
|
||||
def tryUrlencode(s):
|
||||
new = u''
|
||||
new = six.u('')
|
||||
if isinstance(s, dict):
|
||||
for key, value in s.iteritems():
|
||||
new += u'&%s=%s' % (key, tryUrlencode(value))
|
||||
for key, value in s.items():
|
||||
new += six.u('&%s=%s') % (key, tryUrlencode(value))
|
||||
|
||||
return new[1:]
|
||||
else:
|
||||
|
||||
@@ -9,7 +9,7 @@ def getParams(params):
|
||||
reg = re.compile('^[a-z0-9_\.]+$')
|
||||
|
||||
temp = {}
|
||||
for param, value in sorted(params.iteritems()):
|
||||
for param, value in sorted(params.items()):
|
||||
|
||||
nest = re.split("([\[\]]+)", param)
|
||||
if len(nest) > 1:
|
||||
@@ -42,9 +42,9 @@ def dictToList(params):
|
||||
|
||||
if type(params) is dict:
|
||||
new = {}
|
||||
for x, value in params.iteritems():
|
||||
for x, value in params.items():
|
||||
try:
|
||||
new_value = [dictToList(value[k]) for k in sorted(value.iterkeys(), cmp = natcmp)]
|
||||
new_value = [dictToList(value[k]) for k in sorted(value.keys(), cmp = natcmp)]
|
||||
except:
|
||||
new_value = value
|
||||
|
||||
|
||||
@@ -8,6 +8,8 @@ import random
|
||||
import re
|
||||
import string
|
||||
import sys
|
||||
import six
|
||||
from six.moves import map, zip
|
||||
|
||||
log = CPLog(__name__)
|
||||
|
||||
@@ -19,7 +21,7 @@ def fnEscape(pattern):
|
||||
def link(src, dst):
|
||||
if os.name == 'nt':
|
||||
import ctypes
|
||||
if ctypes.windll.kernel32.CreateHardLinkW(unicode(dst), unicode(src), 0) == 0: raise ctypes.WinError()
|
||||
if ctypes.windll.kernel32.CreateHardLinkW(six.text_type(dst), six.text_type(src), 0) == 0: raise ctypes.WinError()
|
||||
else:
|
||||
os.link(src, dst)
|
||||
|
||||
@@ -27,7 +29,7 @@ def link(src, dst):
|
||||
def symlink(src, dst):
|
||||
if os.name == 'nt':
|
||||
import ctypes
|
||||
if ctypes.windll.kernel32.CreateSymbolicLinkW(unicode(dst), unicode(src), 1 if os.path.isdir(src) else 0) in [0, 1280]: raise ctypes.WinError()
|
||||
if ctypes.windll.kernel32.CreateSymbolicLinkW(six.text_type(dst), six.text_type(src), 1 if os.path.isdir(src) else 0) in [0, 1280]: raise ctypes.WinError()
|
||||
else:
|
||||
os.symlink(src, dst)
|
||||
|
||||
@@ -253,7 +255,7 @@ def randomString(size = 8, chars = string.ascii_uppercase + string.digits):
|
||||
|
||||
def splitString(str, split_on = ',', clean = True):
|
||||
l = [x.strip() for x in str.split(split_on)] if str else []
|
||||
return filter(None, l) if clean else l
|
||||
return [x for x in l if x] if clean else l
|
||||
|
||||
|
||||
def dictIsSubset(a, b):
|
||||
|
||||
@@ -4,6 +4,7 @@ from importlib import import_module
|
||||
import os
|
||||
import sys
|
||||
import traceback
|
||||
import six
|
||||
|
||||
log = CPLog(__name__)
|
||||
|
||||
@@ -37,7 +38,7 @@ class Loader(object):
|
||||
self.paths['custom_plugins'] = (30, '', custom_plugin_dir)
|
||||
|
||||
# Loop over all paths and add to module list
|
||||
for plugin_type, plugin_tuple in self.paths.iteritems():
|
||||
for plugin_type, plugin_tuple in self.paths.items():
|
||||
priority, module, dir_name = plugin_tuple
|
||||
self.addFromDir(plugin_type, priority, module, dir_name)
|
||||
|
||||
@@ -45,7 +46,7 @@ class Loader(object):
|
||||
did_save = 0
|
||||
|
||||
for priority in sorted(self.modules):
|
||||
for module_name, plugin in sorted(self.modules[priority].iteritems()):
|
||||
for module_name, plugin in sorted(self.modules[priority].items()):
|
||||
|
||||
# Load module
|
||||
try:
|
||||
@@ -81,7 +82,7 @@ class Loader(object):
|
||||
for filename in os.listdir(root_path):
|
||||
path = os.path.join(root_path, filename)
|
||||
if os.path.isdir(path) and filename[:2] != '__':
|
||||
if u'__init__.py' in os.listdir(path):
|
||||
if six.u('__init__.py') in os.listdir(path):
|
||||
new_base_path = ''.join(s + '.' for s in base_path) + filename
|
||||
self.paths[new_base_path.replace('.', '_')] = (priority, new_base_path, path)
|
||||
|
||||
|
||||
@@ -51,7 +51,7 @@ class CPLog(object):
|
||||
else:
|
||||
msg = msg % ss(replace_tuple)
|
||||
except Exception as e:
|
||||
self.logger.error(u'Failed encoding stuff to log "%s": %s' % (msg, e))
|
||||
self.logger.error('Failed encoding stuff to log "%s": %s' % (msg, e))
|
||||
|
||||
if not Env.get('dev'):
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ from couchpotato.core.logger import CPLog
|
||||
from couchpotato.core.media._base.searcher.base import SearcherBase
|
||||
import datetime
|
||||
import re
|
||||
from six.moves import filter
|
||||
|
||||
log = CPLog(__name__)
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ from couchpotato.core.settings.model import Library, LibraryTitle, File
|
||||
from string import ascii_letters
|
||||
import time
|
||||
import traceback
|
||||
import six
|
||||
|
||||
log = CPLog(__name__)
|
||||
|
||||
@@ -109,7 +110,7 @@ class MovieLibraryPlugin(LibraryBase):
|
||||
t = LibraryTitle(
|
||||
title = title,
|
||||
simple_title = self.simplifyTitle(title),
|
||||
default = (len(default_title) == 0 and counter == 0) or len(titles) == 1 or title.lower() == toUnicode(default_title.lower()) or (toUnicode(default_title) == u'' and toUnicode(titles[0]) == title)
|
||||
default = (len(default_title) == 0 and counter == 0) or len(titles) == 1 or title.lower() == toUnicode(default_title.lower()) or (toUnicode(default_title) == six.u('') and toUnicode(titles[0]) == title)
|
||||
)
|
||||
library.titles.append(t)
|
||||
counter += 1
|
||||
|
||||
@@ -2,6 +2,7 @@ from couchpotato.core.helpers.variable import splitString
|
||||
from couchpotato.core.logger import CPLog
|
||||
from couchpotato.core.notifications.base import Notification
|
||||
import pynma
|
||||
import six
|
||||
|
||||
log = CPLog(__name__)
|
||||
|
||||
@@ -26,7 +27,7 @@ class NotifyMyAndroid(Notification):
|
||||
|
||||
successful = 0
|
||||
for key in keys:
|
||||
if not response[str(key)]['code'] == u'200':
|
||||
if not response[str(key)]['code'] == six.u('200'):
|
||||
log.error('Could not send notification to NotifyMyAndroid (%s). %s', (key, response[key]['message']))
|
||||
else:
|
||||
successful += 1
|
||||
|
||||
@@ -2,6 +2,7 @@ from couchpotato.core.helpers.variable import splitString
|
||||
from couchpotato.core.logger import CPLog
|
||||
from couchpotato.core.notifications.base import Notification
|
||||
from pynmwp import PyNMWP
|
||||
import six
|
||||
|
||||
log = CPLog(__name__)
|
||||
|
||||
@@ -17,7 +18,7 @@ class NotifyMyWP(Notification):
|
||||
response = p.push(application = self.default_title, event = message, description = message, priority = self.conf('priority'), batch_mode = len(keys) > 1)
|
||||
|
||||
for key in keys:
|
||||
if not response[key]['Code'] == u'200':
|
||||
if not response[key]['Code'] == six.u('200'):
|
||||
log.error('Could not send notification to NotifyMyWindowsPhone (%s). %s', (key, response[key]['message']))
|
||||
return False
|
||||
|
||||
|
||||
@@ -169,7 +169,7 @@ class Plugin(object):
|
||||
}
|
||||
method = 'post' if len(data) > 0 or files else 'get'
|
||||
|
||||
log.info('Opening url: %s %s, data: %s', (method, url, [x for x in data.iterkeys()] if isinstance(data, dict) else 'with data'))
|
||||
log.info('Opening url: %s %s, data: %s', (method, url, [x for x in data.keys()] if isinstance(data, dict) else 'with data'))
|
||||
response = r.request(method, url, verify = False, **kwargs)
|
||||
|
||||
data = response.content
|
||||
|
||||
@@ -4,6 +4,7 @@ from couchpotato.core.plugins.base import Plugin
|
||||
import ctypes
|
||||
import os
|
||||
import string
|
||||
import six
|
||||
|
||||
if os.name == 'nt':
|
||||
import imp
|
||||
@@ -96,7 +97,7 @@ class FileBrowser(Plugin):
|
||||
|
||||
def has_hidden_attribute(self, filepath):
|
||||
try:
|
||||
attrs = ctypes.windll.kernel32.GetFileAttributesW(unicode(filepath)) #@UndefinedVariable
|
||||
attrs = ctypes.windll.kernel32.GetFileAttributesW(six.text_type(filepath)) #@UndefinedVariable
|
||||
assert attrs != -1
|
||||
result = bool(attrs & 2)
|
||||
except (AttributeError, AssertionError):
|
||||
|
||||
@@ -223,7 +223,7 @@ class Manage(Plugin):
|
||||
groups = fireEvent('scanner.scan', folder = folder, files = files, single = True)
|
||||
|
||||
if groups:
|
||||
for group in groups.itervalues():
|
||||
for group in groups.values():
|
||||
if group['library'] and group['library'].get('identifier'):
|
||||
fireEvent('release.add', group = group)
|
||||
|
||||
|
||||
@@ -17,6 +17,8 @@ import re
|
||||
import shutil
|
||||
import time
|
||||
import traceback
|
||||
import six
|
||||
from six.moves import filter
|
||||
|
||||
log = CPLog(__name__)
|
||||
|
||||
@@ -787,19 +789,19 @@ Remove it if you want it to be renamed (again, or at least let it try again)
|
||||
replacements['cd_nr'] = ''
|
||||
|
||||
replaced = toUnicode(string)
|
||||
for x, r in replacements.iteritems():
|
||||
for x, r in replacements.items():
|
||||
if x in ['thename', 'namethe']:
|
||||
continue
|
||||
if r is not None:
|
||||
replaced = replaced.replace(u'<%s>' % toUnicode(x), toUnicode(r))
|
||||
replaced = replaced.replace(six.u('<%s>') % toUnicode(x), toUnicode(r))
|
||||
else:
|
||||
#If information is not available, we don't want the tag in the filename
|
||||
replaced = replaced.replace('<' + x + '>', '')
|
||||
|
||||
replaced = self.replaceDoubles(replaced.lstrip('. '))
|
||||
for x, r in replacements.iteritems():
|
||||
for x, r in replacements.items():
|
||||
if x in ['thename', 'namethe']:
|
||||
replaced = replaced.replace(u'<%s>' % toUnicode(x), toUnicode(r))
|
||||
replaced = replaced.replace(six.u('<%s>') % toUnicode(x), toUnicode(r))
|
||||
replaced = re.sub(r"[\x00:\*\?\"<>\|]", '', replaced)
|
||||
|
||||
sep = self.conf('foldersep') if folder else self.conf('separator')
|
||||
|
||||
@@ -15,6 +15,7 @@ import re
|
||||
import threading
|
||||
import time
|
||||
import traceback
|
||||
from six.moves import filter, map, zip
|
||||
|
||||
log = CPLog(__name__)
|
||||
|
||||
@@ -187,7 +188,7 @@ class Scanner(Plugin):
|
||||
|
||||
# Group files minus extension
|
||||
ignored_identifiers = []
|
||||
for identifier, group in movie_files.iteritems():
|
||||
for identifier, group in movie_files.items():
|
||||
if identifier not in group['identifiers'] and len(identifier) > 0: group['identifiers'].append(identifier)
|
||||
|
||||
log.debug('Grouping files: %s', identifier)
|
||||
@@ -228,7 +229,7 @@ class Scanner(Plugin):
|
||||
|
||||
# Group the files based on the identifier
|
||||
delete_identifiers = []
|
||||
for identifier, found_files in path_identifiers.iteritems():
|
||||
for identifier, found_files in path_identifiers.items():
|
||||
log.debug('Grouping files on identifier: %s', identifier)
|
||||
|
||||
group = movie_files.get(identifier)
|
||||
@@ -251,7 +252,7 @@ class Scanner(Plugin):
|
||||
|
||||
# Group based on folder
|
||||
delete_identifiers = []
|
||||
for identifier, found_files in path_identifiers.iteritems():
|
||||
for identifier, found_files in path_identifiers.items():
|
||||
log.debug('Grouping files on foldername: %s', identifier)
|
||||
|
||||
for ff in found_files:
|
||||
|
||||
@@ -108,7 +108,7 @@ class StatusPlugin(Plugin):
|
||||
|
||||
db = get_session()
|
||||
|
||||
for identifier, label in self.statuses.iteritems():
|
||||
for identifier, label in self.statuses.items():
|
||||
s = db.query(Status).filter_by(identifier = identifier).first()
|
||||
if not s:
|
||||
log.info('Creating status: %s', label)
|
||||
|
||||
@@ -45,7 +45,7 @@ class Subtitle(Plugin):
|
||||
if self.isDisabled(): return
|
||||
|
||||
try:
|
||||
available_languages = sum(group['subtitle_language'].itervalues(), [])
|
||||
available_languages = sum(group['subtitle_language'].values(), [])
|
||||
downloaded = []
|
||||
files = [toUnicode(x) for x in group['files']['movie']]
|
||||
log.debug('Searching for subtitles for: %s', files)
|
||||
|
||||
@@ -3,6 +3,7 @@ from couchpotato.core.helpers.variable import tryInt, splitString
|
||||
from couchpotato.core.logger import CPLog
|
||||
from couchpotato.core.providers.automation.base import Automation
|
||||
import re
|
||||
from six.moves import filter
|
||||
|
||||
log = CPLog(__name__)
|
||||
|
||||
|
||||
@@ -81,7 +81,7 @@ class CouchPotatoApi(MovieProvider):
|
||||
|
||||
result = self.getJsonData(self.urls['info'] % identifier, headers = self.getRequestHeaders())
|
||||
if result:
|
||||
return dict((k, v) for k, v in result.iteritems() if v)
|
||||
return dict((k, v) for k, v in result.items() if v)
|
||||
|
||||
return {}
|
||||
|
||||
|
||||
@@ -107,7 +107,7 @@ class OMDBAPI(MovieProvider):
|
||||
'writers': splitString(movie.get('Writer', '')),
|
||||
'actors': splitString(movie.get('Actors', '')),
|
||||
}
|
||||
movie_data = dict((k, v) for k, v in movie_data.iteritems() if v)
|
||||
movie_data = dict((k, v) for k, v in movie_data.items() if v)
|
||||
except:
|
||||
log.error('Failed parsing IMDB API json: %s', traceback.format_exc())
|
||||
|
||||
|
||||
@@ -145,7 +145,7 @@ class TheMovieDb(MovieProvider):
|
||||
'actor_roles': actors
|
||||
}
|
||||
|
||||
movie_data = dict((k, v) for k, v in movie_data.iteritems() if v)
|
||||
movie_data = dict((k, v) for k, v in movie_data.items() if v)
|
||||
|
||||
# Add alternative names
|
||||
if extended:
|
||||
|
||||
@@ -8,6 +8,7 @@ import json
|
||||
import re
|
||||
import time
|
||||
import traceback
|
||||
import six
|
||||
|
||||
log = CPLog(__name__)
|
||||
|
||||
@@ -178,7 +179,7 @@ class PassThePopcorn(TorrentProvider):
|
||||
except KeyError:
|
||||
pass
|
||||
return text # leave as is
|
||||
return re.sub("&#?\w+;", fixup, u'%s' % text)
|
||||
return re.sub("&#?\w+;", fixup, six.u('%s') % text)
|
||||
|
||||
def unicodeToASCII(self, text):
|
||||
import unicodedata
|
||||
|
||||
@@ -5,6 +5,7 @@ from couchpotato.core.logger import CPLog
|
||||
from couchpotato.core.providers.torrent.base import TorrentMagnetProvider
|
||||
import re
|
||||
import traceback
|
||||
import six
|
||||
|
||||
log = CPLog(__name__)
|
||||
|
||||
@@ -73,7 +74,7 @@ class ThePirateBay(TorrentMagnetProvider):
|
||||
download = result.find(href = re.compile('magnet:'))
|
||||
|
||||
try:
|
||||
size = re.search('Size (?P<size>.+),', unicode(result.select('font.detDesc')[0])).group('size')
|
||||
size = re.search('Size (?P<size>.+),', six.text_type(result.select('font.detDesc')[0])).group('size')
|
||||
except:
|
||||
continue
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ from couchpotato.core.helpers.variable import tryInt
|
||||
from couchpotato.core.logger import CPLog
|
||||
from couchpotato.core.providers.torrent.base import TorrentProvider
|
||||
import traceback
|
||||
import six
|
||||
|
||||
log = CPLog(__name__)
|
||||
|
||||
@@ -53,7 +54,7 @@ class TorrentShack(TorrentProvider):
|
||||
|
||||
results.append({
|
||||
'id': link['href'].replace('torrents.php?torrentid=', ''),
|
||||
'name': unicode(link.span.string).translate({ord(u'\xad'): None}),
|
||||
'name': six.text_type(link.span.string).translate({ord(six.u('\xad')): None}),
|
||||
'url': self.urls['download'] % url['href'],
|
||||
'detail_url': self.urls['download'] % link['href'],
|
||||
'size': self.parseSize(result.find_all('td')[4].string),
|
||||
|
||||
@@ -77,7 +77,7 @@ class Settings(object):
|
||||
|
||||
self.addSection(section_name)
|
||||
|
||||
for option_name, option in options.iteritems():
|
||||
for option_name, option in options.items():
|
||||
self.setDefault(section_name, option_name, option.get('default', ''))
|
||||
|
||||
# Migrate old settings from old location to the new location
|
||||
|
||||
Reference in New Issue
Block a user