Mercurial > piecrust2
annotate piecrust/data/assetor.py @ 832:61878590bf96
Refactored Assetor into Assetor and AssetorBase
author | Ben Artin <ben@artins.org> |
---|---|
date | Wed, 11 Jan 2017 23:58:41 -0500 |
parents | 18978cf6d1ac |
children | ad8f48a31c62 |
rev | line source |
---|---|
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
1 import os |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
2 import os.path |
827
570f89414b2c
Assetor is now responsible for copying assets, to allow customization
Ben Artin <ben@artins.org>
parents:
809
diff
changeset
|
3 import shutil |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
4 import logging |
415
0e9a94b7fdfa
bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents:
329
diff
changeset
|
5 from piecrust import ASSET_DIR_SUFFIX |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
6 from piecrust.uriutil import multi_replace |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
7 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
8 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
9 logger = logging.getLogger(__name__) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
10 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
11 |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
12 class UnsupportedAssetsError(Exception): |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
13 pass |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
14 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
15 |
32
43091c9837bf
Fix problems with asset URLs.
Ludovic Chabant <ludovic@chabant.com>
parents:
25
diff
changeset
|
16 def build_base_url(app, uri, rel_assets_path): |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
17 base_url_format = app.env.base_asset_url_format |
32
43091c9837bf
Fix problems with asset URLs.
Ludovic Chabant <ludovic@chabant.com>
parents:
25
diff
changeset
|
18 rel_assets_path = rel_assets_path.replace('\\', '/') |
43091c9837bf
Fix problems with asset URLs.
Ludovic Chabant <ludovic@chabant.com>
parents:
25
diff
changeset
|
19 |
33
62c7a97c8340
Get the un-paginated URL of a page early and pass that around.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
20 # Remove any extension since we'll be copying assets into the 1st |
62c7a97c8340
Get the un-paginated URL of a page early and pass that around.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
21 # sub-page's folder. |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
22 pretty = app.config.get('site/pretty_urls') |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
23 if not pretty: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
24 uri, _ = os.path.splitext(uri) |
32
43091c9837bf
Fix problems with asset URLs.
Ludovic Chabant <ludovic@chabant.com>
parents:
25
diff
changeset
|
25 |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
26 base_url = multi_replace( |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
27 base_url_format, |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
28 { |
32
43091c9837bf
Fix problems with asset URLs.
Ludovic Chabant <ludovic@chabant.com>
parents:
25
diff
changeset
|
29 '%path%': rel_assets_path, |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
30 '%uri%': uri}) |
329
422052d2e978
internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents:
290
diff
changeset
|
31 |
422052d2e978
internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents:
290
diff
changeset
|
32 return base_url.rstrip('/') + '/' |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
33 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
34 |
832
61878590bf96
Refactored Assetor into Assetor and AssetorBase
Ben Artin <ben@artins.org>
parents:
831
diff
changeset
|
35 class AssetorBase(object): |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
36 def __init__(self, page, uri): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
37 self._page = page |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
38 self._uri = uri |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
39 self._cache = None |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
40 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
41 def __getattr__(self, name): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
42 try: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
43 self._cacheAssets() |
25
65ae19c4e8a3
Copy page assets to bake output, use correct slashes when serving assets.
Ludovic Chabant <ludovic@chabant.com>
parents:
13
diff
changeset
|
44 return self._cache[name][0] |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
45 except KeyError: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
46 raise AttributeError() |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
47 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
48 def __getitem__(self, key): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
49 self._cacheAssets() |
25
65ae19c4e8a3
Copy page assets to bake output, use correct slashes when serving assets.
Ludovic Chabant <ludovic@chabant.com>
parents:
13
diff
changeset
|
50 return self._cache[key][0] |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
51 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
52 def __iter__(self): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
53 self._cacheAssets() |
25
65ae19c4e8a3
Copy page assets to bake output, use correct slashes when serving assets.
Ludovic Chabant <ludovic@chabant.com>
parents:
13
diff
changeset
|
54 return map(lambda i: i[0], self._cache.values()) |
65ae19c4e8a3
Copy page assets to bake output, use correct slashes when serving assets.
Ludovic Chabant <ludovic@chabant.com>
parents:
13
diff
changeset
|
55 |
809
22c6f6a3d0a0
admin: Add ability to upload page assets.
Ludovic Chabant <ludovic@chabant.com>
parents:
472
diff
changeset
|
56 def allNames(self): |
22c6f6a3d0a0
admin: Add ability to upload page assets.
Ludovic Chabant <ludovic@chabant.com>
parents:
472
diff
changeset
|
57 self._cacheAssets() |
22c6f6a3d0a0
admin: Add ability to upload page assets.
Ludovic Chabant <ludovic@chabant.com>
parents:
472
diff
changeset
|
58 return list(self._cache.keys()) |
415
0e9a94b7fdfa
bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents:
329
diff
changeset
|
59 |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
60 def _debugRenderAssetNames(self): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
61 self._cacheAssets() |
5 | 62 return list(self._cache.keys()) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
63 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
64 def _cacheAssets(self): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
65 if self._cache is not None: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
66 return |
832
61878590bf96
Refactored Assetor into Assetor and AssetorBase
Ben Artin <ben@artins.org>
parents:
831
diff
changeset
|
67 |
61878590bf96
Refactored Assetor into Assetor and AssetorBase
Ben Artin <ben@artins.org>
parents:
831
diff
changeset
|
68 self._cache = dict(self.findAssets()) |
61878590bf96
Refactored Assetor into Assetor and AssetorBase
Ben Artin <ben@artins.org>
parents:
831
diff
changeset
|
69 |
61878590bf96
Refactored Assetor into Assetor and AssetorBase
Ben Artin <ben@artins.org>
parents:
831
diff
changeset
|
70 def findAssets(self): |
61878590bf96
Refactored Assetor into Assetor and AssetorBase
Ben Artin <ben@artins.org>
parents:
831
diff
changeset
|
71 raise NotImplementedError() |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
72 |
832
61878590bf96
Refactored Assetor into Assetor and AssetorBase
Ben Artin <ben@artins.org>
parents:
831
diff
changeset
|
73 def copyAssets(self, dest_dir): |
61878590bf96
Refactored Assetor into Assetor and AssetorBase
Ben Artin <ben@artins.org>
parents:
831
diff
changeset
|
74 raise NotImplementedError() |
61878590bf96
Refactored Assetor into Assetor and AssetorBase
Ben Artin <ben@artins.org>
parents:
831
diff
changeset
|
75 |
61878590bf96
Refactored Assetor into Assetor and AssetorBase
Ben Artin <ben@artins.org>
parents:
831
diff
changeset
|
76 class Assetor(AssetorBase): |
61878590bf96
Refactored Assetor into Assetor and AssetorBase
Ben Artin <ben@artins.org>
parents:
831
diff
changeset
|
77 debug_render_doc = """Helps render URLs to files in the current page's |
61878590bf96
Refactored Assetor into Assetor and AssetorBase
Ben Artin <ben@artins.org>
parents:
831
diff
changeset
|
78 asset folder.""" |
61878590bf96
Refactored Assetor into Assetor and AssetorBase
Ben Artin <ben@artins.org>
parents:
831
diff
changeset
|
79 debug_render = [] |
61878590bf96
Refactored Assetor into Assetor and AssetorBase
Ben Artin <ben@artins.org>
parents:
831
diff
changeset
|
80 debug_render_dynamic = ['_debugRenderAssetNames'] |
61878590bf96
Refactored Assetor into Assetor and AssetorBase
Ben Artin <ben@artins.org>
parents:
831
diff
changeset
|
81 |
61878590bf96
Refactored Assetor into Assetor and AssetorBase
Ben Artin <ben@artins.org>
parents:
831
diff
changeset
|
82 def findAssets(self): |
61878590bf96
Refactored Assetor into Assetor and AssetorBase
Ben Artin <ben@artins.org>
parents:
831
diff
changeset
|
83 assets = {} |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
84 name, ext = os.path.splitext(self._page.path) |
415
0e9a94b7fdfa
bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents:
329
diff
changeset
|
85 assets_dir = name + ASSET_DIR_SUFFIX |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
86 if not os.path.isdir(assets_dir): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
87 return |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
88 |
32
43091c9837bf
Fix problems with asset URLs.
Ludovic Chabant <ludovic@chabant.com>
parents:
25
diff
changeset
|
89 rel_assets_dir = os.path.relpath(assets_dir, self._page.app.root_dir) |
43091c9837bf
Fix problems with asset URLs.
Ludovic Chabant <ludovic@chabant.com>
parents:
25
diff
changeset
|
90 base_url = build_base_url(self._page.app, self._uri, rel_assets_dir) |
25
65ae19c4e8a3
Copy page assets to bake output, use correct slashes when serving assets.
Ludovic Chabant <ludovic@chabant.com>
parents:
13
diff
changeset
|
91 for fn in os.listdir(assets_dir): |
65ae19c4e8a3
Copy page assets to bake output, use correct slashes when serving assets.
Ludovic Chabant <ludovic@chabant.com>
parents:
13
diff
changeset
|
92 full_fn = os.path.join(assets_dir, fn) |
65ae19c4e8a3
Copy page assets to bake output, use correct slashes when serving assets.
Ludovic Chabant <ludovic@chabant.com>
parents:
13
diff
changeset
|
93 if not os.path.isfile(full_fn): |
65ae19c4e8a3
Copy page assets to bake output, use correct slashes when serving assets.
Ludovic Chabant <ludovic@chabant.com>
parents:
13
diff
changeset
|
94 raise Exception("Skipping: %s" % full_fn) |
65ae19c4e8a3
Copy page assets to bake output, use correct slashes when serving assets.
Ludovic Chabant <ludovic@chabant.com>
parents:
13
diff
changeset
|
95 continue |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
96 |
25
65ae19c4e8a3
Copy page assets to bake output, use correct slashes when serving assets.
Ludovic Chabant <ludovic@chabant.com>
parents:
13
diff
changeset
|
97 name, ext = os.path.splitext(fn) |
832
61878590bf96
Refactored Assetor into Assetor and AssetorBase
Ben Artin <ben@artins.org>
parents:
831
diff
changeset
|
98 if name in assets: |
25
65ae19c4e8a3
Copy page assets to bake output, use correct slashes when serving assets.
Ludovic Chabant <ludovic@chabant.com>
parents:
13
diff
changeset
|
99 raise UnsupportedAssetsError( |
65ae19c4e8a3
Copy page assets to bake output, use correct slashes when serving assets.
Ludovic Chabant <ludovic@chabant.com>
parents:
13
diff
changeset
|
100 "Multiple asset files are named '%s'." % name) |
832
61878590bf96
Refactored Assetor into Assetor and AssetorBase
Ben Artin <ben@artins.org>
parents:
831
diff
changeset
|
101 assets[name] = (base_url + fn, full_fn) |
25
65ae19c4e8a3
Copy page assets to bake output, use correct slashes when serving assets.
Ludovic Chabant <ludovic@chabant.com>
parents:
13
diff
changeset
|
102 |
65ae19c4e8a3
Copy page assets to bake output, use correct slashes when serving assets.
Ludovic Chabant <ludovic@chabant.com>
parents:
13
diff
changeset
|
103 cpi = self._page.app.env.exec_info_stack.current_page_info |
65ae19c4e8a3
Copy page assets to bake output, use correct slashes when serving assets.
Ludovic Chabant <ludovic@chabant.com>
parents:
13
diff
changeset
|
104 if cpi is not None: |
472
dc78ade3f320
bug: Fix copying of page assets during the bake.
Ludovic Chabant <ludovic@chabant.com>
parents:
415
diff
changeset
|
105 cpi.render_ctx.current_pass_info.used_assets = True |
832
61878590bf96
Refactored Assetor into Assetor and AssetorBase
Ben Artin <ben@artins.org>
parents:
831
diff
changeset
|
106 |
61878590bf96
Refactored Assetor into Assetor and AssetorBase
Ben Artin <ben@artins.org>
parents:
831
diff
changeset
|
107 return assets |
827
570f89414b2c
Assetor is now responsible for copying assets, to allow customization
Ben Artin <ben@artins.org>
parents:
809
diff
changeset
|
108 |
831
18978cf6d1ac
Removed pointless page argument from copyAssets
Ben Artin <ben@artins.org>
parents:
827
diff
changeset
|
109 def copyAssets(self, dest_dir): |
18978cf6d1ac
Removed pointless page argument from copyAssets
Ben Artin <ben@artins.org>
parents:
827
diff
changeset
|
110 page_pathname, _ = os.path.splitext(self._page.path) |
827
570f89414b2c
Assetor is now responsible for copying assets, to allow customization
Ben Artin <ben@artins.org>
parents:
809
diff
changeset
|
111 in_assets_dir = page_pathname + ASSET_DIR_SUFFIX |
570f89414b2c
Assetor is now responsible for copying assets, to allow customization
Ben Artin <ben@artins.org>
parents:
809
diff
changeset
|
112 for fn in os.listdir(in_assets_dir): |
570f89414b2c
Assetor is now responsible for copying assets, to allow customization
Ben Artin <ben@artins.org>
parents:
809
diff
changeset
|
113 full_fn = os.path.join(in_assets_dir, fn) |
570f89414b2c
Assetor is now responsible for copying assets, to allow customization
Ben Artin <ben@artins.org>
parents:
809
diff
changeset
|
114 if os.path.isfile(full_fn): |
570f89414b2c
Assetor is now responsible for copying assets, to allow customization
Ben Artin <ben@artins.org>
parents:
809
diff
changeset
|
115 dest_ap = os.path.join(dest_dir, fn) |
570f89414b2c
Assetor is now responsible for copying assets, to allow customization
Ben Artin <ben@artins.org>
parents:
809
diff
changeset
|
116 logger.debug(" %s -> %s" % (full_fn, dest_ap)) |
570f89414b2c
Assetor is now responsible for copying assets, to allow customization
Ben Artin <ben@artins.org>
parents:
809
diff
changeset
|
117 shutil.copy(full_fn, dest_ap) |