Mercurial > piecrust2
comparison piecrust/data/assetor.py @ 833:cb39c0dbe5f0
Merge pull request #40 from GitHub.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sun, 05 Feb 2017 22:42:18 -0800 |
parents | 61878590bf96 |
children | ad8f48a31c62 |
comparison
equal
deleted
inserted
replaced
825:c30574f0dadc | 833:cb39c0dbe5f0 |
---|---|
1 import os | 1 import os |
2 import os.path | 2 import os.path |
3 import shutil | |
3 import logging | 4 import logging |
4 from piecrust import ASSET_DIR_SUFFIX | 5 from piecrust import ASSET_DIR_SUFFIX |
5 from piecrust.uriutil import multi_replace | 6 from piecrust.uriutil import multi_replace |
6 | 7 |
7 | 8 |
29 '%uri%': uri}) | 30 '%uri%': uri}) |
30 | 31 |
31 return base_url.rstrip('/') + '/' | 32 return base_url.rstrip('/') + '/' |
32 | 33 |
33 | 34 |
34 class Assetor(object): | 35 class AssetorBase(object): |
35 debug_render_doc = """Helps render URLs to files in the current page's | |
36 asset folder.""" | |
37 debug_render = [] | |
38 debug_render_dynamic = ['_debugRenderAssetNames'] | |
39 | |
40 def __init__(self, page, uri): | 36 def __init__(self, page, uri): |
41 self._page = page | 37 self._page = page |
42 self._uri = uri | 38 self._uri = uri |
43 self._cache = None | 39 self._cache = None |
44 | 40 |
66 return list(self._cache.keys()) | 62 return list(self._cache.keys()) |
67 | 63 |
68 def _cacheAssets(self): | 64 def _cacheAssets(self): |
69 if self._cache is not None: | 65 if self._cache is not None: |
70 return | 66 return |
67 | |
68 self._cache = dict(self.findAssets()) | |
69 | |
70 def findAssets(self): | |
71 raise NotImplementedError() | |
71 | 72 |
72 self._cache = {} | 73 def copyAssets(self, dest_dir): |
74 raise NotImplementedError() | |
75 | |
76 class Assetor(AssetorBase): | |
77 debug_render_doc = """Helps render URLs to files in the current page's | |
78 asset folder.""" | |
79 debug_render = [] | |
80 debug_render_dynamic = ['_debugRenderAssetNames'] | |
81 | |
82 def findAssets(self): | |
83 assets = {} | |
73 name, ext = os.path.splitext(self._page.path) | 84 name, ext = os.path.splitext(self._page.path) |
74 assets_dir = name + ASSET_DIR_SUFFIX | 85 assets_dir = name + ASSET_DIR_SUFFIX |
75 if not os.path.isdir(assets_dir): | 86 if not os.path.isdir(assets_dir): |
76 return | 87 return |
77 | 88 |
82 if not os.path.isfile(full_fn): | 93 if not os.path.isfile(full_fn): |
83 raise Exception("Skipping: %s" % full_fn) | 94 raise Exception("Skipping: %s" % full_fn) |
84 continue | 95 continue |
85 | 96 |
86 name, ext = os.path.splitext(fn) | 97 name, ext = os.path.splitext(fn) |
87 if name in self._cache: | 98 if name in assets: |
88 raise UnsupportedAssetsError( | 99 raise UnsupportedAssetsError( |
89 "Multiple asset files are named '%s'." % name) | 100 "Multiple asset files are named '%s'." % name) |
90 self._cache[name] = (base_url + fn, full_fn) | 101 assets[name] = (base_url + fn, full_fn) |
91 | 102 |
92 cpi = self._page.app.env.exec_info_stack.current_page_info | 103 cpi = self._page.app.env.exec_info_stack.current_page_info |
93 if cpi is not None: | 104 if cpi is not None: |
94 cpi.render_ctx.current_pass_info.used_assets = True | 105 cpi.render_ctx.current_pass_info.used_assets = True |
95 | 106 |
107 return assets | |
108 | |
109 def copyAssets(self, dest_dir): | |
110 page_pathname, _ = os.path.splitext(self._page.path) | |
111 in_assets_dir = page_pathname + ASSET_DIR_SUFFIX | |
112 for fn in os.listdir(in_assets_dir): | |
113 full_fn = os.path.join(in_assets_dir, fn) | |
114 if os.path.isfile(full_fn): | |
115 dest_ap = os.path.join(dest_dir, fn) | |
116 logger.debug(" %s -> %s" % (full_fn, dest_ap)) | |
117 shutil.copy(full_fn, dest_ap) |