Mercurial > piecrust2
comparison piecrust/commands/builtin/scaffolding.py @ 1182:31e8ee0bf5b2
prepare: Refactor scaffolding code.
| author | Ludovic Chabant <ludovic@chabant.com> |
|---|---|
| date | Thu, 21 May 2020 22:08:23 -0700 |
| parents | aad9b5a0a809 |
| children | 2ead9dcb6bec |
comparison
equal
deleted
inserted
replaced
| 1177:e307f61d7034 | 1182:31e8ee0bf5b2 |
|---|---|
| 58 ctx.parser.parse_args(['prepare', '--help']) | 58 ctx.parser.parse_args(['prepare', '--help']) |
| 59 return | 59 return |
| 60 ctx.args.sub_func(ctx) | 60 ctx.args.sub_func(ctx) |
| 61 | 61 |
| 62 def _doRun(self, ctx): | 62 def _doRun(self, ctx): |
| 63 import time | |
| 64 from piecrust.uriutil import multi_replace | |
| 65 from piecrust.sources.fs import FSContentSourceBase | 63 from piecrust.sources.fs import FSContentSourceBase |
| 66 | 64 |
| 67 if not hasattr(ctx.args, 'source'): | 65 if not hasattr(ctx.args, 'source'): |
| 68 raise Exception("No source specified. " | 66 raise Exception("No source specified. " |
| 69 "Please run `chef prepare -h` for usage.") | 67 "Please run `chef prepare -h` for usage.") |
| 70 | |
| 71 app = ctx.app | |
| 72 tpl_name = ctx.args.template | |
| 73 extensions = self.getExtensions(app) | |
| 74 ext = next( | |
| 75 filter( | |
| 76 lambda e: tpl_name in e.getTemplateNames(app), | |
| 77 extensions), | |
| 78 None) | |
| 79 if ext is None: | |
| 80 raise Exception("No such page template: %s" % tpl_name) | |
| 81 tpl_text = ext.getTemplate(app, tpl_name) | |
| 82 if tpl_text is None: | |
| 83 raise Exception("Error loading template: %s" % tpl_name) | |
| 84 | |
| 85 source = ctx.args.source | 68 source = ctx.args.source |
| 86 content_item = source.createContent(vars(ctx.args)) | 69 |
| 87 if content_item is None: | 70 content_item = build_content( |
| 88 raise Exception("Can't create item.") | 71 source, |
| 89 | 72 vars(ctx.args), |
| 90 config_tokens = { | 73 ctx.args.template, |
| 91 '%title%': "Untitled Content", | 74 force_overwrite=ctx.args.force) |
| 92 '%time.today%': time.strftime('%Y/%m/%d'), | |
| 93 '%time.now%': time.strftime('%H:%M:%S') | |
| 94 } | |
| 95 config = content_item.metadata.get('config') | |
| 96 if config: | |
| 97 for k, v in config.items(): | |
| 98 config_tokens['%%%s%%' % k] = v | |
| 99 tpl_text = multi_replace(tpl_text, config_tokens) | |
| 100 | |
| 101 logger.info("Creating content: %s" % content_item.spec) | |
| 102 mode = 'w' if ctx.args.force else 'x' | |
| 103 with source.openItem(content_item, mode) as f: | |
| 104 f.write(tpl_text) | |
| 105 | 75 |
| 106 # If this was a file-system content item, see if we need to auto-open | 76 # If this was a file-system content item, see if we need to auto-open |
| 107 # an editor on it. | 77 # an editor on it. |
| 108 editor = ctx.app.config.get('prepare/editor') | 78 editor = ctx.app.config.get('prepare/editor') |
| 109 editor_type = ctx.app.config.get('prepare/editor_type', 'exe') | 79 editor_type = ctx.app.config.get('prepare/editor_type', 'exe') |
