Mercurial > piecrust2
annotate piecrust/data/assetor.py @ 868:8d25f76fce98
bake: Add ability to specify pipelines to exclude during the bake.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Mon, 12 Jun 2017 22:22:19 -0700 |
parents | 757fba54bfd3 |
children | 48d25fd68b8d |
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 |
867
757fba54bfd3
refactor: Improve pagination and iterators to work with other sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
862
diff
changeset
|
24 class Assetor(collections.abc.Sequence): |
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 |
867
757fba54bfd3
refactor: Improve pagination and iterators to work with other sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
862
diff
changeset
|
32 self._cache_map = None |
757fba54bfd3
refactor: Improve pagination and iterators to work with other sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
862
diff
changeset
|
33 self._cache_list = None |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
34 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
35 def __getattr__(self, name): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
36 try: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
37 self._cacheAssets() |
862
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
38 return self._cache[name].uri |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
39 except KeyError: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
40 raise AttributeError() |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
41 |
867
757fba54bfd3
refactor: Improve pagination and iterators to work with other sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
862
diff
changeset
|
42 def __getitem__(self, i): |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
43 self._cacheAssets() |
867
757fba54bfd3
refactor: Improve pagination and iterators to work with other sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
862
diff
changeset
|
44 return self._cache_list[i] |
25
65ae19c4e8a3
Copy page assets to bake output, use correct slashes when serving assets.
Ludovic Chabant <ludovic@chabant.com>
parents:
13
diff
changeset
|
45 |
862
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
46 def __len__(self): |
809
22c6f6a3d0a0
admin: Add ability to upload page assets.
Ludovic Chabant <ludovic@chabant.com>
parents:
472
diff
changeset
|
47 self._cacheAssets() |
867
757fba54bfd3
refactor: Improve pagination and iterators to work with other sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
862
diff
changeset
|
48 return len(self._cache_list) |
415
0e9a94b7fdfa
bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents:
329
diff
changeset
|
49 |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
50 def _debugRenderAssetNames(self): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
51 self._cacheAssets() |
5 | 52 return list(self._cache.keys()) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
53 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
54 def _cacheAssets(self): |
867
757fba54bfd3
refactor: Improve pagination and iterators to work with other sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
862
diff
changeset
|
55 if self._cache_map is not None: |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
56 return |
837
ad8f48a31c62
assets: Fix crash when a page doesn't have assets.
Ludovic Chabant <ludovic@chabant.com>
parents:
832
diff
changeset
|
57 |
862
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
58 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
|
59 content_item = self._page.content_item |
867
757fba54bfd3
refactor: Improve pagination and iterators to work with other sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
862
diff
changeset
|
60 assets = source.getRelatedContents(content_item, REL_ASSETS) |
862
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
61 |
867
757fba54bfd3
refactor: Improve pagination and iterators to work with other sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
862
diff
changeset
|
62 self._cache_map = {} |
757fba54bfd3
refactor: Improve pagination and iterators to work with other sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
862
diff
changeset
|
63 self._cache_list = [] |
757fba54bfd3
refactor: Improve pagination and iterators to work with other sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
862
diff
changeset
|
64 |
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 return |
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
67 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
837
diff
changeset
|
68 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
|
69 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
|
70 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
|
71 |
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
72 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
|
73 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
|
74 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
|
75 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
|
76 |
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
77 uri_build_tokens = { |
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
78 '%path%': None, |
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
79 '%filename%': None, |
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
80 '%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
|
81 } |
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
82 |
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
83 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
|
84 name = a.metadata['name'] |
867
757fba54bfd3
refactor: Improve pagination and iterators to work with other sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
862
diff
changeset
|
85 if name in self._cache_map: |
862
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
86 raise UnsupportedAssetsError( |
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
87 "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
|
88 "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
|
89 (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
|
90 |
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
91 # 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
|
92 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
|
93 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
|
94 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
|
95 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
|
96 |
867
757fba54bfd3
refactor: Improve pagination and iterators to work with other sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
862
diff
changeset
|
97 self._cache_map[name] = _AssetInfo(a, uri) |
757fba54bfd3
refactor: Improve pagination and iterators to work with other sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
862
diff
changeset
|
98 self._cache_list.append(uri) |
862
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
99 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
837
diff
changeset
|
100 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
|
101 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
|
102 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
|
103 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
|
104 |
831
18978cf6d1ac
Removed pointless page argument from copyAssets
Ben Artin <ben@artins.org>
parents:
827
diff
changeset
|
105 def copyAssets(self, dest_dir): |
18978cf6d1ac
Removed pointless page argument from copyAssets
Ben Artin <ben@artins.org>
parents:
827
diff
changeset
|
106 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
|
107 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
|
108 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
|
109 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
|
110 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
|
111 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
|
112 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
|
113 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
|
114 |