Mercurial > piecrust2
annotate piecrust/sources/generator.py @ 856:9bb22bbe093c
refactor: Make the blog archives functional again.
The blog archives are using the same pattern as the taxonomy support.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Tue, 06 Jun 2017 01:23:25 -0700 |
parents | |
children | d1095774bfcf |
rev | line source |
---|---|
856
9bb22bbe093c
refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
1 import io |
9bb22bbe093c
refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
2 import time |
9bb22bbe093c
refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
3 from werkzeug.utils import cached_property |
9bb22bbe093c
refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
4 from piecrust.configuration import ConfigurationError |
9bb22bbe093c
refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
5 from piecrust.sources.base import ContentSource, GeneratedContentException |
9bb22bbe093c
refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
6 |
9bb22bbe093c
refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
7 |
9bb22bbe093c
refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
8 class GeneratorSourceBase(ContentSource): |
9bb22bbe093c
refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
9 def __init__(self, app, name, config): |
9bb22bbe093c
refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
10 super().__init__(app, name, config) |
9bb22bbe093c
refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
11 |
9bb22bbe093c
refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
12 source_name = config.get('source') |
9bb22bbe093c
refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
13 if source_name is None: |
9bb22bbe093c
refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
14 raise ConfigurationError( |
9bb22bbe093c
refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
15 "Taxonomy source '%s' requires an inner source." % name) |
9bb22bbe093c
refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
16 self._inner_source_name = source_name |
9bb22bbe093c
refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
17 |
9bb22bbe093c
refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
18 self._raw_item = '' |
9bb22bbe093c
refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
19 |
9bb22bbe093c
refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
20 @cached_property |
9bb22bbe093c
refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
21 def inner_source(self): |
9bb22bbe093c
refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
22 return self.app.getSource(self._inner_source_name) |
9bb22bbe093c
refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
23 |
9bb22bbe093c
refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
24 def getContents(self, group): |
9bb22bbe093c
refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
25 # Our content is procedurally generated from other content sources, |
9bb22bbe093c
refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
26 # so we really don't support listing anything here -- it would be |
9bb22bbe093c
refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
27 # typically quite costly. |
9bb22bbe093c
refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
28 raise GeneratedContentException() |
9bb22bbe093c
refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
29 |
9bb22bbe093c
refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
30 def openItem(self, item, mode='r', **kwargs): |
9bb22bbe093c
refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
31 return io.StringIO(self._raw_item) |
9bb22bbe093c
refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
32 |
9bb22bbe093c
refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
33 def getItemMtime(self, item): |
9bb22bbe093c
refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
34 return time.time() |
9bb22bbe093c
refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
35 |