Mercurial > piecrust2
annotate piecrust/data/assetor.py @ 866:d9059257743c
refactor: Make the linker work again.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Mon, 12 Jun 2017 22:10:50 -0700 |
parents | fddaf43424e2 |
children | 757fba54bfd3 |
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 |
862
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
5 import collections.abc |
415
0e9a94b7fdfa
bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents:
329
diff
changeset
|
6 from piecrust import ASSET_DIR_SUFFIX |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
837
diff
changeset
|
7 from piecrust.sources.base import REL_ASSETS |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
8 from piecrust.uriutil import multi_replace |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
9 |
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 logger = logging.getLogger(__name__) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
12 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
13 |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
14 class UnsupportedAssetsError(Exception): |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
15 pass |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
16 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
17 |
862
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
18 class _AssetInfo: |
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
19 def __init__(self, content_item, uri): |
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
20 self.content_item = content_item |
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
21 self.uri = uri |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
22 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
23 |
862
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
24 class Assetor(collections.abc.Mapping): |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
837
diff
changeset
|
25 debug_render_doc = """Helps render URLs to files in the current page's |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
837
diff
changeset
|
26 asset folder.""" |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
837
diff
changeset
|
27 debug_render = [] |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
837
diff
changeset
|
28 debug_render_dynamic = ['_debugRenderAssetNames'] |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
837
diff
changeset
|
29 |
853
f070a4fc033c
core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
30 def __init__(self, page): |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
31 self._page = page |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
32 self._cache = None |
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 def __getattr__(self, name): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
35 try: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
36 self._cacheAssets() |
862
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
37 return self._cache[name].uri |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
38 except KeyError: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
39 raise AttributeError() |
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 __getitem__(self, key): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
42 self._cacheAssets() |
862
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
43 return self._cache[key].uri |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
44 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
45 def __iter__(self): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
46 self._cacheAssets() |
862
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
47 return self._cache.keys() |
25
65ae19c4e8a3
Copy page assets to bake output, use correct slashes when serving assets.
Ludovic Chabant <ludovic@chabant.com>
parents:
13
diff
changeset
|
48 |
862
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
49 def __len__(self): |
809
22c6f6a3d0a0
admin: Add ability to upload page assets.
Ludovic Chabant <ludovic@chabant.com>
parents:
472
diff
changeset
|
50 self._cacheAssets() |
862
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
51 return len(self._cache) |
415
0e9a94b7fdfa
bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents:
329
diff
changeset
|
52 |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
53 def _debugRenderAssetNames(self): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
54 self._cacheAssets() |
5 | 55 return list(self._cache.keys()) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
56 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
57 def _cacheAssets(self): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
58 if self._cache is not None: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
59 return |
837
ad8f48a31c62
assets: Fix crash when a page doesn't have assets.
Ludovic Chabant <ludovic@chabant.com>
parents:
832
diff
changeset
|
60 |
862
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
61 source = self._page.source |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
837
diff
changeset
|
62 content_item = self._page.content_item |
862
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
63 |
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
64 assets = source.getRelatedContents(content_item, REL_ASSETS) |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
837
diff
changeset
|
65 if assets is None: |
862
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
66 self._cache = {} |
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
67 return |
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
68 |
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
69 self._cache = {} |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
70 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
837
diff
changeset
|
71 app = source.app |
862
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
72 root_dir = app.root_dir |
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
73 asset_url_format = app.config.get('site/asset_url_format') |
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
74 |
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
75 page_uri = self._page.getUri() |
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
76 pretty_urls = app.config.get('site/pretty_urls') |
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
77 if not pretty_urls: |
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
78 page_uri, _ = os.path.splitext(page_uri) |
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
79 |
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
80 uri_build_tokens = { |
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
81 '%path%': None, |
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
82 '%filename%': None, |
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
83 '%page_uri%': page_uri |
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
84 } |
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
85 |
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
86 for a in assets: |
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
87 name = a.metadata['name'] |
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
88 if name in self._cache: |
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
89 raise UnsupportedAssetsError( |
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
90 "An asset with name '%s' already exists for item '%s'. " |
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
91 "Do you have multiple assets with colliding names?" % |
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
92 (name, content_item.spec)) |
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
93 |
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
94 # TODO: this assumes a file-system source! |
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
95 uri_build_tokens['%path%'] = ( |
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
96 os.path.relpath(a.spec, root_dir).replace('\\', '/')) |
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
97 uri_build_tokens['%filename%'] = a.metadata['filename'], |
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
98 uri = multi_replace(asset_url_format, uri_build_tokens) |
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
99 |
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
100 self._cache[name] = _AssetInfo(a, uri) |
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
101 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
837
diff
changeset
|
102 stack = app.env.render_ctx_stack |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
837
diff
changeset
|
103 cur_ctx = stack.current_ctx |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
837
diff
changeset
|
104 if cur_ctx is not None: |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
837
diff
changeset
|
105 cur_ctx.current_pass_info.used_assets = True |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
106 |
831
18978cf6d1ac
Removed pointless page argument from copyAssets
Ben Artin <ben@artins.org>
parents:
827
diff
changeset
|
107 def copyAssets(self, dest_dir): |
18978cf6d1ac
Removed pointless page argument from copyAssets
Ben Artin <ben@artins.org>
parents:
827
diff
changeset
|
108 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
|
109 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
|
110 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
|
111 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
|
112 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
|
113 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
|
114 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
|
115 shutil.copy(full_fn, dest_ap) |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
837
diff
changeset
|
116 |