Mercurial > piecrust2
annotate piecrust/commands/builtin/scaffolding.py @ 215:a47580a0955b
bake: Better error handling for the processing pipeline.
Pipeline jobs now keep track of whether they've seen any errors. This is
aggregated into an overall "success" flag for the processing record. Also, jobs
keep going as long as there's no critical (i.e. internal) failure happening.
Errors raised by processors are also better tracked: the actual processor that
failed, along with the input file, are tracks in the processing record.
The `bake` command returns a failure exit code if processing saw any error.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sat, 31 Jan 2015 17:08:02 -0800 |
parents | d5b7c2a4ec9d |
children | f130365568ff |
rev | line source |
---|---|
100
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
1 import os |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
2 import os.path |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
3 import re |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
4 import io |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
5 import time |
187
d5b7c2a4ec9d
prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
165
diff
changeset
|
6 import glob |
100
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
7 import logging |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
8 import textwrap |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
9 from piecrust import RESOURCES_DIR |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
10 from piecrust.chefutil import print_help_item |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
11 from piecrust.commands.base import ExtendableChefCommand, ChefCommandExtension |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
12 from piecrust.sources.base import IPreparingSource, MODE_CREATING |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
13 from piecrust.uriutil import multi_replace |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
14 |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
15 |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
16 logger = logging.getLogger(__name__) |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
17 |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
18 |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
19 def make_title(slug): |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
20 slug = re.sub(r'[\-_]', ' ', slug) |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
21 return slug.title() |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
22 |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
23 |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
24 class PrepareCommand(ExtendableChefCommand): |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
25 """ Chef command for creating pages with some default content. |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
26 """ |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
27 def __init__(self): |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
28 super(PrepareCommand, self).__init__() |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
29 self.name = 'prepare' |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
30 self.description = "Prepares new content for your website." |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
31 |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
32 def setupParser(self, parser, app): |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
33 # Don't setup anything if this is a null app |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
34 # (for when `chef` is run from outside a website) |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
35 if app.root_dir is None: |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
36 return |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
37 |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
38 subparsers = parser.add_subparsers() |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
39 for src in app.sources: |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
40 if not isinstance(src, IPreparingSource): |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
41 logger.debug("Skipping source '%s' because it's not " |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
42 "preparable." % src.name) |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
43 continue |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
44 if src.is_theme_source: |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
45 logger.debug("Skipping source '%s' because it's a theme " |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
46 "source." % src.name) |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
47 continue |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
48 p = subparsers.add_parser( |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
49 src.item_name, |
165
8355eb9dd8fe
prepare: Show a more friendly user message when no arguments are given.
Ludovic Chabant <ludovic@chabant.com>
parents:
100
diff
changeset
|
50 help=("Creates an empty page in the '%s' source." % |
8355eb9dd8fe
prepare: Show a more friendly user message when no arguments are given.
Ludovic Chabant <ludovic@chabant.com>
parents:
100
diff
changeset
|
51 src.name)) |
100
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
52 src.setupPrepareParser(p, app) |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
53 p.add_argument('-t', '--template', default='default', |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
54 help="The template to use, which will change the " |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
55 "generated text and header.") |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
56 p.set_defaults(source=src) |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
57 |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
58 def run(self, ctx): |
165
8355eb9dd8fe
prepare: Show a more friendly user message when no arguments are given.
Ludovic Chabant <ludovic@chabant.com>
parents:
100
diff
changeset
|
59 if not hasattr(ctx.args, 'source'): |
8355eb9dd8fe
prepare: Show a more friendly user message when no arguments are given.
Ludovic Chabant <ludovic@chabant.com>
parents:
100
diff
changeset
|
60 raise Exception("No source specified. " |
8355eb9dd8fe
prepare: Show a more friendly user message when no arguments are given.
Ludovic Chabant <ludovic@chabant.com>
parents:
100
diff
changeset
|
61 "Please run `chef prepare -h` for usage.") |
8355eb9dd8fe
prepare: Show a more friendly user message when no arguments are given.
Ludovic Chabant <ludovic@chabant.com>
parents:
100
diff
changeset
|
62 |
100
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
63 app = ctx.app |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
64 source = ctx.args.source |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
65 metadata = source.buildMetadata(ctx.args) |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
66 rel_path, metadata = source.findPagePath(metadata, MODE_CREATING) |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
67 path = source.resolveRef(rel_path) |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
68 name, ext = os.path.splitext(path) |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
69 if ext == '.*': |
165
8355eb9dd8fe
prepare: Show a more friendly user message when no arguments are given.
Ludovic Chabant <ludovic@chabant.com>
parents:
100
diff
changeset
|
70 path = '%s.%s' % ( |
8355eb9dd8fe
prepare: Show a more friendly user message when no arguments are given.
Ludovic Chabant <ludovic@chabant.com>
parents:
100
diff
changeset
|
71 name, |
100
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
72 app.config.get('site/default_auto_format')) |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
73 if os.path.exists(path): |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
74 raise Exception("'%s' already exists." % path) |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
75 |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
76 tpl_name = ctx.args.template |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
77 extensions = self.getExtensions(app) |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
78 ext = next( |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
79 filter( |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
80 lambda e: tpl_name in e.getTemplateNames(ctx.app), |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
81 extensions), |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
82 None) |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
83 if ext is None: |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
84 raise Exception("No such page template: %s" % tpl_name) |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
85 |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
86 tpl_text = ext.getTemplate(ctx.app, tpl_name) |
187
d5b7c2a4ec9d
prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
165
diff
changeset
|
87 if tpl_text is None: |
d5b7c2a4ec9d
prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
165
diff
changeset
|
88 raise Exception("Error loading template: %s" % tpl_name) |
100
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
89 title = (metadata.get('slug') or metadata.get('path') or |
165
8355eb9dd8fe
prepare: Show a more friendly user message when no arguments are given.
Ludovic Chabant <ludovic@chabant.com>
parents:
100
diff
changeset
|
90 'Untitled page') |
100
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
91 title = make_title(title) |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
92 tokens = { |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
93 '%title%': title, |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
94 '%time.today%': time.strftime('%Y/%m/%d'), |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
95 '%time.now%': time.strftime('%H:%M:%S')} |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
96 tpl_text = multi_replace(tpl_text, tokens) |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
97 |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
98 logger.info("Creating page: %s" % os.path.relpath(path, app.root_dir)) |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
99 if not os.path.exists(os.path.dirname(path)): |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
100 os.makedirs(os.path.dirname(path), 0o755) |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
101 |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
102 with open(path, 'w') as f: |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
103 f.write(tpl_text) |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
104 |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
105 |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
106 class DefaultPrepareTemplatesCommandExtension(ChefCommandExtension): |
187
d5b7c2a4ec9d
prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
165
diff
changeset
|
107 """ Provides the default scaffolding templates to the `prepare` |
100
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
108 command. |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
109 """ |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
110 def __init__(self): |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
111 super(DefaultPrepareTemplatesCommandExtension, self).__init__() |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
112 self.command_name = 'prepare' |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
113 |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
114 def getTemplateNames(self, app): |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
115 return ['default', 'rss', 'atom'] |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
116 |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
117 def getTemplateDescription(self, app, name): |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
118 descs = { |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
119 'default': "The default template, for a simple page.", |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
120 'rss': "A fully functional RSS feed.", |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
121 'atom': "A fully functional Atom feed."} |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
122 return descs[name] |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
123 |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
124 def getTemplate(self, app, name): |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
125 assert name in ['default', 'rss', 'atom'] |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
126 src_path = os.path.join(RESOURCES_DIR, 'prepare', '%s.html' % name) |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
127 with open(src_path, 'r', encoding='utf8') as fp: |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
128 return fp.read() |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
129 |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
130 |
187
d5b7c2a4ec9d
prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
165
diff
changeset
|
131 class UserDefinedPrepareTemplatesCommandExtension(ChefCommandExtension): |
d5b7c2a4ec9d
prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
165
diff
changeset
|
132 """ Provides user-defined scaffolding templates to the `prepare` |
d5b7c2a4ec9d
prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
165
diff
changeset
|
133 command. |
d5b7c2a4ec9d
prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
165
diff
changeset
|
134 """ |
d5b7c2a4ec9d
prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
165
diff
changeset
|
135 def __init__(self): |
d5b7c2a4ec9d
prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
165
diff
changeset
|
136 super(UserDefinedPrepareTemplatesCommandExtension, self).__init__() |
d5b7c2a4ec9d
prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
165
diff
changeset
|
137 self.command_name = 'prepare' |
d5b7c2a4ec9d
prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
165
diff
changeset
|
138 |
d5b7c2a4ec9d
prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
165
diff
changeset
|
139 def _getTemplatesDir(self, app): |
d5b7c2a4ec9d
prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
165
diff
changeset
|
140 return os.path.join(app.root_dir, 'scaffold/pages') |
d5b7c2a4ec9d
prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
165
diff
changeset
|
141 |
d5b7c2a4ec9d
prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
165
diff
changeset
|
142 def supports(self, app): |
d5b7c2a4ec9d
prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
165
diff
changeset
|
143 return os.path.isdir(self._getTemplatesDir(app)) |
d5b7c2a4ec9d
prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
165
diff
changeset
|
144 |
d5b7c2a4ec9d
prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
165
diff
changeset
|
145 def getTemplateNames(self, app): |
d5b7c2a4ec9d
prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
165
diff
changeset
|
146 names = os.listdir(self._getTemplatesDir(app)) |
d5b7c2a4ec9d
prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
165
diff
changeset
|
147 return map(lambda n: os.path.splitext(n)[0], names) |
d5b7c2a4ec9d
prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
165
diff
changeset
|
148 |
d5b7c2a4ec9d
prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
165
diff
changeset
|
149 def getTemplateDescription(self, app, name): |
d5b7c2a4ec9d
prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
165
diff
changeset
|
150 return "User-defined template." |
d5b7c2a4ec9d
prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
165
diff
changeset
|
151 |
d5b7c2a4ec9d
prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
165
diff
changeset
|
152 def getTemplate(self, app, name): |
d5b7c2a4ec9d
prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
165
diff
changeset
|
153 templates_dir = self._getTemplatesDir(app) |
d5b7c2a4ec9d
prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
165
diff
changeset
|
154 pattern = os.path.join(templates_dir, '%s.*' % name) |
d5b7c2a4ec9d
prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
165
diff
changeset
|
155 matches = glob.glob(pattern) |
d5b7c2a4ec9d
prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
165
diff
changeset
|
156 if not matches: |
d5b7c2a4ec9d
prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
165
diff
changeset
|
157 raise Exception("No such page scaffolding template: %s" % name) |
d5b7c2a4ec9d
prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
165
diff
changeset
|
158 if len(matches) > 1: |
d5b7c2a4ec9d
prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
165
diff
changeset
|
159 raise Exception( |
d5b7c2a4ec9d
prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
165
diff
changeset
|
160 "More than one scaffolding template has name: %s" % name) |
d5b7c2a4ec9d
prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
165
diff
changeset
|
161 with open(matches[0], 'r', encoding='utf8') as fp: |
d5b7c2a4ec9d
prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
165
diff
changeset
|
162 return fp.read() |
d5b7c2a4ec9d
prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
165
diff
changeset
|
163 |
d5b7c2a4ec9d
prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents:
165
diff
changeset
|
164 |
100
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
165 class DefaultPrepareTemplatesHelpTopic(ChefCommandExtension): |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
166 """ Provides help topics for the `prepare` command. |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
167 """ |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
168 command_name = 'help' |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
169 |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
170 def getHelpTopics(self): |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
171 return [('scaffolding', |
165
8355eb9dd8fe
prepare: Show a more friendly user message when no arguments are given.
Ludovic Chabant <ludovic@chabant.com>
parents:
100
diff
changeset
|
172 "Available templates for the 'prepare' command.")] |
100
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
173 |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
174 def getHelpTopic(self, topic, app): |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
175 with io.StringIO() as tplh: |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
176 extensions = app.plugin_loader.getCommandExtensions() |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
177 for e in extensions: |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
178 if e.command_name == 'prepare' and e.supports(app): |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
179 for n in e.getTemplateNames(app): |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
180 d = e.getTemplateDescription(app, n) |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
181 print_help_item(tplh, n, d) |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
182 help_list = tplh.getvalue() |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
183 |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
184 help_txt = ( |
165
8355eb9dd8fe
prepare: Show a more friendly user message when no arguments are given.
Ludovic Chabant <ludovic@chabant.com>
parents:
100
diff
changeset
|
185 textwrap.fill( |
8355eb9dd8fe
prepare: Show a more friendly user message when no arguments are given.
Ludovic Chabant <ludovic@chabant.com>
parents:
100
diff
changeset
|
186 "Running the 'prepare' command will let " |
8355eb9dd8fe
prepare: Show a more friendly user message when no arguments are given.
Ludovic Chabant <ludovic@chabant.com>
parents:
100
diff
changeset
|
187 "PieCrust setup a page for you in the correct place, with " |
8355eb9dd8fe
prepare: Show a more friendly user message when no arguments are given.
Ludovic Chabant <ludovic@chabant.com>
parents:
100
diff
changeset
|
188 "some hopefully useful default text.") + |
100
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
189 "\n\n" + |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
190 textwrap.fill("The following templates are available:") + |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
191 "\n\n" + |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
192 help_list) |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
193 return help_txt |
69d5eecfa449
Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
194 |