Mercurial > piecrust2
annotate piecrust/data/linker.py @ 854:08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
- Make a few APIs simpler.
- Content pipelines create their own jobs, so that generator sources can
keep aborting in `getContents`, but rely on their pipeline to generate
pages for baking.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sun, 04 Jun 2017 23:34:28 -0700 |
parents | f070a4fc033c |
children | fddaf43424e2 |
rev | line source |
---|---|
172
4fc1d306046b
linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
1 import logging |
440
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
411
diff
changeset
|
2 from piecrust.data.paginationdata import PaginationData |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
3 from piecrust.sources.base import ( |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
4 REL_PARENT_GROUP, REL_LOGICAL_PARENT_ITEM, REL_LOGICAl_CHILD_GROUP) |
172
4fc1d306046b
linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
5 |
4fc1d306046b
linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
6 |
4fc1d306046b
linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
7 logger = logging.getLogger(__name__) |
4fc1d306046b
linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
8 |
4fc1d306046b
linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
9 |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
10 _unloaded = object() |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
11 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
12 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
13 class Linker: |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
14 """ A template-exposed data class that lets the user navigate the |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
15 logical hierarchy of pages in a page source. |
247
d9d5c5de02a8
data: Add a top level wrapper for `Linker`.
Ludovic Chabant <ludovic@chabant.com>
parents:
242
diff
changeset
|
16 """ |
408
fd8e39254da0
debug: Better debug info output for iterators, providers, and linkers.
Ludovic Chabant <ludovic@chabant.com>
parents:
404
diff
changeset
|
17 debug_render = ['parent', 'ancestors', 'siblings', 'children', 'root', |
fd8e39254da0
debug: Better debug info output for iterators, providers, and linkers.
Ludovic Chabant <ludovic@chabant.com>
parents:
404
diff
changeset
|
18 'forpath'] |
590
3cca1f6bd610
debug: Fix how the linker shows children/siblings/etc. in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
518
diff
changeset
|
19 debug_render_invoke = ['parent', 'ancestors', 'siblings', 'children', |
3cca1f6bd610
debug: Fix how the linker shows children/siblings/etc. in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
518
diff
changeset
|
20 'root'] |
3cca1f6bd610
debug: Fix how the linker shows children/siblings/etc. in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
518
diff
changeset
|
21 debug_render_redirect = { |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
590
diff
changeset
|
22 'ancestors': '_debugRenderAncestors', |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
590
diff
changeset
|
23 'siblings': '_debugRenderSiblings', |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
590
diff
changeset
|
24 'children': '_debugRenderChildren', |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
590
diff
changeset
|
25 'root': '_debugRenderRoot'} |
408
fd8e39254da0
debug: Better debug info output for iterators, providers, and linkers.
Ludovic Chabant <ludovic@chabant.com>
parents:
404
diff
changeset
|
26 |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
27 def __init__(self, page): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
28 self._page = page |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
29 self._content_item = page.content_item |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
30 self._source = page.source |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
31 self._app = page.app |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
32 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
33 self._parent = _unloaded |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
34 self._ancestors = None |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
35 self._siblings = None |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
36 self._children = None |
404
27b10024f8d8
linker: Add ability to return the parent and ancestors of a page.
Ludovic Chabant <ludovic@chabant.com>
parents:
402
diff
changeset
|
37 |
27b10024f8d8
linker: Add ability to return the parent and ancestors of a page.
Ludovic Chabant <ludovic@chabant.com>
parents:
402
diff
changeset
|
38 @property |
27b10024f8d8
linker: Add ability to return the parent and ancestors of a page.
Ludovic Chabant <ludovic@chabant.com>
parents:
402
diff
changeset
|
39 def parent(self): |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
40 if self._parent is _unloaded: |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
41 pi = self._source.getRelatedContents(self._content_item, |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
42 REL_LOGICAL_PARENT_ITEM) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
43 if pi is not None: |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
44 pipage = self._app.getPage(self._source, pi) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
45 self._parent = PaginationData(pipage) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
46 else: |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
47 self._parent = None |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
48 return self._parent |
404
27b10024f8d8
linker: Add ability to return the parent and ancestors of a page.
Ludovic Chabant <ludovic@chabant.com>
parents:
402
diff
changeset
|
49 |
27b10024f8d8
linker: Add ability to return the parent and ancestors of a page.
Ludovic Chabant <ludovic@chabant.com>
parents:
402
diff
changeset
|
50 @property |
27b10024f8d8
linker: Add ability to return the parent and ancestors of a page.
Ludovic Chabant <ludovic@chabant.com>
parents:
402
diff
changeset
|
51 def ancestors(self): |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
52 if self._ancestors is None: |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
53 cur_item = self._content_item |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
54 self._ancestors = [] |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
55 while True: |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
56 pi = self._source.getRelatedContents( |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
57 cur_item, REL_LOGICAL_PARENT_ITEM) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
58 if pi is not None: |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
59 pipage = self._app.getPage(self._source, pi) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
60 self._ancestors.append(PaginationData(pipage)) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
61 cur_item = pi |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
62 else: |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
63 break |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
64 return self._ancestors |
247
d9d5c5de02a8
data: Add a top level wrapper for `Linker`.
Ludovic Chabant <ludovic@chabant.com>
parents:
242
diff
changeset
|
65 |
d9d5c5de02a8
data: Add a top level wrapper for `Linker`.
Ludovic Chabant <ludovic@chabant.com>
parents:
242
diff
changeset
|
66 @property |
d9d5c5de02a8
data: Add a top level wrapper for `Linker`.
Ludovic Chabant <ludovic@chabant.com>
parents:
242
diff
changeset
|
67 def siblings(self): |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
68 if self._siblings is None: |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
69 self._siblings = [] |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
70 parent_group = self._source.getRelatedContents( |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
71 self._content_item, REL_PARENT_GROUP) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
72 for i in self._source.getContents(parent_group): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
73 if not i.is_group: |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
74 ipage = self._app.getPage(self._source, i) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
75 self._siblings.append(PaginationData(ipage)) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
76 return self._siblings |
247
d9d5c5de02a8
data: Add a top level wrapper for `Linker`.
Ludovic Chabant <ludovic@chabant.com>
parents:
242
diff
changeset
|
77 |
d9d5c5de02a8
data: Add a top level wrapper for `Linker`.
Ludovic Chabant <ludovic@chabant.com>
parents:
242
diff
changeset
|
78 @property |
d9d5c5de02a8
data: Add a top level wrapper for `Linker`.
Ludovic Chabant <ludovic@chabant.com>
parents:
242
diff
changeset
|
79 def children(self): |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
80 if self._children is None: |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
81 self._children = [] |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
82 child_group = self._source.getRelatedContents( |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
83 self._content_item, REL_LOGICAl_CHILD_GROUP) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
84 if child_group: |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
85 for i in self._source.getContents(child_group): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
86 ipage = self._app.getPage(self._source, i) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
87 self._children.append(PaginationData(ipage)) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
88 return self._children |
404
27b10024f8d8
linker: Add ability to return the parent and ancestors of a page.
Ludovic Chabant <ludovic@chabant.com>
parents:
402
diff
changeset
|
89 |
590
3cca1f6bd610
debug: Fix how the linker shows children/siblings/etc. in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
518
diff
changeset
|
90 def _debugRenderAncestors(self): |
3cca1f6bd610
debug: Fix how the linker shows children/siblings/etc. in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
518
diff
changeset
|
91 return [i.name for i in self.ancestors] |
3cca1f6bd610
debug: Fix how the linker shows children/siblings/etc. in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
518
diff
changeset
|
92 |
3cca1f6bd610
debug: Fix how the linker shows children/siblings/etc. in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
518
diff
changeset
|
93 def _debugRenderSiblings(self): |
3cca1f6bd610
debug: Fix how the linker shows children/siblings/etc. in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
518
diff
changeset
|
94 return [i.name for i in self.siblings] |
3cca1f6bd610
debug: Fix how the linker shows children/siblings/etc. in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
518
diff
changeset
|
95 |
3cca1f6bd610
debug: Fix how the linker shows children/siblings/etc. in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
518
diff
changeset
|
96 def _debugRenderChildren(self): |
3cca1f6bd610
debug: Fix how the linker shows children/siblings/etc. in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
518
diff
changeset
|
97 return [i.name for i in self.children] |
3cca1f6bd610
debug: Fix how the linker shows children/siblings/etc. in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
518
diff
changeset
|
98 |
3cca1f6bd610
debug: Fix how the linker shows children/siblings/etc. in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
518
diff
changeset
|
99 def _debugRenderRoot(self): |
3cca1f6bd610
debug: Fix how the linker shows children/siblings/etc. in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
518
diff
changeset
|
100 r = self.root |
3cca1f6bd610
debug: Fix how the linker shows children/siblings/etc. in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
518
diff
changeset
|
101 if r is not None: |
3cca1f6bd610
debug: Fix how the linker shows children/siblings/etc. in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
518
diff
changeset
|
102 return r.name |
3cca1f6bd610
debug: Fix how the linker shows children/siblings/etc. in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
518
diff
changeset
|
103 return None |
3cca1f6bd610
debug: Fix how the linker shows children/siblings/etc. in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
518
diff
changeset
|
104 |