py3: Use compat.text_type instead of unicode
This commit is contained in:
parent
b9a5192d5f
commit
58527406c1
@ -4,6 +4,8 @@ import pygst
|
||||
pygst.require('0.10')
|
||||
import gst # noqa
|
||||
|
||||
from mopidy.utils import compat
|
||||
|
||||
|
||||
def calculate_duration(num_samples, sample_rate):
|
||||
"""Determine duration of samples using GStreamer helper for precise
|
||||
@ -18,7 +20,7 @@ def create_buffer(data, capabilites=None, timestamp=None, duration=None):
|
||||
"""
|
||||
buffer_ = gst.Buffer(data)
|
||||
if capabilites:
|
||||
if isinstance(capabilites, basestring):
|
||||
if isinstance(capabilites, compat.string_types):
|
||||
capabilites = gst.caps_from_string(capabilites)
|
||||
buffer_.set_caps(capabilites)
|
||||
if timestamp:
|
||||
|
||||
@ -10,7 +10,7 @@ import re
|
||||
from mopidy.config import keyring
|
||||
from mopidy.config.schemas import * # noqa
|
||||
from mopidy.config.types import * # noqa
|
||||
from mopidy.utils import path, versioning
|
||||
from mopidy.utils import compat, path, versioning
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@ -108,7 +108,7 @@ def _load(files, defaults, overrides):
|
||||
# all in the same way?
|
||||
logger.info('Loading config from builtin defaults')
|
||||
for default in defaults:
|
||||
if isinstance(default, unicode):
|
||||
if isinstance(default, compat.text_type):
|
||||
default = default.encode('utf-8')
|
||||
parser.readfp(io.BytesIO(default))
|
||||
|
||||
|
||||
@ -9,6 +9,8 @@ try:
|
||||
except ImportError:
|
||||
dbus = None
|
||||
|
||||
from mopidy.utils import compat
|
||||
|
||||
|
||||
# XXX: Hack to workaround introspection bug caused by gnome-keyring, should be
|
||||
# fixed by version 3.5 per:
|
||||
@ -92,7 +94,7 @@ def set(section, key, value):
|
||||
if not collection:
|
||||
return False
|
||||
|
||||
if isinstance(value, unicode):
|
||||
if isinstance(value, compat.text_type):
|
||||
value = value.encode('utf-8')
|
||||
|
||||
session = service.OpenSession('plain', EMPTY_STRING)[1]
|
||||
|
||||
@ -5,18 +5,18 @@ import re
|
||||
import socket
|
||||
|
||||
from mopidy.config import validators
|
||||
from mopidy.utils import path
|
||||
from mopidy.utils import compat, path
|
||||
|
||||
|
||||
def decode(value):
|
||||
if isinstance(value, unicode):
|
||||
if isinstance(value, compat.text_type):
|
||||
return value
|
||||
# TODO: only unescape \n \t and \\?
|
||||
return value.decode('string-escape').decode('utf-8')
|
||||
|
||||
|
||||
def encode(value):
|
||||
if not isinstance(value, unicode):
|
||||
if not isinstance(value, compat.text_type):
|
||||
return value
|
||||
for char in ('\\', '\n', '\t'): # TODO: more escapes?
|
||||
value = value.replace(char, char.encode('unicode-escape'))
|
||||
@ -278,7 +278,7 @@ class Path(ConfigValue):
|
||||
return ExpandedPath(value, expanded)
|
||||
|
||||
def serialize(self, value, display=False):
|
||||
if isinstance(value, unicode):
|
||||
if isinstance(value, compat.text_type):
|
||||
raise ValueError('paths should always be bytes')
|
||||
if isinstance(value, ExpandedPath):
|
||||
return value.original
|
||||
|
||||
@ -6,6 +6,7 @@ import random
|
||||
|
||||
from mopidy.core import listener
|
||||
from mopidy.models import TlTrack
|
||||
from mopidy.utils import compat
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@ -329,7 +330,7 @@ class TracklistController(object):
|
||||
matches = self._tl_tracks
|
||||
for (key, values) in criteria.iteritems():
|
||||
if (not isinstance(values, collections.Iterable)
|
||||
or isinstance(values, basestring)):
|
||||
or isinstance(values, compat.string_types)):
|
||||
# Fail hard if anyone is using the <0.17 calling style
|
||||
raise ValueError('Filter values must be iterable: %r' % values)
|
||||
if key == 'tlid':
|
||||
|
||||
@ -7,6 +7,7 @@ import urllib
|
||||
import urlparse
|
||||
|
||||
from mopidy.models import Track
|
||||
from mopidy.utils import compat
|
||||
from mopidy.utils.encoding import locale_decode
|
||||
from mopidy.utils.path import path_to_uri, uri_to_path
|
||||
|
||||
@ -29,14 +30,14 @@ def local_track_uri_to_path(uri, media_dir):
|
||||
|
||||
def path_to_local_track_uri(relpath):
|
||||
"""Convert path releative to media_dir to local track URI."""
|
||||
if isinstance(relpath, unicode):
|
||||
if isinstance(relpath, compat.text_type):
|
||||
relpath = relpath.encode('utf-8')
|
||||
return b'local:track:%s' % urllib.quote(relpath)
|
||||
|
||||
|
||||
def path_to_local_directory_uri(relpath):
|
||||
"""Convert path relative to :confval:`local/media_dir` directory URI."""
|
||||
if isinstance(relpath, unicode):
|
||||
if isinstance(relpath, compat.text_type):
|
||||
relpath = relpath.encode('utf-8')
|
||||
return b'local:directory:%s' % urllib.quote(relpath)
|
||||
|
||||
|
||||
@ -2,9 +2,11 @@ from __future__ import absolute_import, unicode_literals
|
||||
|
||||
import locale
|
||||
|
||||
from mopidy.utils import compat
|
||||
|
||||
|
||||
def locale_decode(bytestr):
|
||||
try:
|
||||
return unicode(bytestr)
|
||||
return compat.text_type(bytestr)
|
||||
except UnicodeError:
|
||||
return str(bytestr).decode(locale.getpreferredencoding())
|
||||
return bytes(bytestr).decode(locale.getpreferredencoding())
|
||||
|
||||
@ -6,6 +6,8 @@ import traceback
|
||||
|
||||
import pykka
|
||||
|
||||
from mopidy.utils import compat
|
||||
|
||||
|
||||
class JsonRpcWrapper(object):
|
||||
"""
|
||||
@ -137,13 +139,13 @@ class JsonRpcWrapper(object):
|
||||
except TypeError as error:
|
||||
raise JsonRpcInvalidParamsError(data={
|
||||
'type': error.__class__.__name__,
|
||||
'message': unicode(error),
|
||||
'message': compat.text_type(error),
|
||||
'traceback': traceback.format_exc(),
|
||||
})
|
||||
except Exception as error:
|
||||
raise JsonRpcApplicationError(data={
|
||||
'type': error.__class__.__name__,
|
||||
'message': unicode(error),
|
||||
'message': compat.text_type(error),
|
||||
'traceback': traceback.format_exc(),
|
||||
})
|
||||
except JsonRpcError as error:
|
||||
@ -164,7 +166,7 @@ class JsonRpcWrapper(object):
|
||||
if 'method' not in request:
|
||||
raise JsonRpcInvalidRequestError(
|
||||
data='"method" member must be included')
|
||||
if not isinstance(request['method'], unicode):
|
||||
if not isinstance(request['method'], compat.text_type):
|
||||
raise JsonRpcInvalidRequestError(
|
||||
data='"method" must be a string')
|
||||
|
||||
|
||||
@ -11,6 +11,8 @@ import urlparse
|
||||
|
||||
import glib
|
||||
|
||||
from mopidy.utils import compat
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@ -64,7 +66,7 @@ def path_to_uri(path):
|
||||
|
||||
Returns a file:// URI as an unicode string.
|
||||
"""
|
||||
if isinstance(path, unicode):
|
||||
if isinstance(path, compat.text_type):
|
||||
path = path.encode('utf-8')
|
||||
path = urllib.quote(path)
|
||||
return urlparse.urlunsplit((b'file', b'', path, b'', b''))
|
||||
@ -81,7 +83,7 @@ def uri_to_path(uri):
|
||||
look up the matching dir or file on your file system because the exact path
|
||||
would be lost by ignoring its encoding.
|
||||
"""
|
||||
if isinstance(uri, unicode):
|
||||
if isinstance(uri, compat.text_type):
|
||||
uri = uri.encode('utf-8')
|
||||
return urllib.unquote(urlparse.urlsplit(uri).path)
|
||||
|
||||
|
||||
@ -2,6 +2,8 @@ from __future__ import absolute_import, unicode_literals
|
||||
|
||||
import os
|
||||
|
||||
from mopidy.utils import compat
|
||||
|
||||
|
||||
def path_to_data_dir(name):
|
||||
if not isinstance(name, bytes):
|
||||
@ -31,4 +33,4 @@ class IsA(object):
|
||||
|
||||
any_int = IsA(int)
|
||||
any_str = IsA(str)
|
||||
any_unicode = IsA(unicode)
|
||||
any_unicode = IsA(compat.text_type)
|
||||
|
||||
@ -9,6 +9,7 @@ import unittest
|
||||
import mock
|
||||
|
||||
from mopidy.config import types
|
||||
from mopidy.utils import compat
|
||||
|
||||
# TODO: DecodeTest and EncodeTest
|
||||
|
||||
@ -48,7 +49,7 @@ class StringTest(unittest.TestCase):
|
||||
def test_deserialize_conversion_success(self):
|
||||
value = types.String()
|
||||
self.assertEqual('foo', value.deserialize(b' foo '))
|
||||
self.assertIsInstance(value.deserialize(b'foo'), unicode)
|
||||
self.assertIsInstance(value.deserialize(b'foo'), compat.text_type)
|
||||
|
||||
def test_deserialize_decodes_utf8(self):
|
||||
value = types.String()
|
||||
@ -119,7 +120,7 @@ class SecretTest(unittest.TestCase):
|
||||
def test_deserialize_decodes_utf8(self):
|
||||
value = types.Secret()
|
||||
result = value.deserialize('æøå'.encode('utf-8'))
|
||||
self.assertIsInstance(result, unicode)
|
||||
self.assertIsInstance(result, compat.text_type)
|
||||
self.assertEqual('æøå', result)
|
||||
|
||||
def test_deserialize_enforces_required(self):
|
||||
|
||||
@ -7,7 +7,7 @@ import unittest
|
||||
|
||||
from mock import Mock, sentinel
|
||||
|
||||
from mopidy.utils import network
|
||||
from mopidy.utils import compat, network
|
||||
|
||||
from tests import any_unicode
|
||||
|
||||
@ -259,13 +259,13 @@ class LineProtocolTest(unittest.TestCase):
|
||||
def test_decode_plain_ascii(self):
|
||||
result = network.LineProtocol.decode(self.mock, 'abc')
|
||||
self.assertEqual('abc', result)
|
||||
self.assertEqual(unicode, type(result))
|
||||
self.assertEqual(compat.text_type, type(result))
|
||||
|
||||
def test_decode_utf8(self):
|
||||
result = network.LineProtocol.decode(
|
||||
self.mock, 'æøå'.encode('utf-8'))
|
||||
self.assertEqual('æøå', result)
|
||||
self.assertEqual(unicode, type(result))
|
||||
self.assertEqual(compat.text_type, type(result))
|
||||
|
||||
def test_decode_invalid_data(self):
|
||||
string = Mock()
|
||||
|
||||
@ -9,7 +9,7 @@ import unittest
|
||||
|
||||
import glib
|
||||
|
||||
from mopidy.utils import path
|
||||
from mopidy.utils import compat, path
|
||||
|
||||
import tests
|
||||
|
||||
@ -57,7 +57,7 @@ class GetOrCreateDirTest(unittest.TestCase):
|
||||
|
||||
def test_create_dir_with_unicode(self):
|
||||
with self.assertRaises(ValueError):
|
||||
dir_path = unicode(os.path.join(self.parent, b'test'))
|
||||
dir_path = compat.text_type(os.path.join(self.parent, b'test'))
|
||||
path.get_or_create_dir(dir_path)
|
||||
|
||||
def test_create_dir_with_none(self):
|
||||
@ -108,7 +108,7 @@ class GetOrCreateFileTest(unittest.TestCase):
|
||||
|
||||
def test_create_dir_with_unicode(self):
|
||||
with self.assertRaises(ValueError):
|
||||
file_path = unicode(os.path.join(self.parent, b'test'))
|
||||
file_path = compat.text_type(os.path.join(self.parent, b'test'))
|
||||
path.get_or_create_file(file_path)
|
||||
|
||||
def test_create_file_with_none(self):
|
||||
|
||||
Loading…
Reference in New Issue
Block a user