diff --git a/assetman/managers.py b/assetman/managers.py index c9f8bc1..909fc8d 100644 --- a/assetman/managers.py +++ b/assetman/managers.py @@ -5,7 +5,7 @@ import functools import hashlib -from assetman.tools import get_shard_from_list, _utf8 +from assetman.tools import get_shard_from_list, _unicode from assetman.manifest import Manifest class AssetManager(object): @@ -46,7 +46,7 @@ def __init__(self, rel_url_text, local=False, include_tag=True, src_path=None, s Any extra kwargs will be interpreted as extra HTML params to include on the rendered element. """ - self.rel_urls = [_f for _f in _utf8(rel_url_text).split() if _f] + self.rel_urls = [_f for _f in _unicode(rel_url_text).split() if _f] self.local = local self.include_tag = include_tag self.src_path = src_path @@ -100,7 +100,7 @@ def render_attrs(self): """Returns this asset block's attrs as an HTML string. Includes a leading space. """ - attrs = ' '.join('%s=%r' % (attr, _utf8(val)) + attrs = ' '.join('%s=%r' % (attr, _unicode(val)) for attr, val in self.attrs.items()) return ' ' + attrs if attrs else '' diff --git a/assetman/tools.py b/assetman/tools.py index 08c54be..efed01f 100644 --- a/assetman/tools.py +++ b/assetman/tools.py @@ -30,6 +30,16 @@ def _utf8(s): assert isinstance(s, bytes), "_utf8 expected a str or utf-8 encoded bytes, not %r" % type(s) return s +def _unicode(value): + """decode utf-8 bytes, returning string""" + if isinstance(value, bytes): + try: + return value.decode("utf-8") + except UnicodeDecodeError: + raise AssertionError("Invalid encoding. _unicode expected a str or utf-8 encoded bytes") + assert isinstance(value, str), "_unicode expected a str or utf-8 encoded bytes, not %r" % type(value) + return value + def iter_template_paths(template_dirs, template_ext): """Walks each directory in the given list of template directories, yielding the path to each template found.