# HG changeset patch # User Ludovic Chabant # Date 1519014750 28800 # Node ID b2a34a6ec5e534b4b5ec21caf1dc82df6b3986e6 # Parent a1c6050c980150b0215c8223c9eb138316c0e067 data: Let the `asset` endpoint load JSON data into the template engine. diff -r a1c6050c9801 -r b2a34a6ec5e5 piecrust/data/assetor.py --- a/piecrust/data/assetor.py Sun Feb 18 20:31:16 2018 -0800 +++ b/piecrust/data/assetor.py Sun Feb 18 20:32:30 2018 -0800 @@ -1,5 +1,6 @@ import os import os.path +import json import logging from piecrust.sources.base import REL_ASSETS from piecrust.uriutil import multi_replace @@ -17,6 +18,13 @@ self.content_item = content_item self.uri = uri + def __str__(self): + return self.uri + + def json(self): + with open(self.content_item.spec, 'r', encoding='utf8') as fp: + return json.load(fp) + class Assetor: debug_render_doc = """Helps render URLs to files in the current page's @@ -32,13 +40,13 @@ def __getattr__(self, name): try: self._cacheAssets() - return self._cache_map[name].uri + return self._cache_map[name] except KeyError: raise AttributeError() def __getitem__(self, name): self._cacheAssets() - return self._cache_map[name].uri + return self._cache_map[name] def __contains__(self, name): self._cacheAssets() @@ -106,7 +114,7 @@ # TODO: this assumes a file-system source! uri_build_tokens['%path%'] = \ os.path.relpath(a.spec, root_dir).replace('\\', '/') - uri_build_tokens['%filename%'] = a.metadata['filename'] + uri_build_tokens['%filename%'] = a.metadata.get('filename') uri = multi_replace(asset_url_format, uri_build_tokens) self._cache_map[name] = _AssetInfo(a, uri)