comparison piecrust/data/assetor.py @ 25:65ae19c4e8a3

Copy page assets to bake output, use correct slashes when serving assets.
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 19 Aug 2014 11:07:42 -0700
parents a8f9c78a6608
children 43091c9837bf
comparison
equal deleted inserted replaced
24:644869022b6e 25:65ae19c4e8a3
12 12
13 13
14 def build_base_url(app, uri, assets_path): 14 def build_base_url(app, uri, assets_path):
15 base_url_format = app.env.base_asset_url_format 15 base_url_format = app.env.base_asset_url_format
16 site_root = app.config.get('site/root') 16 site_root = app.config.get('site/root')
17 rel_path = os.path.relpath(assets_path, app.root_dir) 17 rel_path = os.path.relpath(assets_path, app.root_dir).replace('\\', '/')
18 pretty = app.config.get('site/pretty_urls') 18 pretty = app.config.get('site/pretty_urls')
19 if not pretty: 19 if not pretty:
20 uri, _ = os.path.splitext(uri) 20 uri, _ = os.path.splitext(uri)
21 uri = uri.lstrip('/') 21 uri = uri.lstrip('/')
22 base_url = multi_replace( 22 base_url = multi_replace(
42 self._cache = None 42 self._cache = None
43 43
44 def __getattr__(self, name): 44 def __getattr__(self, name):
45 try: 45 try:
46 self._cacheAssets() 46 self._cacheAssets()
47 return self._cache[name] 47 return self._cache[name][0]
48 except KeyError: 48 except KeyError:
49 raise AttributeError() 49 raise AttributeError()
50 50
51 def __getitem__(self, key): 51 def __getitem__(self, key):
52 self._cacheAssets() 52 self._cacheAssets()
53 return self._cache[key] 53 return self._cache[key][0]
54 54
55 def __iter__(self): 55 def __iter__(self):
56 self._cacheAssets() 56 self._cacheAssets()
57 return iter(self._cache.values()) 57 return map(lambda i: i[0], self._cache.values())
58
59 def _getAssetPaths(self):
60 self._cacheAssets()
61 return map(lambda i: i[1], self._cache.values())
58 62
59 def _debugRenderAssetNames(self): 63 def _debugRenderAssetNames(self):
60 self._cacheAssets() 64 self._cacheAssets()
61 return list(self._cache.keys()) 65 return list(self._cache.keys())
62 66
69 assets_dir = name + Assetor.ASSET_DIR_SUFFIX 73 assets_dir = name + Assetor.ASSET_DIR_SUFFIX
70 if not os.path.isdir(assets_dir): 74 if not os.path.isdir(assets_dir):
71 return 75 return
72 76
73 base_url = build_base_url(self._page.app, self._uri, assets_dir) 77 base_url = build_base_url(self._page.app, self._uri, assets_dir)
74 for _, __, filenames in os.walk(assets_dir): 78 for fn in os.listdir(assets_dir):
75 for fn in filenames: 79 full_fn = os.path.join(assets_dir, fn)
76 name, ext = os.path.splitext(fn) 80 if not os.path.isfile(full_fn):
77 if name in self._cache: 81 raise Exception("Skipping: %s" % full_fn)
78 raise UnsupportedAssetsError( 82 continue
79 "Multiple asset files are named '%s'." % name)
80 self._cache[name] = base_url + fn
81 83
84 name, ext = os.path.splitext(fn)
85 if name in self._cache:
86 raise UnsupportedAssetsError(
87 "Multiple asset files are named '%s'." % name)
88 self._cache[name] = (base_url + fn, full_fn)
89
90 cpi = self._page.app.env.exec_info_stack.current_page_info
91 if cpi is not None:
92 cpi.render_ctx.used_assets = self
93