changeset 1108:b2a34a6ec5e5

data: Let the `asset` endpoint load JSON data into the template engine.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 18 Feb 2018 20:32:30 -0800
parents a1c6050c9801
children 6f26e83dfced
files piecrust/data/assetor.py
diffstat 1 files changed, 11 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- 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)