Skip to content

Commit

Permalink
add _unicode
Browse files Browse the repository at this point in the history
  • Loading branch information
pevans96 committed May 23, 2024
1 parent d0ca1a4 commit a3ff0a0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
6 changes: 3 additions & 3 deletions assetman/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 ''

Expand Down
10 changes: 10 additions & 0 deletions assetman/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit a3ff0a0

Please sign in to comment.