diff --git a/beets/autotag/hooks.py b/beets/autotag/hooks.py index f9388ad83..e0f543870 100644 --- a/beets/autotag/hooks.py +++ b/beets/autotag/hooks.py @@ -292,6 +292,7 @@ class LazyClassProperty(object): @total_ordering +@six.python_2_unicode_compatible class Distance(object): """Keeps track of multiple distance penalties. Provides a single weighted distance for all penalties as well as a weighted distance @@ -378,7 +379,9 @@ class Distance(object): def __rsub__(self, other): return other - self.distance - def __unicode__(self): + # Behave like a string + + def __str__(self): return "{0:.2f}".format(self.distance) # Behave like a dict. diff --git a/beets/library.py b/beets/library.py index 7821f93f4..2c5ff47b5 100644 --- a/beets/library.py +++ b/beets/library.py @@ -264,7 +264,7 @@ PF_KEY_DEFAULT = 'default' # Exceptions. - +@six.python_2_unicode_compatible class FileOperationError(Exception): """Indicates an error when interacting with a file on disk. Possibilities include an unsupported media type, a permissions @@ -278,7 +278,7 @@ class FileOperationError(Exception): self.path = path self.reason = reason - def __unicode__(self): + def text(self): """Get a string representing the error. Describes both the underlying reason and the file path in question. """ @@ -287,26 +287,30 @@ class FileOperationError(Exception): six.text_type(self.reason) ) - def __str__(self): - return six.text_type(self).encode('utf8') + # define __str__ as text to avoid infinite loop on super() calls + # with @six.python_2_unicode_compatible + __str__ = text +@six.python_2_unicode_compatible class ReadError(FileOperationError): """An error while reading a file (i.e. in `Item.read`). """ - def __unicode__(self): - return u'error reading ' + super(ReadError, self).__unicode__() + def __str__(self): + return u'error reading ' + super(ReadError, self).text() +@six.python_2_unicode_compatible class WriteError(FileOperationError): """An error while writing a file (i.e. in `Item.write`). """ - def __unicode__(self): - return u'error writing ' + super(WriteError, self).__unicode__() + def __str__(self): + return u'error writing ' + super(WriteError, self).text() # Item and Album model classes. +@six.python_2_unicode_compatible class LibModel(dbcore.Model): """Shared concrete functionality for Items and Albums. """ @@ -343,9 +347,6 @@ class LibModel(dbcore.Model): return result def __str__(self): - return format(self).encode('utf8') - - def __unicode__(self): return format(self) diff --git a/beetsplug/beatport.py b/beetsplug/beatport.py index 530bf5184..5949589c3 100644 --- a/beetsplug/beatport.py +++ b/beetsplug/beatport.py @@ -197,8 +197,9 @@ class BeatportClient(object): return response.json()['results'] +@six.python_2_unicode_compatible class BeatportRelease(BeatportObject): - def __unicode__(self): + def __str__(self): if len(self.artists) < 4: artist_str = ", ".join(x[1] for x in self.artists) else: @@ -225,8 +226,9 @@ class BeatportRelease(BeatportObject): data['slug'], data['id']) +@six.python_2_unicode_compatible class BeatportTrack(BeatportObject): - def __unicode__(self): + def __str__(self): artist_str = ", ".join(x[1] for x in self.artists) return (u"" .format(artist_str, self.name, self.mix_name))