py3k port helpers

This commit is contained in:
Ruud committed 2014-01-20 23:58:54 +01:00
1 parent 08e2a3a883
commit 29a34fef8c
28 files changed
+58 -44

No files matched your search

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