Fix bytes/str with unicode-nazi

Further cleaning will require setting unicode_literals on plugins
This commit is contained in:
Bruno Cauet 2015-01-20 15:06:37 +01:00
parent afb1611cf2
commit 438044c6ae
8 changed files with 23 additions and 20 deletions

View file

@ -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

View file

@ -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.

View file

@ -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, {})

View file

@ -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)

View file

@ -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

View file

@ -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)

View file

@ -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):

View file

@ -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