Invent a new utility to deal with native strings

This commit is contained in:
Adrian Sampson 2016-06-29 22:25:03 -07:00
parent ecfda77f5a
commit 386f582364
2 changed files with 15 additions and 2 deletions

View file

@ -1617,7 +1617,7 @@ def config_func(lib, opts, args):
# Dump configuration.
else:
config_out = config.dump(full=opts.defaults, redact=opts.redact)
print_(util.as_string(config_out))
print_(util.text_string(config_out))
def config_edit():
@ -1687,7 +1687,7 @@ def completion_script(commands):
"""
base_script = os.path.join(_package_path('beets.ui'), 'completion_base.sh')
with open(base_script, 'r') as base_script:
yield util.as_string(base_script.read())
yield util.text_string(base_script.read())
options = {}
aliases = {}

View file

@ -647,6 +647,19 @@ def as_string(value):
return six.text_type(value)
def text_string(value, encoding='utf8'):
"""Convert a string, which can either be bytes or unicode, to
unicode.
Text (unicode) is left untouched; bytes are decoded. This is useful
to convert from a "native string" (bytes on Python 2, str on Python
3) to a consistently unicode value.
"""
if isinstance(value, bytes):
return value.decode(encoding)
return value
def plurality(objs):
"""Given a sequence of hashble objects, returns the object that
is most common in the set and the its number of appearance. The