Mercurial > piecrust2
annotate piecrust/data/paginationdata.py @ 852:4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
* Everything is a `ContentSource`, including assets directories.
* Most content sources are subclasses of the base file-system source.
* A source is processed by a "pipeline", and there are 2 built-in pipelines,
one for assets and one for pages. The asset pipeline is vaguely functional,
but the page pipeline is completely broken right now.
* Rewrite the baking process as just running appropriate pipelines on each
content item. This should allow for better parallelization.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Wed, 17 May 2017 00:11:48 -0700 |
parents | a12ad254176e |
children | f070a4fc033c |
rev | line source |
---|---|
440
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
1 import time |
718
d4408fbbbc7d
internal: Prevent crash because of missing logger.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
2 import logging |
440
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
3 from piecrust.data.pagedata import LazyPageConfigData |
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
4 |
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
5 |
718
d4408fbbbc7d
internal: Prevent crash because of missing logger.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
6 logger = logging.getLogger(__name__) |
d4408fbbbc7d
internal: Prevent crash because of missing logger.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
7 |
d4408fbbbc7d
internal: Prevent crash because of missing logger.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
8 |
440
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
9 class PaginationData(LazyPageConfigData): |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
830
diff
changeset
|
10 def __init__(self, qualified_page): |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
830
diff
changeset
|
11 super(PaginationData, self).__init__(qualified_page.page) |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
830
diff
changeset
|
12 self._qualified_page = qualified_page |
440
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
13 |
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
14 def _load(self): |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
830
diff
changeset
|
15 from piecrust.uriutil import split_uri |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
830
diff
changeset
|
16 |
440
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
17 page = self._page |
729
e35407c60e00
templating: Make blog archives generator expose more templating data.
Ludovic Chabant <ludovic@chabant.com>
parents:
718
diff
changeset
|
18 dt = page.datetime |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
830
diff
changeset
|
19 page_url = self._qualified_page.uri |
440
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
20 _, slug = split_uri(page.app, page_url) |
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
21 self._setValue('url', page_url) |
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
22 self._setValue('slug', slug) |
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
23 self._setValue( |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
830
diff
changeset
|
24 'timestamp', |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
830
diff
changeset
|
25 time.mktime(page.datetime.timetuple())) |
729
e35407c60e00
templating: Make blog archives generator expose more templating data.
Ludovic Chabant <ludovic@chabant.com>
parents:
718
diff
changeset
|
26 self._setValue('datetime', { |
e35407c60e00
templating: Make blog archives generator expose more templating data.
Ludovic Chabant <ludovic@chabant.com>
parents:
718
diff
changeset
|
27 'year': dt.year, 'month': dt.month, 'day': dt.day, |
e35407c60e00
templating: Make blog archives generator expose more templating data.
Ludovic Chabant <ludovic@chabant.com>
parents:
718
diff
changeset
|
28 'hour': dt.hour, 'minute': dt.minute, 'second': dt.second}) |
440
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
29 date_format = page.app.config.get('site/date_format') |
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
30 if date_format: |
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
31 self._setValue('date', page.datetime.strftime(date_format)) |
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
32 self._setValue('mtime', page.path_mtime) |
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
33 |
830
a12ad254176e
Renamed buildPageAssetor to buildAssetor
Ben Artin <ben@artins.org>
parents:
828
diff
changeset
|
34 assetor = page.source.buildAssetor(page, page_url) |
440
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
35 self._setValue('assets', assetor) |
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
36 |
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
37 segment_names = page.config.get('segments') |
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
38 for name in segment_names: |
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
39 self._mapLoader(name, self._load_rendered_segment) |
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
40 |
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
41 def _load_rendered_segment(self, data, name): |
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
42 do_render = True |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
830
diff
changeset
|
43 stack = self._page.app.env.render_ctx_stack |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
830
diff
changeset
|
44 if stack.hasPage(self._page): |
440
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
45 # This is the pagination data for the page that is currently |
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
46 # being rendered! Inception! But this is possible... so just |
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
47 # prevent infinite recursion. |
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
48 do_render = False |
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
49 |
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
50 assert self is data |
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
51 |
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
52 if do_render: |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
830
diff
changeset
|
53 uri = self._qualified_page.uri |
440
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
54 try: |
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
55 from piecrust.rendering import ( |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
830
diff
changeset
|
56 RenderingContext, render_page_segments) |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
830
diff
changeset
|
57 ctx = RenderingContext(self._qualified_page) |
440
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
58 render_result = render_page_segments(ctx) |
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
59 segs = render_result.segments |
711
ab5c6a8ae90a
bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
440
diff
changeset
|
60 except Exception as ex: |
ab5c6a8ae90a
bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
440
diff
changeset
|
61 logger.exception(ex) |
440
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
62 raise Exception( |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
830
diff
changeset
|
63 "Error rendering segments for '%s'" % uri) from ex |
440
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
64 else: |
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
65 segs = {} |
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
66 for name in self._page.config.get('segments'): |
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
67 segs[name] = "<unavailable: current page>" |
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
68 |
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
69 for k, v in segs.items(): |
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
70 self._unmapLoader(k) |
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
71 self._setValue(k, v) |
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
72 |
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
73 if 'content.abstract' in segs: |
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
74 self._setValue('content', segs['content.abstract']) |
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
75 self._setValue('has_more', True) |
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
76 if name == 'content': |
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
77 return segs['content.abstract'] |
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
78 |
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
79 return segs[name] |
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
80 |