Mercurial > piecrust2
annotate piecrust/sources/prose.py @ 161:516db87a04d4
cosmetic: pep8 compliance.
| author | Ludovic Chabant <ludovic@chabant.com> |
|---|---|
| date | Thu, 01 Jan 2015 21:10:16 -0800 |
| parents | 55910ab4bfea |
| children | c3831a762bc2 |
| 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 |
|
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
19 def buildPageFactories(self): |
|
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
20 factories = super(ProseSource, self).buildPageFactories() |
|
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
21 for f in factories: |
|
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
22 f.metadata['config'] = self._makeConfig(f.path) |
|
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
23 logger.debug(f.__dict__) |
|
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
24 yield f |
|
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
25 |
|
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
26 def findPagePath(self, metadata, mode): |
|
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
27 rel_path, metadata = super(ProseSource, self).findPagePath(metadata, mode) |
|
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
28 if rel_path: |
|
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
29 metadata['config'] = self._makeConfig(self.resolveRef(rel_path)) |
|
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
30 return rel_path, metadata |
|
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 def _makeConfig(self, path): |
|
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
33 c = dict(self.config_recipe) |
|
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
34 if c.get('title') == '%first_line%': |
|
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
35 c['title'] = get_first_line(path) |
|
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
36 return c |
|
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
37 |
|
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
38 |
|
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
39 def get_first_line(path): |
|
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
40 with open(path, 'r') as f: |
|
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
41 while True: |
|
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
42 l = f.readline() |
|
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
43 if not l: |
|
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
44 break |
|
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
45 l = l.strip() |
|
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
46 if not l: |
|
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
47 continue |
|
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
48 return l |
|
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
49 return None |
|
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
50 |
