Mercurial > piecrust2
annotate piecrust/sources/prose.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 | 65db6df28120 |
children | f070a4fc033c |
rev | line source |
---|---|
157
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
1 import os |
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
2 import os.path |
369
4b1019bb2533
serve: Giant refactor to change how we handle data when serving pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
242
diff
changeset
|
3 import copy |
157
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
4 import logging |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
394
diff
changeset
|
5 from piecrust.sources.default import DefaultContentSource |
157
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
6 |
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
7 |
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
8 logger = logging.getLogger(__name__) |
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
9 |
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
10 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
394
diff
changeset
|
11 class ProseSource(DefaultContentSource): |
157
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
12 SOURCE_NAME = 'prose' |
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
13 |
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
14 def __init__(self, app, name, config): |
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
15 super(ProseSource, self).__init__(app, name, config) |
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
16 self.config_recipe = config.get('config', {}) |
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
17 |
186
e61fbae61402
sources: Pass any current mode to `_populateMetadata` when finding pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
170
diff
changeset
|
18 def _populateMetadata(self, rel_path, metadata, mode=None): |
e61fbae61402
sources: Pass any current mode to `_populateMetadata` when finding pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
170
diff
changeset
|
19 metadata['config'] = self._makeConfig(rel_path, mode) |
157
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
20 |
186
e61fbae61402
sources: Pass any current mode to `_populateMetadata` when finding pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
170
diff
changeset
|
21 def _makeConfig(self, rel_path, mode): |
369
4b1019bb2533
serve: Giant refactor to change how we handle data when serving pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
242
diff
changeset
|
22 c = copy.deepcopy(self.config_recipe) |
186
e61fbae61402
sources: Pass any current mode to `_populateMetadata` when finding pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
170
diff
changeset
|
23 if c.get('title') == '%first_line%' and mode != MODE_CREATING: |
170
c3831a762bc2
sources: Make the `SimplePageSource` more extensible, fix bugs in `prose` source.
Ludovic Chabant <ludovic@chabant.com>
parents:
157
diff
changeset
|
24 path = os.path.join(self.fs_endpoint_path, rel_path) |
369
4b1019bb2533
serve: Giant refactor to change how we handle data when serving pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
242
diff
changeset
|
25 try: |
4b1019bb2533
serve: Giant refactor to change how we handle data when serving pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
242
diff
changeset
|
26 c['title'] = get_first_line(path) |
394
65db6df28120
tests: Also mock `open` in Jinja to be able to use templates in bake tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
369
diff
changeset
|
27 except IOError: |
369
4b1019bb2533
serve: Giant refactor to change how we handle data when serving pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
242
diff
changeset
|
28 if mode == MODE_PARSING: |
4b1019bb2533
serve: Giant refactor to change how we handle data when serving pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
242
diff
changeset
|
29 raise |
157
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
30 return c |
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
31 |
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
32 |
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
33 def get_first_line(path): |
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
34 with open(path, 'r') as f: |
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
35 while True: |
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
36 l = f.readline() |
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
37 if not l: |
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
38 break |
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
39 l = l.strip() |
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
40 if not l: |
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
41 continue |
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
42 return l |
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
43 return None |
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
44 |