Mercurial > piecrust2
comparison piecrust/data/assetor.py @ 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 | 8adc27285d93 |
children | 29c51b981c17 |
comparison
equal
deleted
inserted
replaced
1107:a1c6050c9801 | 1108:b2a34a6ec5e5 |
---|---|
1 import os | 1 import os |
2 import os.path | 2 import os.path |
3 import json | |
3 import logging | 4 import logging |
4 from piecrust.sources.base import REL_ASSETS | 5 from piecrust.sources.base import REL_ASSETS |
5 from piecrust.uriutil import multi_replace | 6 from piecrust.uriutil import multi_replace |
6 | 7 |
7 | 8 |
15 class _AssetInfo: | 16 class _AssetInfo: |
16 def __init__(self, content_item, uri): | 17 def __init__(self, content_item, uri): |
17 self.content_item = content_item | 18 self.content_item = content_item |
18 self.uri = uri | 19 self.uri = uri |
19 | 20 |
21 def __str__(self): | |
22 return self.uri | |
23 | |
24 def json(self): | |
25 with open(self.content_item.spec, 'r', encoding='utf8') as fp: | |
26 return json.load(fp) | |
27 | |
20 | 28 |
21 class Assetor: | 29 class Assetor: |
22 debug_render_doc = """Helps render URLs to files in the current page's | 30 debug_render_doc = """Helps render URLs to files in the current page's |
23 asset folder.""" | 31 asset folder.""" |
24 debug_render = [] | 32 debug_render = [] |
30 self._cache_list = None | 38 self._cache_list = None |
31 | 39 |
32 def __getattr__(self, name): | 40 def __getattr__(self, name): |
33 try: | 41 try: |
34 self._cacheAssets() | 42 self._cacheAssets() |
35 return self._cache_map[name].uri | 43 return self._cache_map[name] |
36 except KeyError: | 44 except KeyError: |
37 raise AttributeError() | 45 raise AttributeError() |
38 | 46 |
39 def __getitem__(self, name): | 47 def __getitem__(self, name): |
40 self._cacheAssets() | 48 self._cacheAssets() |
41 return self._cache_map[name].uri | 49 return self._cache_map[name] |
42 | 50 |
43 def __contains__(self, name): | 51 def __contains__(self, name): |
44 self._cacheAssets() | 52 self._cacheAssets() |
45 return name in self._cache_map | 53 return name in self._cache_map |
46 | 54 |
104 (name, content_item.spec)) | 112 (name, content_item.spec)) |
105 | 113 |
106 # TODO: this assumes a file-system source! | 114 # TODO: this assumes a file-system source! |
107 uri_build_tokens['%path%'] = \ | 115 uri_build_tokens['%path%'] = \ |
108 os.path.relpath(a.spec, root_dir).replace('\\', '/') | 116 os.path.relpath(a.spec, root_dir).replace('\\', '/') |
109 uri_build_tokens['%filename%'] = a.metadata['filename'] | 117 uri_build_tokens['%filename%'] = a.metadata.get('filename') |
110 uri = multi_replace(asset_url_format, uri_build_tokens) | 118 uri = multi_replace(asset_url_format, uri_build_tokens) |
111 | 119 |
112 self._cache_map[name] = _AssetInfo(a, uri) | 120 self._cache_map[name] = _AssetInfo(a, uri) |
113 self._cache_list.append(uri) | 121 self._cache_list.append(uri) |
114 | 122 |