Mercurial > piecrust2
comparison piecrust/data/assetor.py @ 3:f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
- Serving works, with debug window.
- Baking works, multi-threading, with dependency handling.
- Various things not implemented yet.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sun, 10 Aug 2014 23:43:16 -0700 |
parents | |
children | 474c9882decf |
comparison
equal
deleted
inserted
replaced
2:40fa08b261b9 | 3:f485ba500df3 |
---|---|
1 import os.path | |
2 import logging | |
3 from piecrust.uriutil import multi_replace | |
4 | |
5 | |
6 logger = logging.getLogger(__name__) | |
7 | |
8 | |
9 def build_base_url(app, uri, assets_path): | |
10 base_url_format = app.env.base_asset_url_format | |
11 site_root = app.config.get('site/root') | |
12 rel_path = os.path.relpath(assets_path, app.root_dir) | |
13 pretty = app.config.get('site/pretty_urls') | |
14 if not pretty: | |
15 uri, _ = os.path.splitext(uri) | |
16 base_url = multi_replace( | |
17 base_url_format, | |
18 { | |
19 '%site_root%': site_root, | |
20 '%path%': rel_path, | |
21 '%uri%': uri}) | |
22 return base_url.rstrip('/') + '/' | |
23 | |
24 | |
25 class Assetor(object): | |
26 ASSET_DIR_SUFFIX = '-assets' | |
27 | |
28 debug_render_doc = """Helps render URLs to files in the current page's | |
29 asset folder.""" | |
30 debug_render = [] | |
31 debug_render_dynamic = ['_debugRenderAssetNames'] | |
32 | |
33 def __init__(self, page, uri): | |
34 self._page = page | |
35 self._uri = uri | |
36 self._cache = None | |
37 | |
38 def __getattr__(self, name): | |
39 try: | |
40 self._cacheAssets() | |
41 return self._cache[name] | |
42 except KeyError: | |
43 raise AttributeError() | |
44 | |
45 def __getitem__(self, key): | |
46 self._cacheAssets() | |
47 return self._cache[key] | |
48 | |
49 def __iter__(self): | |
50 self._cacheAssets() | |
51 return self._cache.__iter__() | |
52 | |
53 def iterkeys(self): | |
54 return self.__iter__(self) | |
55 | |
56 def _debugRenderAssetNames(self): | |
57 self._cacheAssets() | |
58 return self._cache.keys() | |
59 | |
60 def _cacheAssets(self): | |
61 if self._cache is not None: | |
62 return | |
63 | |
64 self._cache = {} | |
65 name, ext = os.path.splitext(self._page.path) | |
66 assets_dir = name + Assetor.ASSET_DIR_SUFFIX | |
67 if not os.path.isdir(assets_dir): | |
68 return | |
69 | |
70 base_url = build_base_url(self._page.app, self._uri, assets_dir) | |
71 for _, __, filenames in os.walk(assets_dir): | |
72 for fn in filenames: | |
73 name, ext = os.path.splitext(fn) | |
74 self._cache[name] = base_url + fn | |
75 |