Skip to content

Commit

Permalink
fix _utf8, introduce _unicode (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
pevans96 authored May 28, 2024
1 parent 17979d5 commit 8023d9d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
2 changes: 1 addition & 1 deletion assetman/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
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
21 changes: 17 additions & 4 deletions assetman/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
]
Expand Down

0 comments on commit 8023d9d

Please sign in to comment.