From 438044c6aefbf3b919b90267c3feae0735951aa9 Mon Sep 17 00:00:00 2001 From: Bruno Cauet Date: Tue, 20 Jan 2015 15:06:37 +0100 Subject: [PATCH] Fix bytes/str with unicode-nazi Further cleaning will require setting unicode_literals on plugins --- beets/library.py | 4 ++-- beets/mediafile.py | 5 +++-- beets/plugins.py | 11 ++++++----- beets/ui/__init__.py | 4 ++-- beets/util/__init__.py | 4 ++-- beets/util/confit.py | 10 +++++----- beetsplug/discogs.py | 2 +- beetsplug/lyrics.py | 3 ++- 8 files changed, 23 insertions(+), 20 deletions(-) diff --git a/beets/library.py b/beets/library.py index 488be5cfb..3cf4aec9f 100644 --- a/beets/library.py +++ b/beets/library.py @@ -373,7 +373,7 @@ class Item(LibModel): _search_fields = ('artist', 'title', 'comments', 'album', 'albumartist', 'genre') - _media_fields = set(MediaFile.readable_fields()) \ + _media_fields = set(f.decode('utf8') for f in MediaFile.readable_fields()) \ .intersection(_fields.keys()) """Set of item fields that are backed by `MediaFile` fields. @@ -1155,7 +1155,7 @@ class DefaultTemplateFunctions(object): additional context to the functions -- specifically, the Item being evaluated. """ - _prefix = 'tmpl_' + _prefix = b'tmpl_' def __init__(self, item=None, lib=None): """Paramaterize the functions. If `item` or `lib` is None, then diff --git a/beets/mediafile.py b/beets/mediafile.py index 26a630ce6..1b3956aca 100644 --- a/beets/mediafile.py +++ b/beets/mediafile.py @@ -398,8 +398,9 @@ class StorageStyle(object): self.float_places = float_places # Convert suffix to correct string type. - if self.suffix and self.as_type is unicode: - self.suffix = self.as_type(self.suffix) + if self.suffix and self.as_type is unicode \ + and not isinstance(self.suffix, unicode): + self.suffix = self.suffix.decode('utf8') # Getter. diff --git a/beets/plugins.py b/beets/plugins.py index d67aadf54..712774fe4 100755 --- a/beets/plugins.py +++ b/beets/plugins.py @@ -28,7 +28,7 @@ import beets from beets import logging from beets import mediafile -PLUGIN_NAMESPACE = 'beetsplug' +PLUGIN_NAMESPACE = b'beetsplug' # Plugins using the Last.fm API can share the same API key. LASTFM_KEY = '2dc3914abf35f0d9c92d97d8f8e42b43' @@ -72,7 +72,7 @@ class BeetsPlugin(object): def __init__(self, name=None): """Perform one-time plugin setup. """ - self.name = name or self.__module__.split('.')[-1] + self.name = name or self.__module__.decode('utf8').split('.')[-1] self.config = beets.config[self.name] if not self.template_funcs: self.template_funcs = {} @@ -252,7 +252,8 @@ def load_plugins(names=()): BeetsPlugin subclasses desired. """ for name in names: - modname = '%s.%s' % (PLUGIN_NAMESPACE, name) + bname = name.encode('utf8') + modname = b'%s.%s' % (PLUGIN_NAMESPACE, bname) try: try: namespace = __import__(modname, None, None) @@ -263,7 +264,7 @@ def load_plugins(names=()): else: raise else: - for obj in getattr(namespace, name).__dict__.values(): + for obj in getattr(namespace, bname).__dict__.values(): if isinstance(obj, type) and issubclass(obj, BeetsPlugin) \ and obj != BeetsPlugin and obj not in _classes: _classes.add(obj) @@ -314,7 +315,7 @@ def queries(): def types(model_cls): # Gives us `item_types` and `album_types` - attr_name = '{0}_types'.format(model_cls.__name__.lower()) + attr_name = b'{0}_types'.format(model_cls.__name__.lower()) types = {} for plugin in find_plugins(): plugin_types = getattr(plugin, attr_name, {}) diff --git a/beets/ui/__init__.py b/beets/ui/__init__.py index eef331e62..118089dbb 100644 --- a/beets/ui/__init__.py +++ b/beets/ui/__init__.py @@ -43,7 +43,7 @@ from beets.autotag import mb from beets.dbcore import query as db_query # On Windows platforms, use colorama to support "ANSI" terminal colors. -if sys.platform == 'win32': +if sys.platform == b'win32': try: import colorama except ImportError: @@ -855,7 +855,7 @@ def _configure(options): # Add any additional config files specified with --config. This # special handling lets specified plugins get loaded before we # finish parsing the command line. - if getattr(options, 'config', None) is not None: + if getattr(options, b'config', None) is not None: config_path = options.config del options.config config.set_file(config_path) diff --git a/beets/util/__init__.py b/beets/util/__init__.py index 6a3215606..6af9bc6f7 100644 --- a/beets/util/__init__.py +++ b/beets/util/__init__.py @@ -301,13 +301,13 @@ def _fsencoding(): UTF-8 (not MBCS). """ encoding = sys.getfilesystemencoding() or sys.getdefaultencoding() - if encoding == 'mbcs': + if encoding == b'mbcs': # On Windows, a broken encoding known to Python as "MBCS" is # used for the filesystem. However, we only use the Unicode API # for Windows paths, so the encoding is actually immaterial so # we can avoid dealing with this nastiness. We arbitrarily # choose UTF-8. - encoding = 'utf8' + encoding = b'utf8' return encoding diff --git a/beets/util/confit.py b/beets/util/confit.py index f86091fa2..c30e52c5d 100644 --- a/beets/util/confit.py +++ b/beets/util/confit.py @@ -423,7 +423,7 @@ class Subview(ConfigView): if isinstance(self.key, int): self.name += '#{0}'.format(self.key) elif isinstance(self.key, BASESTRING): - self.name += '{0}'.format(self.key) + self.name += '{0}'.format(self.key.decode('utf8')) else: self.name += '{0}'.format(repr(self.key)) @@ -469,7 +469,7 @@ def _package_path(name): if loader is None or name == b'__main__': return None - if hasattr(loader, 'get_filename'): + if hasattr(loader, b'get_filename'): filepath = loader.get_filename(name) else: # Fall back to importing the specified module. @@ -489,13 +489,13 @@ def config_dirs(): """ paths = [] - if platform.system() == 'Darwin': + if platform.system() == b'Darwin': paths.append(MAC_DIR) paths.append(UNIX_DIR_FALLBACK) if UNIX_DIR_VAR in os.environ: paths.append(os.environ[UNIX_DIR_VAR]) - elif platform.system() == 'Windows': + elif platform.system() == b'Windows': paths.append(WINDOWS_DIR_FALLBACK) if WINDOWS_DIR_VAR in os.environ: paths.append(os.environ[WINDOWS_DIR_VAR]) @@ -578,7 +578,7 @@ def load_yaml(filename): parsed, a ConfigReadError is raised. """ try: - with open(filename, 'r') as f: + with open(filename, b'r') as f: return yaml.load(f, Loader=Loader) except (IOError, yaml.error.YAMLError) as exc: raise ConfigReadError(filename, exc) diff --git a/beetsplug/discogs.py b/beetsplug/discogs.py index 2c3e253c2..0e7b25862 100644 --- a/beetsplug/discogs.py +++ b/beetsplug/discogs.py @@ -35,7 +35,7 @@ import json urllib3_logger = logging.getLogger('requests.packages.urllib3') urllib3_logger.setLevel(logging.CRITICAL) -USER_AGENT = 'beets/{0} +http://beets.radbox.org/'.format(beets.__version__) +USER_AGENT = u'beets/{0} +http://beets.radbox.org/'.format(beets.__version__) class DiscogsPlugin(BeetsPlugin): diff --git a/beetsplug/lyrics.py b/beetsplug/lyrics.py index f65fddd0b..199bdf362 100644 --- a/beetsplug/lyrics.py +++ b/beetsplug/lyrics.py @@ -15,7 +15,8 @@ """Fetches, embeds, and displays lyrics. """ -from __future__ import division, absolute_import, print_function +from __future__ import (division, absolute_import, print_function, + unicode_literals) import re import requests