From d1d9cc2aef84cbb33d113509532ff8574855f871 Mon Sep 17 00:00:00 2001 From: Patrick Evans Date: Thu, 23 May 2024 12:07:31 -0600 Subject: [PATCH] fix _utf8, introduce _unicode --- assetman/compile.py | 2 +- assetman/managers.py | 6 +++--- assetman/tools.py | 21 +++++++++++++++++---- pyproject.toml | 2 +- 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/assetman/compile.py b/assetman/compile.py index e763dcd..33b19d0 100755 --- a/assetman/compile.py +++ b/assetman/compile.py @@ -302,7 +302,7 @@ def _build_manifest_helper(static_dir, src_paths, static_url_prefix, manifest): for src_path in src_paths: # Make sure every source path at least has the skeleton entry rel_src_path = make_relative_static_path(static_dir, src_path) - logging.info('_build_manifest_helper %s (crrent %s)', src_path, manifest.assets.get(rel_src_path)) + logging.info('_build_manifest_helper %s (current %s)', src_path, manifest.assets.get(rel_src_path)) manifest.assets.setdefault(rel_src_path, empty_asset_entry()) for dep_path in iter_deps(static_dir, src_path, static_url_prefix): logging.info('%s > dependency %s', src_path, dep_path) 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 88731b9..efed01f 100644 --- a/assetman/tools.py +++ b/assetman/tools.py @@ -19,14 +19,27 @@ def _crc(key): return binascii.crc32(_utf8(key)) & 0xffffffff def _utf8(s): - """encode a unicode string as utf-8""" + """encode a string as utf-8, returning bytes""" if isinstance(s, str): - return s.encode("utf-8").decode() + return s.encode("utf-8") if isinstance(s, bytes): - return s.decode("utf-8") - assert isinstance(s, str), "_utf8 expected a str, not %r" % type(s) + try: + s.decode("utf-8") + except UnicodeDecodeError: + raise AssertionError("Invalid encoding. _utf8 expected a str or utf-8 encoded bytes") + 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. diff --git a/pyproject.toml b/pyproject.toml index 4d11842..dcbb5ab 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,7 +5,7 @@ build-backend = "setuptools.build_meta" [project] name = "pyassetman" description = "assetman assetmanager" -version = "0.3.0rc2" +version = "0.3.0rc3" authors = [ { name="Will McCutchen", email="wm@bit.ly" }, ]