Mercurial > piecrust2
annotate piecrust/pipelines/page.py @ 876:d1095774bfcf
refactor: Fix some issues with record/cache entry collisions, add counters.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Thu, 15 Jun 2017 07:33:40 -0700 |
parents | 504ddb370df8 |
children | d6d35b2efd04 |
rev | line source |
---|---|
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
1 import logging |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
2 from piecrust.pipelines.base import ContentPipeline |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
3 from piecrust.pipelines._pagebaker import PageBaker |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
4 from piecrust.pipelines._pagerecords import PagePipelineRecordEntry |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
5 from piecrust.rendering import ( |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
6 RenderingContext, render_page_segments) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
7 from piecrust.sources.base import AbortedSourceUseError |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
8 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
9 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
10 logger = logging.getLogger(__name__) |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
11 |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
12 |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
13 class PagePipeline(ContentPipeline): |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
14 PIPELINE_NAME = 'page' |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
15 RECORD_ENTRY_CLASS = PagePipelineRecordEntry |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
16 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
17 def __init__(self, source, ppctx): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
18 super().__init__(source, ppctx) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
19 self._pagebaker = None |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
20 self._stats = source.app.env.stats |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
21 |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
22 def initialize(self): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
23 stats = self.app.env.stats |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
24 stats.registerCounter('SourceUseAbortions', raise_if_registered=False) |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
25 |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
26 self._pagebaker = PageBaker(self.app, |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
27 self.ctx.out_dir, |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
28 force=self.ctx.force) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
29 self._pagebaker.startWriterQueue() |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
30 |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
31 def mergeRecordEntry(self, record_entry, ctx): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
32 existing = ctx.record.getEntry(record_entry.item_spec) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
33 existing.errors += record_entry.errors |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
34 existing.flags |= record_entry.flags |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
35 existing.subs = record_entry.subs |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
36 |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
37 def run(self, job, ctx, result): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
38 pass_name = job.data.get('pass', 0) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
39 if pass_name == 0: |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
40 self._renderSegmentsOrPostpone(job.content_item, ctx, result) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
41 elif pass_name == 1: |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
42 self._fullRender(job.content_item, ctx, result) |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
43 |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
44 def getDeletions(self, ctx): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
45 for prev, cur in ctx.record_history.diffs: |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
46 if prev and not cur: |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
47 for sub in prev.subs: |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
48 yield (sub.out_path, 'previous source file was removed') |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
49 elif prev and cur: |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
50 prev_out_paths = [o.out_path for o in prev.subs] |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
51 cur_out_paths = [o.out_path for o in cur.subs] |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
52 diff = set(prev_out_paths) - set(cur_out_paths) |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
53 for p in diff: |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
54 yield (p, 'source file changed outputs') |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
55 |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
56 def collapseRecords(self, ctx): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
57 pass |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
58 |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
59 def shutdown(self): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
60 self._pagebaker.stopWriterQueue() |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
61 |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
62 def _renderSegmentsOrPostpone(self, content_item, ctx, result): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
63 # Here our job is to render the page's segments so that they're |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
64 # cached in memory and on disk... unless we detect that the page |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
65 # is using some other sources, in which case we abort and we'll try |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
66 # again on the second pass. |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
67 logger.debug("Rendering segments for: %s" % content_item.spec) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
68 record_entry = result.record_entry |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
69 stats = self.app.env.stats |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
70 |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
71 page = self.app.getPage(self.source, content_item) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
72 record_entry.config = page.config.getAll() |
871
504ddb370df8
refactor: Fixing some issues with baking assets.
Ludovic Chabant <ludovic@chabant.com>
parents:
854
diff
changeset
|
73 record_entry.timestamp = page.datetime.timestamp() |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
74 |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
75 rdrctx = RenderingContext(page) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
76 self.app.env.abort_source_use = True |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
77 try: |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
78 render_page_segments(rdrctx) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
79 except AbortedSourceUseError: |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
80 logger.debug("Page was aborted for using source: %s" % |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
81 content_item.spec) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
82 stats.stepCounter("SourceUseAbortions") |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
83 finally: |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
84 self.app.env.abort_source_use = False |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
85 |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
86 result.next_pass_job = self.createJob(content_item) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
87 result.next_pass_job.data.update({ |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
88 'pass': 1, |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
89 'record_entry': record_entry |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
90 }) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
91 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
92 def _fullRender(self, content_item, ctx, result): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
93 logger.debug("Full render for: %s" % content_item.spec) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
94 page = self.app.getPage(self.source, content_item) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
95 prev_entry = ctx.previous_entry |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
96 cur_entry = result.record_entry |
871
504ddb370df8
refactor: Fixing some issues with baking assets.
Ludovic Chabant <ludovic@chabant.com>
parents:
854
diff
changeset
|
97 self._pagebaker.bake(page, prev_entry, cur_entry) |