annotate piecrust/sources/prose.py @ 170:c3831a762bc2

sources: Make the `SimplePageSource` more extensible, fix bugs in `prose` source. The `SimplePageSource` now calls a `_populateMetadata` function that subclasses can override to add/edit their custom metadata everwhere it would be returned to the system.
author Ludovic Chabant <ludovic@chabant.com>
date Sat, 03 Jan 2015 20:49:00 -0800
parents 55910ab4bfea
children e61fbae61402
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
3 import logging
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
4 from piecrust.sources.base import (
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
5 SimplePageSource, SimplePaginationSourceMixin)
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
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
11 class ProseSource(SimplePageSource,
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
12 SimplePaginationSourceMixin):
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
13 SOURCE_NAME = 'prose'
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
14
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15 def __init__(self, app, name, config):
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
16 super(ProseSource, self).__init__(app, name, config)
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
17 self.config_recipe = config.get('config', {})
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
18
170
c3831a762bc2 sources: Make the `SimplePageSource` more extensible, fix bugs in `prose` source.
Ludovic Chabant <ludovic@chabant.com>
parents: 157
diff changeset
19 def _populateMetadata(self, rel_path, metadata):
c3831a762bc2 sources: Make the `SimplePageSource` more extensible, fix bugs in `prose` source.
Ludovic Chabant <ludovic@chabant.com>
parents: 157
diff changeset
20 metadata['config'] = self._makeConfig(rel_path)
157
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
21
170
c3831a762bc2 sources: Make the `SimplePageSource` more extensible, fix bugs in `prose` source.
Ludovic Chabant <ludovic@chabant.com>
parents: 157
diff changeset
22 def _makeConfig(self, rel_path):
157
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
23 c = dict(self.config_recipe)
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
24 if c.get('title') == '%first_line%':
170
c3831a762bc2 sources: Make the `SimplePageSource` more extensible, fix bugs in `prose` source.
Ludovic Chabant <ludovic@chabant.com>
parents: 157
diff changeset
25 path = os.path.join(self.fs_endpoint_path, rel_path)
157
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
26 c['title'] = get_first_line(path)
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
27 return c
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
28
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
29
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
30 def get_first_line(path):
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
31 with open(path, 'r') as f:
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
32 while True:
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
33 l = f.readline()
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
34 if not l:
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
35 break
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
36 l = l.strip()
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 continue
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
39 return l
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
40 return None
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
41