annotate piecrust/sources/prose.py @ 211:0b2d8f6df4ce

internal: A bit of input validation for source APIs.
author Ludovic Chabant <ludovic@chabant.com>
date Fri, 30 Jan 2015 23:51:54 -0800
parents e61fbae61402
children f130365568ff
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 (
186
e61fbae61402 sources: Pass any current mode to `_populateMetadata` when finding pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 170
diff changeset
5 SimplePageSource, SimplePaginationSourceMixin,
e61fbae61402 sources: Pass any current mode to `_populateMetadata` when finding pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 170
diff changeset
6 MODE_CREATING)
157
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
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
9 logger = logging.getLogger(__name__)
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
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
12 class ProseSource(SimplePageSource,
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
13 SimplePaginationSourceMixin):
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
14 SOURCE_NAME = 'prose'
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
16 def __init__(self, app, name, config):
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
17 super(ProseSource, self).__init__(app, name, config)
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
18 self.config_recipe = config.get('config', {})
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
19
186
e61fbae61402 sources: Pass any current mode to `_populateMetadata` when finding pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 170
diff changeset
20 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
21 metadata['config'] = self._makeConfig(rel_path, mode)
157
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
22
186
e61fbae61402 sources: Pass any current mode to `_populateMetadata` when finding pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 170
diff changeset
23 def _makeConfig(self, rel_path, mode):
157
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
24 c = dict(self.config_recipe)
186
e61fbae61402 sources: Pass any current mode to `_populateMetadata` when finding pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 170
diff changeset
25 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
26 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
27 c['title'] = get_first_line(path)
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
28 return c
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
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
31 def get_first_line(path):
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
32 with open(path, 'r') as f:
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
33 while True:
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
34 l = f.readline()
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
35 if not l:
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
36 break
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
37 l = l.strip()
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
38 if not l:
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
39 continue
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
40 return l
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
41 return None
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
42