# HG changeset patch # User Ludovic Chabant # Date 1603522242 25200 # Node ID 2ead9dcb6bec49720654f8add9f7112f90c7f801 # Parent 24413a2963b9e42fe8008ad13e86aeb491895462 prepare: Fix scaffolding refactor code. For some reason the previous commit was missing some changes. diff -r 24413a2963b9 -r 2ead9dcb6bec piecrust/commands/builtin/scaffolding.py --- a/piecrust/commands/builtin/scaffolding.py Thu Oct 01 10:58:33 2020 -0700 +++ b/piecrust/commands/builtin/scaffolding.py Fri Oct 23 23:50:42 2020 -0700 @@ -1,6 +1,7 @@ import os import os.path import logging +import time from piecrust.commands.base import ( ChefCommand, ExtendableChefCommand, ChefCommandExtension) @@ -8,6 +9,45 @@ logger = logging.getLogger(__name__) +def build_content(source, content_metadata, tpl_name, force_overwrite=False): + app = source.app + extensions = app.getCommandExtensions('prepare') + ext = next( + filter( + lambda e: tpl_name in e.getTemplateNames(app), + extensions), + None) + if ext is None: + raise Exception("No such page template: %s" % tpl_name) + tpl_text = ext.getTemplate(app, tpl_name) + if tpl_text is None: + raise Exception("Error loading template: %s" % tpl_name) + + content_item = source.createContent(content_metadata) + if content_item is None: + raise Exception("Can't create item.") + + from piecrust.uriutil import multi_replace + + config_tokens = { + '%title%': "Untitled Content", + '%time.today%': time.strftime('%Y/%m/%d'), + '%time.now%': time.strftime('%H:%M:%S') + } + config = content_item.metadata.get('config') + if config: + for k, v in config.items(): + config_tokens['%%%s%%' % k] = v + tpl_text = multi_replace(tpl_text, config_tokens) + + logger.info("Creating content: %s" % content_item.spec) + mode = 'w' if force_overwrite else 'x' + with source.openItem(content_item, mode) as f: + f.write(tpl_text) + + return content_item + + class PrepareCommand(ExtendableChefCommand): """ Chef command for creating pages with some default content. """