annotate piecrust/commands/builtin/scaffolding.py @ 535:03c3a77fda60 2.0.0b2

prepare: More help about scaffolding.
author Ludovic Chabant <ludovic@chabant.com>
date Wed, 29 Jul 2015 09:13:54 -0700
parents 5bbeb11fe8d9
children 87f1e79d3fbe
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
242
f130365568ff internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents: 187
diff changeset
12 from piecrust.sources.base import MODE_CREATING
f130365568ff internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents: 187
diff changeset
13 from piecrust.sources.interfaces import IPreparingSource
100
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
14 from piecrust.uriutil import multi_replace
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
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
17 logger = logging.getLogger(__name__)
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
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
20 def make_title(slug):
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
21 slug = re.sub(r'[\-_]', ' ', slug)
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
22 return slug.title()
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
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
25 class PrepareCommand(ExtendableChefCommand):
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
26 """ 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
27 """
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
28 def __init__(self):
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
29 super(PrepareCommand, self).__init__()
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
30 self.name = 'prepare'
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
31 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
32
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
33 def setupParser(self, parser, app):
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
34 # 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
35 # (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
36 if app.root_dir is None:
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
37 return
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
38
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
39 subparsers = parser.add_subparsers()
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
40 for src in app.sources:
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
41 if not isinstance(src, IPreparingSource):
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
42 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
43 "preparable." % src.name)
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
44 continue
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
45 if src.is_theme_source:
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
46 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
47 "source." % src.name)
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
48 continue
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
49 p = subparsers.add_parser(
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
50 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
51 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
52 src.name))
100
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
53 src.setupPrepareParser(p, app)
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
54 p.add_argument('-t', '--template', default='default',
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
55 help="The template to use, which will change the "
535
03c3a77fda60 prepare: More help about scaffolding.
Ludovic Chabant <ludovic@chabant.com>
parents: 534
diff changeset
56 "generated text and header. Run `chef help "
03c3a77fda60 prepare: More help about scaffolding.
Ludovic Chabant <ludovic@chabant.com>
parents: 534
diff changeset
57 "scaffolding` for more information.")
100
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
58 p.set_defaults(source=src)
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
59
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
60 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
61 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
62 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
63 "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
64
100
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
65 app = ctx.app
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
66 source = ctx.args.source
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
67 metadata = source.buildMetadata(ctx.args)
363
dd25bd3ce1f9 serve: Refactoring and fixes to be able to serve taxonomy pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 242
diff changeset
68 factory = source.findPageFactory(metadata, MODE_CREATING)
dd25bd3ce1f9 serve: Refactoring and fixes to be able to serve taxonomy pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 242
diff changeset
69 path = factory.path
100
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
70 name, ext = os.path.splitext(path)
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
71 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
72 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
73 name,
100
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
74 app.config.get('site/default_auto_format'))
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
75 if os.path.exists(path):
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
76 raise Exception("'%s' already exists." % path)
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
77
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
78 tpl_name = ctx.args.template
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
79 extensions = self.getExtensions(app)
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
80 ext = next(
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
81 filter(
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
82 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
83 extensions),
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
84 None)
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
85 if ext is None:
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
86 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
87
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
88 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
89 if tpl_text is None:
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
90 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
91 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
92 'Untitled page')
100
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
93 title = make_title(title)
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
94 tokens = {
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
95 '%title%': title,
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
96 '%time.today%': time.strftime('%Y/%m/%d'),
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
97 '%time.now%': time.strftime('%H:%M:%S')}
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
98 tpl_text = multi_replace(tpl_text, tokens)
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
99
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
100 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
101 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
102 os.makedirs(os.path.dirname(path), 0o755)
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
103
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
104 with open(path, 'w') as f:
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
105 f.write(tpl_text)
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
106
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
107
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
108 class DefaultPrepareTemplatesCommandExtension(ChefCommandExtension):
187
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
109 """ 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
110 command.
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
111 """
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
112 def __init__(self):
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
113 super(DefaultPrepareTemplatesCommandExtension, self).__init__()
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
114 self.command_name = 'prepare'
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
115
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
116 def getTemplateNames(self, app):
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
117 return ['default', 'rss', 'atom']
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
118
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
119 def getTemplateDescription(self, app, name):
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
120 descs = {
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
121 '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
122 'rss': "A fully functional RSS feed.",
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
123 'atom': "A fully functional Atom feed."}
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
124 return descs[name]
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
125
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
126 def getTemplate(self, app, name):
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
127 assert name in ['default', 'rss', 'atom']
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
128 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
129 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
130 return fp.read()
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
131
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
132
187
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
133 class UserDefinedPrepareTemplatesCommandExtension(ChefCommandExtension):
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
134 """ Provides user-defined scaffolding templates to the `prepare`
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
135 command.
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
136 """
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
137 def __init__(self):
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
138 super(UserDefinedPrepareTemplatesCommandExtension, self).__init__()
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
139 self.command_name = 'prepare'
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
140
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
141 def _getTemplatesDir(self, app):
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
142 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
143
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
144 def supports(self, app):
534
5bbeb11fe8d9 bug: Fix crash running `chef help scaffolding` outside of a website.
Ludovic Chabant <ludovic@chabant.com>
parents: 363
diff changeset
145 if not app.root_dir:
5bbeb11fe8d9 bug: Fix crash running `chef help scaffolding` outside of a website.
Ludovic Chabant <ludovic@chabant.com>
parents: 363
diff changeset
146 return False
187
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
147 return os.path.isdir(self._getTemplatesDir(app))
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 getTemplateNames(self, app):
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
150 names = os.listdir(self._getTemplatesDir(app))
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
151 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
152
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
153 def getTemplateDescription(self, app, name):
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
154 return "User-defined template."
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
155
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
156 def getTemplate(self, app, name):
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
157 templates_dir = self._getTemplatesDir(app)
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
158 pattern = os.path.join(templates_dir, '%s.*' % name)
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
159 matches = glob.glob(pattern)
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
160 if not matches:
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
161 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
162 if len(matches) > 1:
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
163 raise Exception(
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
164 "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
165 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
166 return fp.read()
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
167
d5b7c2a4ec9d prepare: Add user-defined scaffolding templates.
Ludovic Chabant <ludovic@chabant.com>
parents: 165
diff changeset
168
100
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
169 class DefaultPrepareTemplatesHelpTopic(ChefCommandExtension):
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
170 """ Provides help topics for the `prepare` command.
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
171 """
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
172 command_name = 'help'
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 getHelpTopics(self):
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
175 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
176 "Available templates for the 'prepare' command.")]
100
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
177
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
178 def getHelpTopic(self, topic, app):
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
179 with io.StringIO() as tplh:
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
180 extensions = app.plugin_loader.getCommandExtensions()
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
181 for e in extensions:
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
182 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
183 for n in e.getTemplateNames(app):
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
184 d = e.getTemplateDescription(app, n)
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
185 print_help_item(tplh, n, d)
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
186 help_list = tplh.getvalue()
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
187
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
188 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
189 textwrap.fill(
8355eb9dd8fe prepare: Show a more friendly user message when no arguments are given.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
190 "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
191 "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
192 "some hopefully useful default text.") +
100
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
193 "\n\n" +
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
194 textwrap.fill("The following templates are available:") +
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
195 "\n\n" +
535
03c3a77fda60 prepare: More help about scaffolding.
Ludovic Chabant <ludovic@chabant.com>
parents: 534
diff changeset
196 help_list +
03c3a77fda60 prepare: More help about scaffolding.
Ludovic Chabant <ludovic@chabant.com>
parents: 534
diff changeset
197 "\n" +
03c3a77fda60 prepare: More help about scaffolding.
Ludovic Chabant <ludovic@chabant.com>
parents: 534
diff changeset
198 "You can add user-defined templates by creating pages in a "
03c3a77fda60 prepare: More help about scaffolding.
Ludovic Chabant <ludovic@chabant.com>
parents: 534
diff changeset
199 "`scaffold/pages` sub-directory in your website.")
100
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
200 return help_txt
69d5eecfa449 Better `prepare` command, with templates and help topics.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
201