changeset 1182:31e8ee0bf5b2

prepare: Refactor scaffolding code.
author Ludovic Chabant <ludovic@chabant.com>
date Thu, 21 May 2020 22:08:23 -0700
parents e307f61d7034
children 62900c42d6dd
files piecrust/app.py piecrust/commands/builtin/scaffolding.py
diffstat 2 files changed, 19 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- 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(
--- 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.