# HG changeset patch # User Ludovic Chabant # Date 1590124103 25200 # Node ID 31e8ee0bf5b2e53f5544af7fd60268876960db43 # Parent e307f61d7034a19b970e66b727f6543c05a34cef prepare: Refactor scaffolding code. diff -r e307f61d7034 -r 31e8ee0bf5b2 piecrust/app.py --- a/piecrust/app.py Thu May 21 22:05:30 2020 -0700 +++ b/piecrust/app.py Thu May 21 22:08:23 2020 -0700 @@ -224,6 +224,19 @@ return pub return None + def getCommand(self, command_name): + for cmd in self.plugin_loader.getCommands(): + if cmd.name == command_name: + return cmd + return None + + def getCommandExtensions(self, command_name, supported_only=True): + extensions = self.plugin_loader.getCommandExtensions() + for e in extensions: + if (e.command_name == command_name and + (not supported_only or e.supports(self))): + yield e + def getPage(self, source, content_item): cache_key = '%s@%s' % (source.name, content_item.spec) return self.env.page_repository.get( diff -r e307f61d7034 -r 31e8ee0bf5b2 piecrust/commands/builtin/scaffolding.py --- a/piecrust/commands/builtin/scaffolding.py Thu May 21 22:05:30 2020 -0700 +++ b/piecrust/commands/builtin/scaffolding.py Thu May 21 22:08:23 2020 -0700 @@ -60,48 +60,18 @@ ctx.args.sub_func(ctx) def _doRun(self, ctx): - import time - from piecrust.uriutil import multi_replace from piecrust.sources.fs import FSContentSourceBase if not hasattr(ctx.args, 'source'): raise Exception("No source specified. " "Please run `chef prepare -h` for usage.") - - app = ctx.app - tpl_name = ctx.args.template - extensions = self.getExtensions(app) - 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) + source = ctx.args.source - source = ctx.args.source - content_item = source.createContent(vars(ctx.args)) - if content_item is None: - raise Exception("Can't create item.") - - 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 ctx.args.force else 'x' - with source.openItem(content_item, mode) as f: - f.write(tpl_text) + content_item = build_content( + source, + vars(ctx.args), + ctx.args.template, + force_overwrite=ctx.args.force) # If this was a file-system content item, see if we need to auto-open # an editor on it.