Mercurial > piecrust2
comparison piecrust/commands/builtin/scaffolding.py @ 1186:2ead9dcb6bec
prepare: Fix scaffolding refactor code.
For some reason the previous commit was missing some changes.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Fri, 23 Oct 2020 23:50:42 -0700 |
parents | 31e8ee0bf5b2 |
children |
comparison
equal
deleted
inserted
replaced
1185:24413a2963b9 | 1186:2ead9dcb6bec |
---|---|
1 import os | 1 import os |
2 import os.path | 2 import os.path |
3 import logging | 3 import logging |
4 import time | |
4 from piecrust.commands.base import ( | 5 from piecrust.commands.base import ( |
5 ChefCommand, ExtendableChefCommand, ChefCommandExtension) | 6 ChefCommand, ExtendableChefCommand, ChefCommandExtension) |
6 | 7 |
7 | 8 |
8 logger = logging.getLogger(__name__) | 9 logger = logging.getLogger(__name__) |
10 | |
11 | |
12 def build_content(source, content_metadata, tpl_name, force_overwrite=False): | |
13 app = source.app | |
14 extensions = app.getCommandExtensions('prepare') | |
15 ext = next( | |
16 filter( | |
17 lambda e: tpl_name in e.getTemplateNames(app), | |
18 extensions), | |
19 None) | |
20 if ext is None: | |
21 raise Exception("No such page template: %s" % tpl_name) | |
22 tpl_text = ext.getTemplate(app, tpl_name) | |
23 if tpl_text is None: | |
24 raise Exception("Error loading template: %s" % tpl_name) | |
25 | |
26 content_item = source.createContent(content_metadata) | |
27 if content_item is None: | |
28 raise Exception("Can't create item.") | |
29 | |
30 from piecrust.uriutil import multi_replace | |
31 | |
32 config_tokens = { | |
33 '%title%': "Untitled Content", | |
34 '%time.today%': time.strftime('%Y/%m/%d'), | |
35 '%time.now%': time.strftime('%H:%M:%S') | |
36 } | |
37 config = content_item.metadata.get('config') | |
38 if config: | |
39 for k, v in config.items(): | |
40 config_tokens['%%%s%%' % k] = v | |
41 tpl_text = multi_replace(tpl_text, config_tokens) | |
42 | |
43 logger.info("Creating content: %s" % content_item.spec) | |
44 mode = 'w' if force_overwrite else 'x' | |
45 with source.openItem(content_item, mode) as f: | |
46 f.write(tpl_text) | |
47 | |
48 return content_item | |
9 | 49 |
10 | 50 |
11 class PrepareCommand(ExtendableChefCommand): | 51 class PrepareCommand(ExtendableChefCommand): |
12 """ Chef command for creating pages with some default content. | 52 """ Chef command for creating pages with some default content. |
13 """ | 53 """ |