comparison piecrust/data/assetor.py @ 871:504ddb370df8

refactor: Fixing some issues with baking assets.
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 13 Jun 2017 22:30:27 -0700
parents 48d25fd68b8d
children dcdec4b951a1
comparison
equal deleted inserted replaced
870:48d25fd68b8d 871:504ddb370df8
1 import os 1 import os
2 import os.path 2 import os.path
3 import shutil
4 import logging 3 import logging
5 import collections.abc 4 import collections.abc
6 from piecrust import ASSET_DIR_SUFFIX
7 from piecrust.sources.base import REL_ASSETS 5 from piecrust.sources.base import REL_ASSETS
8 from piecrust.uriutil import multi_replace 6 from piecrust.uriutil import multi_replace
9 7
10 8
11 logger = logging.getLogger(__name__) 9 logger = logging.getLogger(__name__)
45 43
46 def __len__(self): 44 def __len__(self):
47 self._cacheAssets() 45 self._cacheAssets()
48 return len(self._cache_list) 46 return len(self._cache_list)
49 47
48 def _getAssetItems(self):
49 self._cacheAssets()
50 return map(lambda i: i.content_item, self._cache_map.values())
51
50 def _debugRenderAssetNames(self): 52 def _debugRenderAssetNames(self):
51 self._cacheAssets() 53 self._cacheAssets()
52 return list(self._cache_map.keys()) 54 return list(self._cache_map.keys())
53 55
54 def _cacheAssets(self): 56 def _cacheAssets(self):
87 "An asset with name '%s' already exists for item '%s'. " 89 "An asset with name '%s' already exists for item '%s'. "
88 "Do you have multiple assets with colliding names?" % 90 "Do you have multiple assets with colliding names?" %
89 (name, content_item.spec)) 91 (name, content_item.spec))
90 92
91 # TODO: this assumes a file-system source! 93 # TODO: this assumes a file-system source!
92 uri_build_tokens['%path%'] = ( 94 uri_build_tokens['%path%'] = \
93 os.path.relpath(a.spec, root_dir).replace('\\', '/')) 95 os.path.relpath(a.spec, root_dir).replace('\\', '/')
94 uri_build_tokens['%filename%'] = a.metadata['filename'], 96 uri_build_tokens['%filename%'] = a.metadata['filename']
95 uri = multi_replace(asset_url_format, uri_build_tokens) 97 uri = multi_replace(asset_url_format, uri_build_tokens)
96 98
97 self._cache_map[name] = _AssetInfo(a, uri) 99 self._cache_map[name] = _AssetInfo(a, uri)
98 self._cache_list.append(uri) 100 self._cache_list.append(uri)
99 101
100 stack = app.env.render_ctx_stack 102 stack = app.env.render_ctx_stack
101 cur_ctx = stack.current_ctx 103 cur_ctx = stack.current_ctx
102 if cur_ctx is not None: 104 if cur_ctx is not None:
103 cur_ctx.current_pass_info.used_assets = True 105 cur_ctx.current_pass_info.used_assets = True
104 106
105 def copyAssets(self, dest_dir):
106 page_pathname, _ = os.path.splitext(self._page.path)
107 in_assets_dir = page_pathname + ASSET_DIR_SUFFIX
108 for fn in os.listdir(in_assets_dir):
109 full_fn = os.path.join(in_assets_dir, fn)
110 if os.path.isfile(full_fn):
111 dest_ap = os.path.join(dest_dir, fn)
112 logger.debug(" %s -> %s" % (full_fn, dest_ap))
113 shutil.copy(full_fn, dest_ap)
114